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.
Does not do what you think, and besides, it isn't even what you want.
if( a="x" )
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.
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 *
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.
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.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
usingnamespace std;
int main() {
vector<int> ints;
string line;
cout << "Please enter the elements of the array.\n"
<< "Press ENTER after each element.\n"
<< "Press ENTER on a blank line to stop."
<< endl;
while (true) {
getline( cin, line );
if (line.empty()) break;
stringstream ss( line );
int value;
ss >> value;
ints.push_back( value );
}
cout << "Thank you. The array you entered is: ";
for (unsigned i = 0; i < ints.size(); i++) cout << ints[ i ] << ' ';
cout << endl;
return 0;
}
Again, this is a simple example. It could use a few improvements. But you can easily modify it to suit your needs.