here's a nice safe way to get input:
Code:
#include <iostream> //for console I/O
#include <cstdlib> //for atoi()
#include <string> //for strings
int main()
{
int num; //holds the number
std::string line; //holds the user input
std::cout<<"Enter some stuff (Type 'exit' to exit the program)\n";
for(;;)
{
std::cout<<"> ";
getline(std::cin,line,'\n'); //take in input as a string
if(line=="exit") //if the command to exit is given
{
break; //follow it
}
num=atoi(line.c_str()); //if not, convert the string to an int
std::cout<<num<<std::endl; //and do something with it
}
return 0;
}
notice that doesn't do anything to validate that input... so if they enter their name, it's going to return 0, and if they enter 99999999999999999999999999, it's going to return 2147483647. Just look at it this way: It's better than watching your program launch itself into an infinite loop.