int main ()
{
int choice;
cout << "This program translates integers with magnitudes less than a billion into words" << "\n";
cout << "Enter 0 to terminate" << "\n";
cout << "\n";
do
{
cout << "Enter a number: ";
cin >> choice; //how can i check if the user just hit enter?
cout << choice << "\n";
if (choice != 0)
{
Numbers object1(choice);
}
} while (choice != 0);
}
Hmm i tried the cin.get function but the thing is, it is not forcing cin to stop waiting for an input. Is there a function that forces cin to stop reading even though there is no input?
To get the "Input failed" output, you can press Ctrl+Z and then Enter in a Windows console - this terminates program input (e.g. you can never, ever read input ever again).
Yes but i thought std::getline only works with char or string type variables, if i use an int with getline it wont seem to work.
I even tried using std::stringstream to later convert a string to an int but there seems to be bugs when i pass the converted int variable to a member function
#include <iostream>
int main()
{
constchar newline = '\n' ;
std::cout << "enter an integer (or just enter to quit): " ;
int a ;
if( std::cin.peek() != newline && std::cin >> a )
{
std::cin.ignore( 1000000, '\n' ) ; // extract and throw away the new line
std::cout << "enter another integer (or just enter to quit): " ;
int b ;
if( std::cin.peek() != newline && std::cin >> b )
std::cout << a << " + " << b << " == " << a+b << '\n' ;
}
}