Question

In: Computer Science

Please write a program in c++ The method sellProduct of the Juice Machine programming example gives...

Please write a program in c++ The method sellProduct of the Juice Machine programming example gives the user only two chances to enter enough money to buy the product. Rewrite the definition of the method sellProduct so that it keeps prompting the user to enter more money as long as the user has not entered enough money to buy the product. Also, write a program to test your method.

Your program should produce the following example output:

*** Welcome to Shelly's Juice Shop ***                               
To select an item, enter                                             
1 for orange juice (50 cents)                                        
2 for apple juice (65 cents)                                         
3 for mango juice (80 cents)                                         
4 for strawberry banana juice (85 cents)                             
9 to exit                                                            
3                                                                    
Please deposit 80 cents                                              
40                                                                   
Please deposit another 40 cents.                                     
40                                                                   
Collect your item at the bottom and enjoy.                           
*** Welcome to Shelly's Juice Shop ***                               
To select an item, enter                                             
1 for orange juice (50 cents)                                        
2 for apple juice (65 cents)                                         
3 for mango juice (80 cents)                                         
4 for strawberry banana juice (85 cents)                             
9 to exit                                                            
9

Solutions

Expert Solution

#include<iostream>
using namespace std;

//calling the method sell product
int sellProduct(int select)
{
    int price,temp;
    //condition to check selection
    if(select == 1)
    {
        //for select 1 prompt to enter 50 cents
        cout << "Please deposit 50 cents\n";
        cin >> price;
        //prompt the user,untill user deposited enough money
        while(price != 50)
        {
            cout << "Please deposit another " << 50-price<<" cents\n";
            temp = price;
            cin >> price;
            price = temp + price;
        }        
    }
   
    else if(select == 2)
    {
        //for selection 2,prompt to enter 65 cents
        cout << "Please deposit 65 cents\n";
        cin >> price;
        //prompt the user,untill user deposited enough money
        while(price != 65)
        {
            cout << "Please deposit another " << 65-price<<" cents\n";
            temp = price;
            cin >> price;
            price = temp + price;
        }
    }
    else if(select == 3)
    {
        //for selection 3,prompt to enter 80 cents
        cout << "Please deposit 80 cents\n";
        cin >> price;
        //prompt the user,untill user deposited enough money
        while(price != 80)
        {
            cout << "Please deposit another " << 80-price<<" cents\n";
            temp = price;
            cin >> price;
            price = temp + price;
        }
    }
    else if(select == 4)
    {
        //for selection 4,prompt to enter 85 cents
        cout << "Please deposit 85 cents\n";
        cin >> price;
        //prompt the user,untill user deposited enough money
        while(price != 85)
        {
            cout << "Please deposit another " << 85-price<<" cents\n";
            temp = price;
            cin >> price;
            price = temp + price;
        }
    }
    else if(select == 9)
    {
         // if user select 9,then exit the code
         cout <<"Exit";
        return -1;
    }
    else
    {
        //if user enter wrong selection
          cout <<"Wrong selection\n";
          return 0;
    }
    return 0;
}
int main()
{
    int select,price,i;
    //do- while loop doesn't check the condition in first iteration.From second iteration it checks the condition
    //loop used to prompt the user,until user want to exit
    do
    {
        cout << "*** Welcome to Shelly's Juice Shop ***\n";
        cout << "To select an item,enter\n";
        cout << "1 for orange juice (50 cents)\n";
        cout << "2 for apple juice (65 cents)\n";
        cout << "3 for mango juice (80 cents)\n";
        cout << "4 for strawberry banana juice (85 cents)\n";
        cout << "9 to exit\n";
   
        cin >> select;
        //calling the method sellproduct()
        i = sellProduct(select);
        //This condition to exit the program
        if(i== -1)
        return 0;
       
        cout << "Collect your item at the bottom and enjoy\n\n";
    }while(i != -1);
}

Continued from above picture


Related Solutions

Write in C programming using if and else statements only please!!! Write a program that plays...
Write in C programming using if and else statements only please!!! Write a program that plays the following card game: The user starts out with a pot of $100. At each hand of the game, the dealer and the player are dealt a random number between 1 and 52. The player wins $20 if his/her number is greater than the dealer's number; otherwise they lose $20.
C Programming Please write a single function and not the whole program Can you please provide...
C Programming Please write a single function and not the whole program Can you please provide comments and explanations for all questions. Thank you Write a single function for the following: void split_date(int day_of_year, int year, int *month, int *day);             day_of_year is an integer between 1 and 366, specifying a particular day within the year designated by the parameter year. Month and day point to variables in which the function will store the equivalent month (1 – 12) and day...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
<Using C++ programming.> <Prof said that you use the example given below.> Write a program to...
<Using C++ programming.> <Prof said that you use the example given below.> Write a program to find (A*B)*10 + (2*C+D) using class Matrix. For member functions, use add() and mul(). Add() -a,b,c are all m*n matrices. -Add(a,b,c,m,n): cij = aij + bij -Add(a,m,n,num): cij = aij + num Mul() -a is an m*n matrix, b is an n*p matrix, and c is an m*p matrix. -mul(a,b,c,m,n): (Matrix Multiplication) -mul(a,m,n,num): aij = aij * num
*Answer must be in C++ and please use a Windows machine NOT IOS! Write a program...
*Answer must be in C++ and please use a Windows machine NOT IOS! Write a program as follows: Ask the user for an integer representing a number of integers to be sorted. Create an array of integers of the size provided by user. Initialize the array to zeros. Ask the user for and populate the array with user input. Output the array elements on one line separated by a space. Write a function name “supersort” that takes an integer pointer...
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Programming In C Write a program that prints the values in an array and the addresses...
Programming In C Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT