help me understand end of do while loop

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 27
Reputation: Der_sed is an unknown quantity at this point 
Solved Threads: 0
Der_sed Der_sed is offline Offline
Light Poster

help me understand end of do while loop

 
0
  #1
Nov 17th, 2004
OK PEOPLE I NEED HELP AGAIN........pls dont do my homework..........thats y i havnt given the question this time aroung


the question im asking is simple :

the program asks user to input product id ( a,b,c,d or e) and its quantity sold......it then saves total sale in "int sale"

but y doesnt the "do while" loop terminate upon entering "\r" (enter)







  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <iomanip.h>
  4. int main()
  5. {
  6. float A=7.98,B=3.22,C=5.59,D=8.71,E=2.33; //prices of products
  7. char ch; //identity of product
  8. int sold; //no. of sold product
  9. float sale=0; //total sale
  10.  
  11. do{
  12. cout<<"\n Enter product code( a,b,c,d,e ),followed by quantity of product sold"<<endl;
  13. cout<<" Or press Enter twice for total"<<endl;
  14.  
  15. cin>>ch>>sold;
  16.  
  17. switch (ch)
  18. {
  19. case 'a':
  20. sale=+A*sold;
  21. break;
  22.  
  23. case 'b':
  24. sale=+B*sold;
  25. break;
  26.  
  27. case 'c':
  28. sale=+C*sold;
  29. break;
  30.  
  31. case 'd':
  32. sale=+D*sold;
  33. break;
  34.  
  35. case 'e':
  36. sale=+E*sold;
  37. break;
  38.  
  39. default:
  40. cout<<"Invalid product"<<endl;
  41.  
  42. }
  43. }while ( ch != '\r' || sold!='\r');
  44.  
  45. cout<<"\n Total sales equal"<<sale;
  46.  
  47. getch();
  48. return 0;
  49. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: help me understand end of do while loop

 
0
  #2
Nov 17th, 2004
because you need to enter 2 newlines before the condition is false.

when you enter just 1, ch will have been read but the program is still waiting for input to the sold parameter.
Only when that's filled as well will loop processing commence.
When at the end of the loop both are found to contain \r the loop will terminate.
When there's only 1 \r, one of the conditions of the while will be false and the other true.
true || false == true as is false || true.
Only false || false results in false.

Run the program in a debugger if you wish to confirm this
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: help me understand end of do while loop

 
0
  #3
Nov 17th, 2004
>but y doesnt the "do while" loop terminate upon entering "\r" (enter)
cin's >> operator terminates on whitespace. \r and \n are whitespace, so ch and sold will never have those values, the program will simply sit there waiting for input until it gets something it likes.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 27
Reputation: Der_sed is an unknown quantity at this point 
Solved Threads: 0
Der_sed Der_sed is offline Offline
Light Poster

Re: help me understand end of do while loop

 
0
  #4
Nov 17th, 2004
this doesnt help either;

do{
.
.
.
cout<<" Or input X and X for total"<<endl;
.
.
.
.
.
while ( ch != 'X' || sold!='X');
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: help me understand end of do while loop

 
0
  #5
Nov 17th, 2004
>this doesnt help either
ch is a char, but sold isn't. cin doesn't know that they're technically interchangeable and X will fail miserably when read by cin for sold. I think your problem is that you don't have a good idea of what the user interface should be, so you end up floundering about for anything that works, no matter how silly, and you make mistakes.

Humor me and try this: Figure out exactly how you want the user interface to work to the point where you can describe it precisely, with examples. Then we'll go from there. I can guarantee that with the proper design foundation, the program will be much better.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 1
Reputation: thehap is an unknown quantity at this point 
Solved Threads: 0
thehap thehap is offline Offline
Newbie Poster

Re: help me understand end of do while loop

 
0
  #6
Nov 17th, 2004
I think you understand do...while loops. However, your problem lies in your use of C++ console input.

The getch(char ch) function always skips whitespaces. This includes <space>, <tab>, and <newline>, which, unfortunately, you are relying on to terminate your input loop.

The '\r' character, along with ' ', '\n', '\t', etc., will never be assigned to your "ch" or "sold" variables, so your test will never quit.

You do have options, though:

1. Use a function that does not skip whitespace, such as get.ch(char ch) from the <iostream> library. There is a caveat, however; you will now be capturing whitespace, and you must manually skip over spaces and such from the user.

2. Use a sentinel value in your loop. This means you use a dummy value, such as 'q' for quit, to indicate that the user wants to stop input.

I feel for you; C++ console input is HARD! But, you can do it, even if you don't use one of these suggestions. Just think about what you want the user to input, and design your code accordingly, just like Narue said to do.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 27
Reputation: Der_sed is an unknown quantity at this point 
Solved Threads: 0
Der_sed Der_sed is offline Offline
Light Poster

Re: help me understand end of do while loop

 
0
  #7
Nov 17th, 2004
THANX ALOT PPL!!!!!!!!!!................


......narue for poiting out that X cannot be stored in float :o


.....thehap for suggesting valuable alternatives!!!! :cheesy:
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4
Reputation: Rizwan Yasin is an unknown quantity at this point 
Solved Threads: 0
Rizwan Yasin Rizwan Yasin is offline Offline
Newbie Poster

Re: help me understand end of do while loop

 
0
  #8
Nov 18th, 2004
Dear do this

do{
...
...
cin>>ch;
cin>>sold;
...
...
}while(ch!='\r' && (char)sold!='\r');
//sold is an integer typecast as character here then u will not got any warning.
//ok try this one
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: help me understand end of do while loop

 
0
  #9
Nov 18th, 2004
>//sold is an integer typecast as character here then u will not got any warning.
You have no idea what you're talking about, do you?

>//ok try this one
Try it if you want, it won't work, as I explained in detail above. If you had bothered to read it (or even try your own suggestion) then you wouldn't have been stupid enough to suggest something so blatantly incorrect.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,982
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 925
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: help me understand end of do while loop

 
0
  #10
Nov 18th, 2004
You will not be adding up your total with something like =+ the correct operator is +=

Unless you have to use double return key, I would go with something as simple as t1 and flag down the 't' character to exit the loop.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC