I tried this and it is not working since input is an integer and 'x' is a char
how can i make this work. I want to make the loop exit when the user presses x,
#include<iostream>
int main()
{
int* numbers = newint [size];
int input;
int i=0;
std::cout<<"Enter some integers or x to exit;
std::cin>>input;
while(input != 'x')
{
numbers[i]=input;
i++;
std::cin>>input;
}
}
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.
yeah I know but I was wondering if there is a way. because i have a project where the teacher made the same project and it exits with 'x' can't figure out how he did it.
#include <vector>
#include <iostream>
int main()
{
// std::vector resizes itself if we input more objects that it can contain.
// Something that plain arrays can't do on their own
std::vector<int> numbers;
int input;
// ios::good() checks if a stream is able to operate properly.
while(std::cin.good())
{
// 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.
std::cin.clear();
// Read the non-number that was entered.
char c;
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;
break;
}
// 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.
continue;
}
// If all is well, we store the input.
numbers.push_back(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;
return 0;
}
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".
#include <iostream>
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" ;
}