473,695 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ice Cream Problem

16 New Member
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 2559
kaioshin00
46 New Member
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(i os::fixed)
<<setiosflags(i os::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
audiokarate
16 New Member
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
kaioshin00
46 New Member
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
audiokarate
16 New Member
yah, thats my problem. I have no idea how to do that. Nested loop? if statement?
Oct 4 '07 #5
kaioshin00
46 New Member
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
audiokarate
16 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2. while( choice != C || V || S)
  3. cin >> choice;
  4.  
  5.  
like that?
Oct 4 '07 #7
kaioshin00
46 New Member
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
audiokarate
16 New Member
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
kaioshin00
46 New Member
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

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

Similar topics

0
3069
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 the context of a main window. The problem does not occur on HPUX or Linux. The following simple example code illustrates the problem and the work around I've come up with; However, I'd like, very much, to get rid of the kludgy work around....
11
3753
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 (ClassB* b; b->func(); ) the base-class function is called instead of the new function in the derived class. All other similar functions (virtual in the base class and overwritten in the the derived class) work fine, it's just this one function. ...
0
2020
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 users So I designed the project as master Node that has all administration
117
7204
by: Peter Olcott | last post by:
www.halting-problem.com
28
5213
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(); .... and then call the virtual method, why is it that the base class's method is called instead of the overridden method? How do I fix this if I don't know at runtime what the child class is? I'm using Activator.CreateInstance() to load the...
6
3803
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 is more that 1249 bytes, then the progress bar of the browser will move too slow and then displaying that the page not found!!!! If the message is less than or equal to 1249 then no problem.
16
4912
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 Microsoft must be installed on their servers. Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses. This can be easily verified: Implement the following in a web service method (just raises a SOAPException with a...
2
4550
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 set to...it is set to false. Can someone show me what I am doing wrong and tell me the correct way? Thank you. In the page load event, I am doing the following:
0
727
by: urquhart.nak | last post by:
crack cream
0
8647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9132
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9004
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7682
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3024
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2288
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1986
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.