|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rythmicillusion (5) | |
|
I'm trying to write a little program that allows you to enter a couple integer values, print them to the screen, and continue indefinitely. To exit the program, I'd just like to be able to press a specific character ("x", for example) and hit enter. Here is my code thus far... *** int main() { int a=0; int b=0; cout<<"Please enter two integer values. To exit, press 'x', then press enter.\n"; while (a>=0, b>=0){ cin>>a>>b; cout<<a<<"\t"<<b<<"\n"; if (a="x") break;} keep_window_open();} *** Currently, I am allowed to enter one set of the two integer values and have them print. But after they print, the "Please enter a character to exit" message comes up. I am using Visual C++ 2008 Express on a Windows XP machine. Thanks in advance! | |
|
|
|
| jsmith (5804) | |||||
Does not do what you think, and besides, it isn't even what you want.
is triply wrong, first because it is an assignment not a comparison, second because it is assigning (or at least attempting to compare) an integer to a const char* (C string), and thirdly because an integer can never hold a non-integral value. | |||||
|
|
|||||
| joeriMJ (44) | |
|
a while loop is executed as long as its condition is true. So all you need to do is make sure the condition isn't met. I don't know what the comma in your while condition means, but I usually use && or || depending on the situation. Could you tell me what it means? 2 things though, if(a="x") the = operator is an assignment operator not a comparison operator. Never use it in a conditional statement like this. If you were trying to compare a with "x", this would not be possible since a is declared as an integer and "x" is a const char * | |
|
|
|
| rythmicillusion (5) | |
|
Thanks you both for the input. I think I'm starting to see how to tweak the code. I am unfamiliar with how to reassign "a" from an integer to a character in the loop. joeriMJ: to answer your question, I believe that the comma in the while condition simply allows for more arguments to be added. I am *very* new to C++ and programming in general, so even talking about coding and calling everything by the proper label is still difficult. | |
|
|
|
| ragnamanga (9) | |||
you can try this code:
i'm not sure though but i have tried it many times. characters can also hold integers but with a very limited range. | |||
|
|
|||
| joeriMJ (44) | |
|
char can hold an integer, but only its integer value which you can find out using an ascii table. For example the character '0' (zero) has ascii-code 43, the character '1' has ascii-code 44. These may vary, since I don't know the table by heart :-) This can be very usefull but keep in mind that the other way around (integers holding chars) is never possible. Rythmicillusion: Do you know if seperating two conditions in a while using a comma will provide 'true' if both the conditions are met or if just one condition is met ? Still I think it's always best to use && or || That's my experience from other programming languages and I can't seem to find that comma seperator anywhere in my C++ books. | |
|
|
|
| Duoas (5977) | |||
I always prefer to avoid "signal" characters. When the user is done, make it easy. Here's something I wrote a while ago to demonstrate the point.
Again, this is a simple example. It could use a few improvements. But you can easily modify it to suit your needs. Hope this helps. | |||
|
|
|||