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 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...
*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...
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
Write a C program that calculates a student grade in the C Programming Class. Ask the...
Write a C program that calculates a student grade in the C Programming Class. Ask the user to enter the grades for each one of the assignments completed in class: Quiz #1 - 25 points Quiz #2 - 50 points Quiz #3 - 30 points Project #1 - 100 points Project #2 - 100 points Final Test - 100 points The total of the quizzes count for a 30% of the total grade, the total of the projects counts for...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
please write it in printf and scanf please Please write it in c# program please and...
please write it in printf and scanf please Please write it in c# program please and if possible no arrays only loops Loop Introduction Assignment Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches. Your function will convert the weight and height into Body Mass...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT