473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Ice Cream Problem

Hey everyone, Ok, this problem is giving me some trouble. I got it to iterate but I want it to generate an error message if a user types in an incorrect choice. I got the error message down however I think something may wrong with my do-while loop. It wont execute properly. Whenever I want to perform another transaction, my error message pops up. Any help will be appreciated.






Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std; 
  4.  
  5.  
  6. int main()
  7. {
  8.  
  9.     cout <<setprecision(2)
  10.         <<setiosflags(ios::fixed)
  11.         <<setiosflags(ios::showpoint);
  12.  
  13.  
  14.     char choice,
  15.          stop;
  16.  
  17.     double choco_D = 0.50,
  18.             van_S = 0.50,
  19.             straw_B = 0.65;
  20.  
  21.      double num_needed = 0;
  22.  
  23.  
  24.     do
  25.     {
  26.     cout << "\t********************************************************\n";
  27.     cout << "            Welcome to Rainbow Ice Cream                   \n";
  28.     cout << "\t========================================================\n";
  29.     cout << "\t C - Chocolate Deluxe                         **\n";
  30.     cout << "\t V - Vanilla Special                         **\n";
  31.     cout << "\t S - Strawberry Burst                         **\n";
  32.     cout << endl;
  33.     cout << "\t      Please select your flavor of Ice Cream: ";
  34.     choice = cin.get();
  35.     cout << "\n\n";
  36.  
  37.     switch (choice)
  38.     {
  39.     case 'C':
  40.         cout << "\t You have chosen Chocolate Deluxe\n\n";
  41.         cout << "\t Please enter the number of Ice creams needed: ";
  42.         cin >> num_needed;
  43.         cout << "\t" << num_needed << " Chocolate Deluxe, $" <<  (num_needed * choco_D)<< "\n\n";
  44.         break;
  45.     case 'V':
  46.         cout << "\t You have chosen Vanilla Special\n\n";
  47.         cout << "\t Please enter the number of Ice creams needed: ";
  48.         cin >> num_needed;
  49.         cout << "\t" << num_needed << " Vanilla Special, $" << (num_needed * van_S) << "\n\n";
  50.         break;
  51.     case 'S': 
  52.         cout << "\t You have chosen Strawberry Burst\n\n" ;
  53.         cout << "\t Please enter the number of Ice creams needed: ";
  54.         cin >> num_needed;
  55.         cout << "\t" << num_needed << " Strawberry Burst, $" << (num_needed * straw_B) << "\n\n" ;
  56.         break;
  57.     default:
  58.         cout << "\t Not a valid choice! Please enter again.: ";
  59.         cin >> choice;
  60.         break;
  61.     }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.     cout << "\n\nWould you like to process another transaction (Y/Z)?: ";
  69.     cin >> stop;
  70.     cout << "\n\n";
  71.     }
  72.     while(stop != 'Z'); 
  73.  
  74.  
  75.     return 0;
  76. }
Oct 3 '07 #1
14 2545
Hey everyone, Ok, this problem is giving me some trouble. I got it to iterate but I want it to generate an error message if a user types in an incorrect choice. I got the error message down however I think something may wrong with my do-while loop. It wont execute properly. Whenever I want to perform another transaction, my error message pops up. Any help will be appreciated.






#include <iostream>
#include <iomanip>
using namespace std;


int main()
{

cout <<setprecision(2)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);


char choice,
stop;

double choco_D = 0.50,
van_S = 0.50,
straw_B = 0.65;

double num_needed = 0;


do
{
cout << "\t*********************************************** *********\n";
cout << " Welcome to Rainbow Ice Cream \n";
cout << "\t=============================================== =========\n";
cout << "\t C - Chocolate Deluxe **\n";
cout << "\t V - Vanilla Special **\n";
cout << "\t S - Strawberry Burst **\n";
cout << endl;
cout << "\t Please select your flavor of Ice Cream: ";
choice = cin.get();
cout << "\n\n";

switch (choice)
{
case 'C':
cout << "\t You have chosen Chocolate Deluxe\n\n";
cout << "\t Please enter the number of Ice creams needed: ";
cin >> num_needed;
cout << "\t" << num_needed << " Chocolate Deluxe, $" << (num_needed * choco_D)<< "\n\n";
break;
case 'V':
cout << "\t You have chosen Vanilla Special\n\n";
cout << "\t Please enter the number of Ice creams needed: ";
cin >> num_needed;
cout << "\t" << num_needed << " Vanilla Special, $" << (num_needed * van_S) << "\n\n";
break;
case 'S':
cout << "\t You have chosen Strawberry Burst\n\n" ;
cout << "\t Please enter the number of Ice creams needed: ";
cin >> num_needed;
cout << "\t" << num_needed << " Strawberry Burst, $" << (num_needed * straw_B) << "\n\n" ;
break;
default:
cout << "\t Not a valid choice! Please enter again.: ";
cin >> choice;
break;
}






cout << "\n\nWould you like to process another transaction (Y/Z)?: ";
cin >> stop;
cout << "\n\n";
}
while(stop != 'Z');


return 0;
}

After 1 iteration of the do while loop, cin either contains a Y or a Z.

Then, on the second iteration, it exectues choice = cin.get(), which takes either this Y or Z as your choice, and thus it is invalid. You need to flush cin before the loop ends. This can be done by adding:

Expand|Select|Wrap|Line Numbers
  1. cin.ignore(1, ' ');
before the close of the do loop.

Or you can change cin.get() to

Expand|Select|Wrap|Line Numbers
  1. cin >> choice;
I think either of those should work
Oct 3 '07 #2
Yah thanks, that helped. However whenever I perform another transaction and the user enters an invalid choice, the user is then prompted to enter a correct choice. When the user does so, it goes straight to asking if I want to perform another transaction. How do I get it to iterate again?
Oct 3 '07 #3
Yah thanks, that helped. However whenever I perform another transaction and the user enters an invalid choice, the user is then prompted to enter a correct choice. When the user does so, it goes straight to asking if I want to perform another transaction. How do I get it to iterate again?

Here's a hint:

You need to keep on iterating the switch statement unless the user provides a valid choice. Right now the switch statement that asks the user for his/her choice only iterates once per 'transaction'.
Oct 3 '07 #4
yah, thats my problem. I have no idea how to do that. Nested loop? if statement?
Oct 4 '07 #5
yah, thats my problem. I have no idea how to do that. Nested loop? if statement?
Try this. Before you enter the switch statement, ensure that choice is either C, V, or S.

So some pseudo code would be as follows:

while (choice isn't C V or S) {
get choice from the user
}

switch (choice) {
case C
..
case V
..
case S
..
}
Oct 4 '07 #6
Expand|Select|Wrap|Line Numbers
  1.  
  2. while( choice != C || V || S)
  3. cin >> choice;
  4.  
  5.  
like that?
Oct 4 '07 #7
Expand|Select|Wrap|Line Numbers
  1.  
  2. while( choice != C || V || S)
  3. cin >> choice;
  4.  
  5.  
like that?
Lets think about this logically. We want the while loop that finds valid inputs to terminate if choice == C or choice == V or choice == S.

If the above is not the case, then we want to continue in order to find a valid input.

In other words, the loop should:
STOP when choice == C or choice == V or choice == S
GO if this not the case.

while (loop condition is GO) {
try and get valid value for choice from user
}


translated into C++, this is:

Expand|Select|Wrap|Line Numbers
  1. while ( !(choice == 'C' || choice == 'V' || choice == 'S')) {
  2.     cout << "\t Not a valid choice! Please enter again.: ";
  3.     cin >> choice;
  4. }
  5.  
Make sense?
Oct 4 '07 #8
yes that makes so much sense now. However now when I prompt the user

Expand|Select|Wrap|Line Numbers
  1. cout << "\n\nWould you like to process another transaction (Y/Z)?: ";
  2.     cin >> stop;
  3.     cout << "\n\n";
  4.     }
  5.     while(stop != 'Z'); 
  6.  
  7.  
it won't stop iterating when I type 'Z'. It just iterates again.
Oct 4 '07 #9
yes that makes so much sense now. However now when I prompt the user

Expand|Select|Wrap|Line Numbers
  1. cout << "\n\nWould you like to process another transaction (Y/Z)?: ";
  2.     cin >> stop;
  3.     cout << "\n\n";
  4.     }
  5.     while(stop != 'Z'); 
  6.  
  7.  
it won't stop iterating when I type 'Z'. It just iterates again.

Can you post your whole code?
Oct 4 '07 #10
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std; 
  4.  
  5.  
  6. int main()
  7. {
  8.  
  9.     cout <<setprecision(2)
  10.         <<setiosflags(ios::fixed)
  11.         <<setiosflags(ios::showpoint);
  12.  
  13.  
  14.     char choice,  
  15.         stop;
  16.  
  17.     double choco_D = 0.50,
  18.             van_S = 0.50,
  19.             straw_B = 0.65;
  20.  
  21.     double orderC = 40;
  22.     double orderV = 50;
  23.     double orderS = 35;
  24.  
  25.      double num_needed = 0;
  26.  
  27.  
  28.     do
  29.     {
  30.     cout << "\t********************************************************\n";
  31.     cout << "            Welcome to Rainbow Ice Cream                   \n";
  32.     cout << "\t========================================================\n";
  33.     cout << "\t C - Chocolate Deluxe                         **\n";
  34.     cout << "\t V - Vanilla Special                         **\n";
  35.     cout << "\t S - Strawberry Burst                         **\n";
  36.     cout << endl;
  37.     cout << "\t      Please select your flavor of Ice Cream: ";
  38.     cin >> choice;
  39.     cout << "\n\n";
  40.  
  41.       while ( !(choice == 'C' || choice == 'V' || choice == 'S')) 
  42.       {
  43.           cout << "\t Not a valid choice! Please enter again.: ";
  44.           cin >> choice;
  45.       }
  46.  
  47.  
  48.     switch (choice)
  49.     {
  50.     case 'C':
  51.         cout << "\t You have chosen Chocolate Deluxe\n\n";
  52.         cout << "\t Please enter the number of Ice creams needed: ";
  53.         cin >> num_needed;
  54.         cout << "\t" << num_needed << " Chocolate Deluxe, $" <<  (num_needed * choco_D)<< "\n\n";
  55.         break;
  56.     case 'V':
  57.         cout << "\t You have chosen Vanilla Special\n\n";
  58.         cout << "\t Please enter the number of Ice creams needed: ";
  59.         cin >> num_needed;
  60.         cout << "\t" << num_needed << " Vanilla Special, $" << (num_needed * van_S) << "\n\n";
  61.         break;
  62.     case 'S': 
  63.         cout << "\t You have chosen Strawberry Burst\n\n" ;
  64.         cout << "\t Please enter the number of Ice creams needed: ";
  65.         cin >> num_needed;
  66.         cout << "\t" << num_needed << " Strawberry Burst, $" << (num_needed * straw_B) << "\n\n" ;
  67.         break;
  68.     }
  69.  
  70.  
  71.  
  72.  
  73.     cout << "\n\nWould you like to process another transaction (Y/Z)?: ";
  74.     cin >> stop;
  75.     cout << "\n\n";
  76.     }
  77.     while(stop == 'Z'); 
  78.  
  79.        return 0;
  80. }
  81.  
Oct 4 '07 #11
Line 77 says

Expand|Select|Wrap|Line Numbers
  1. while(stop == 'Z')
