Question

In: Computer Science

Ice Cream Program Assignment Write a program that uses a function to ask the user to...

Ice Cream Program Assignment

Write a program that uses a function to ask the user to choose an ice cream flavor from a menu (see output below.) You must validate the users input for the flavor of ice cream accounting for both upper and lower-case letters. You must give them an appropriate error message and allow them to try again.   Once you have a valid flavor, your function will return the flavor back to the main() function.   

Your main() function will then ask for the number of scoops. You must validate this data! Make sure that the user chooses at least 1 scoop but no more than 4. If they try another other, you must give them an error message and allow them to try again.   

Your program will then calculate and display the cost of the ice cream. The cost of ice cream is $ .75 for the cone and $1.25 per scoop.

Your program will continue asking customers for the flavor and number of scoops until they choose ‘Q’ to quit.

The program will then send all of the data to a function to display the total number of cones sold, the total amount collected, and the total scoops of each type of ice cream sold.  

Sample Output:

Please Choose your Favorite Flavor!

        V - Vanilla

        C - Chocolate

        F - Fudge

        Q - Quit

-----> v

How many scoops would you like? 2

Your ice cream cone cost $   3.25 Please Choose your Favorite Flavor!

        V - Vanilla

        C - Chocolate

        F - Fudge

        Q - Quit

-----> c

How many scoops would you like? 5

That is an invalid number of scoops! You may only choose between 1 and 4

Please try again!

How many scoops would you like? 0

That is an invalid number of scoops! You may only choose between 1 and 4

Please try again!

How many scoops would you like? 3

Your ice cream cone cost $   4.50 Please Choose your Favorite Flavor!

        V - Vanilla

        C - Chocolate

        F - Fudge

        Q - Quit

-----> s

How many scoops would you like? 1

Your ice cream cone cost $   2.00 Please Choose your Favorite Flavor!

        V - Vanilla

        C - Chocolate

        F - Fudge

        Q - Quit

-----> q

The total number of cones sold:           3

The total scoops of vanilla sold:         2

The total scoops of chocolate sold:       3

The total scoops of fudge sold:           1

The total amount collected:         $ 9.75

Solutions

Expert Solution

#include<iostream>
using namespace std;
char icecream()
{
    char x;
    cout<<"Please choose ypour favorite flavor!"<<endl;
    cout<<" V - Vanilla\nC - Chocolate\nF - Fudge\nQ - Quit"<<endl;
    cin>>x;
    return x;
}
int main()
{
    float n, total;
    int v_count=0, c_count=0, f_count=0, no_of_cones=0;
    char x=icecream();
    while(1)
    {
        if(x=='v' || x=='V' || x=='c' || x=='C' || x=='f' || x=='F')
        {
            if(x=='v' || x=='V')
            {
                cout<<"How many scoops do you like?";
                cin>>n;
                if(n>=5 || n<=0)
                {
                    cout<<"That is an invalid number of scoops. Please choosew between 1 and 4";
                }
                else
                {
                    cout<<"Your icecream cone costs$"<<n*1.25 + 0.75;
                }
                total=total+n*1.25 + 0.75;
                v_count+=v_count*n;
            }
            else if(x=='f' || x=='F')
            {
                cout<<"How many scoops do you like?";
                cin>>n;
                if(n>=5 || n<=0)
                {
                    cout<<"That is an invalid number of scoops. Please choosew between 1 and 4";
                }
                else
                {
                    cout<<"Your icecream cone costs$"<<n*1.25 + 0.75;
                }
                f_count+=f_count*n;
                total=total+n*1.25 + 0.75;
            }
            else if(x=='c' || x=='C')
            {
                cout<<"How many scoops do you like?";
                cin>>n;
                if(n>=5 || n<=0)
                {
                    cout<<"That is an invalid number of scoops. Please choosew between 1 and 4";
                }
                else
                {
                    cout<<"Your icecream cone costs$"<<n*1.25 + 0.75;
                }
                c_count+=c_count*n;
                total=total+n*1.25 + 0.75;
            }
            no_of_cones++;
        }
        else if(x=='q' || x=='Q')
        {
            break;
        }
        else
        {
            cout<<"Invalid input. Please try again!";
        }
        x=icecream();
    }
    cout<<"The total number of cones sold:"<<no_of_cones<<endl;
    cout<<"The total scoops of vanilla sold:"<<v_count<<endl;
    cout<<"The total scoops of chocolate sold:"<<c_count<<endl;
    cout<<"The total scoops of fudge sold:"<<f_count<<endl;
    cout<<"The total amount collected:"<<total;
    return 0;
}

Related Solutions

Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
Write a program will ask the user for the annual income and the number of years...
Write a program will ask the user for the annual income and the number of years that the user has worked as their current job. The program will tell the user whether or not he or she qualifies for a loan based on the following condition: income is equal to or exceeds $35,000 or number of years on job is greater than 5 Do not use “nested if statements” in your solution. As an example of the output: What is...
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT