int* numbers = newint [size];
std::cout<<"Enter some integers or x to exit;
while(input != 'x')
One thing to point out is something you already said yourself:
input is an integer and 'x' is a char
This won't work because the condition of the while statement will keep looking for something that can never be true in this integer/character case, thus the loop keeps going on. Another way to look at it is the condition is looking for an apple when it can only see oranges, so to speak.
If you want the user to use numbers and then be able to exit a loop, then another number that is usually not used (such as -1) can suffice in this instance.
// std::vector resizes itself if we input more objects that it can contain.
// Something that plain arrays can't do on their own
// ios::good() checks if a stream is able to operate properly.
// istream::operator>> returns the stream itself (cin).
// The stream can be used as a bool to check if the last input operation
// succeeded. The operation fails if the input can't be interpreted as a
// value of the type of the variable ('x' can't be read as a number so it fails).
if( !(std::cin >> input) )
// Reset cin to a good state to make it able to read again.
// Read the non-number that was entered.
std::cin >> c;
// tolower() converts a character to lowercase, so 'X' can be used
// to exit the loop too.
if(std::tolower(c) == 'x')
// Inform that the program is continuing properly.
std::cout << c << " found. Quitting." << std::endl;
// Inform of the error.
std::cout << "Invalid input: " << c << std::endl;
// If c was garbage we want to continue reading
// but we don't want to get to the point where the invalid
// input is stored (that is, vector::push_back()).
// So we restart the loop.
// If all is well, we store the input.
// Print back the input sequence to see if it correct.
std::cout << std::endl << "Valid numbers entered:" << std::endl;
for(int i = 0; i < numbers.size(); ++i)
std::cout << numbers[i] << ' ';
std::cout << std::endl;
It has two "bugs":
1) It doesn't read strings. If you input "roxanne" it will read and discard 'r' and 'o', then read 'x' and terminate.
2) It doesn't work with decimal numbers. If you write "12.34" it reads "12", then '.' as a character and discards it, then "34".
int main ()
std::cout << "enter the single character 'x' to quit the program\n" ;
while( std::cout << "number (or x)? " && std::cin.peek() != 'x' )
int n ;
if( std::cin >> n ) std::cout << "you entered " << n << '\n' ;
else std::cout << "you did not enter a number\n" ;
std::cin.clear() ; // clear the error state if any
std::cin.ignore( 1000, '\n' ) ; // throw away everything else on this line
std::cout << "you entered an 'x': quitting\n" ;