A
arnuld
WANTED: A simple program to print out the number user entered:
PROBLEM: does not handle the invalid input
#include <iostream>
int main()
{
int i;
std::cout << "Please enter a number: ";
std::cin >> i;
std::cout << "you entered: "
<< i
<< std::endl;
return 0;
}
================= OUTPUT =====================
[arnuld@dune C++]$ g++4 -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@dune C++]$ ./a.out
Please enter a number: 345
you entered: 345
[arnuld@dune C++]$ ./a.out
Please enter a number: w
you entered: 134514646
[arnuld@dune C++]$ ./a.out
Please enter a number: 1222222222222222222222222222222222
you entered: 134514646
[arnuld@dune C++]$
Now can I make this program to handle these invalid inputs, so that it
prints the message that you did not enter a number or to ask the user to
enter a small number ?
PROBLEM: does not handle the invalid input
#include <iostream>
int main()
{
int i;
std::cout << "Please enter a number: ";
std::cin >> i;
std::cout << "you entered: "
<< i
<< std::endl;
return 0;
}
================= OUTPUT =====================
[arnuld@dune C++]$ g++4 -ansi -pedantic -Wall -Wextra test.cpp
[arnuld@dune C++]$ ./a.out
Please enter a number: 345
you entered: 345
[arnuld@dune C++]$ ./a.out
Please enter a number: w
you entered: 134514646
[arnuld@dune C++]$ ./a.out
Please enter a number: 1222222222222222222222222222222222
you entered: 134514646
[arnuld@dune C++]$
Now can I make this program to handle these invalid inputs, so that it
prints the message that you did not enter a number or to ask the user to
enter a small number ?