You would want to change that to !=
Oct 4 '07 #12
the variables orderC etc are for tracking inventory. The numbers next to it is the amount of ice cream I have in stock. She wants to track it and display how much ice cream is left and display if there is no more in stock. Just so you know. At the end I have to display a sales report showing SOLD, IN STOCK, NEEDED, SALES and PROFIT. The calculations go after the loop correct?
Oct 4 '07 #13
I am having trouble coming up with and placing the calculations for the Sales report on the bottom of the program. Help?


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std; 
  4.  
  5.  
  6. int main()
  7. {
  8.  
  9.     cout <<setprecision(2)
  10.         <<setiosflags(ios::fixed)
  11.         <<setiosflags(ios::showpoint);
  12.  
  13.  
  14.     char choice,  
  15.         stop;
  16.  
  17.     double choco_D = 0.50,
  18.             van_S = 0.50,
  19.             straw_B = 0.65;
  20.  
  21.     double stock_C = 0;
  22.     double stock_V = 0;
  23.     double stock_S = 0;
  24.  
  25.      int num_needed = 0;
  26.  
  27.  
  28.     do
  29.     {
  30.     cout << "\t********************************************************\n";
  31.     cout << "            Welcome to Rainbow Ice Cream                   \n";
  32.     cout << "\t========================================================\n";
  33.     cout << "\t C - Chocolate Deluxe                         **\n";
  34.     cout << "\t V - Vanilla Special                         **\n";
  35.     cout << "\t S - Strawberry Burst                         **\n";
  36.     cout << endl;
  37.     cout << "\t      Please select your flavor of Ice Cream: ";
  38.     cin >> choice;
  39.     cout << "\n\n";
  40.  
  41.       while ( !(choice == 'C' || choice == 'V' || choice == 'S')) 
  42.       {
  43.           cout << "\t Not a valid choice! Please enter again.: ";
  44.           cin >> choice;
  45.       }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.     switch (choice)
  54.     {
  55.     case 'C':  // CHOCOALTE DELUXE OPTION
  56.         cout << "\t You have chosen Chocolate Deluxe\n\n";
  57.         cout << "\t Please enter the number of Ice creams needed: ";
  58.         cin >> num_needed;
  59.  
  60.         if(num_needed < 40)
  61.             cout << "\t " << num_needed << " Chocolate Deluxe, $" << (num_needed * choco_D) << "\n\n";
  62.             else
  63.                 cout << "Sorry! Out of Stock!\n";
  64.             break;
  65.  
  66.  
  67.  
  68.     case 'V':  // VANILLA SPECIAL OPTION
  69.         cout << "\t You have chosen Vanilla Special\n\n";
  70.         cout << "\t Please enter the number of Ice creams needed: ";
  71.         cin >> num_needed;
  72.  
  73.         if(num_needed < 50)
  74.             cout << "\t" << num_needed << " Vanilla Special, $" << (num_needed * van_S) << "\n\n";
  75.             else
  76.             cout << "Sorry! Out of Stock!\n";
  77.         break;
  78.  
  79.  
  80.  
  81.  
  82.     case 'S': // STRAWBERRY BURST OPTION
  83.         cout << "\t You have chosen Strawberry Burst\n\n" ;
  84.         cout << "\t Please enter the number of Ice creams needed: ";
  85.         cin >> num_needed;
  86.  
  87.         if(num_needed < 35)
  88.             cout << "\t" << num_needed << " Strawberry Burst, $" << (num_needed * straw_B) << "\n\n"; 
  89.             else 
  90.             cout << "Sorry! Out of Stock!\n";
  91.         break;
  92.     }
  93.  
  94.  
  95.  
  96.  
  97.     cout << "\n\nWould you like to process another transaction (Y/Z)?: ";
  98.     cin >> stop;
  99.     cout << "\n\n";
  100.     }
  101.     while(stop != 'Z'); // END DO-WHILE LOOP
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.     //CALCULATIONS
  109.  
  110.  
  111.  
  112.     //DISPLAY REPORT
  113.     cout <<"\n\n\n\n\n\n****************** Rainbow's Ice Cream Inventory Report: ******************\n\n";
  114.     cout << setw(28)<< "Sold" << setw(12) << "In Stock" << setw(12) << "Needed" << setw(12) << "Sales" << setw(12) << "Profit\n\n";
  115.     cout <<"Chocolate Deluxe\n";
  116.     cout <<"Vanilla Special\n";
  117.     cout <<"Strawberry Burst\n\n";
  118.     cout <<"Grand Total\n\n";
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.     return 0;
  126. }
  127.  
  128.  
Oct 4 '07 #14
What does your sales report need to include?
Oct 4 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: urquhart.nak | last post by:
crack cream
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.