| | |
help me understand end of do while loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 27
Reputation:
Solved Threads: 0
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)
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)
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <conio.h> #include <iomanip.h> int main() { float A=7.98,B=3.22,C=5.59,D=8.71,E=2.33; //prices of products char ch; //identity of product int sold; //no. of sold product float sale=0; //total sale do{ cout<<"\n Enter product code( a,b,c,d,e ),followed by quantity of product sold"<<endl; cout<<" Or press Enter twice for total"<<endl; cin>>ch>>sold; switch (ch) { case 'a': sale=+A*sold; break; case 'b': sale=+B*sold; break; case 'c': sale=+C*sold; break; case 'd': sale=+D*sold; break; case 'e': sale=+E*sold; break; default: cout<<"Invalid product"<<endl; } }while ( ch != '\r' || sold!='\r'); cout<<"\n Total sales equal"<<sale; getch(); return 0; }
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
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
>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.
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.
>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.
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.
•
•
Join Date: Nov 2004
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
>//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.
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.
![]() |
Similar Threads
- Cannot end my loop properly (IT Professionals' Lounge)
- End a for loop (C++)
Other Threads in the C++ Forum
- Previous Thread: arranging 4 for loop * triangles in line
- Next Thread: program correction
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






