-
little problem
using the following code:
-------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
int num = 0;
cin >> num;
while (num < 1 || num > 170)
{
cout << "You entered an invalid number, try again:" << endl;
cin >> num;
}
cout << num;
return 0;
}
----------------------------------------------
everything works fine unless i input a character, which prints the cout message over and over again to the screen.
can someone help me with this please
-
You can't read a character into an integer variable.
The most common solution to this is to read theinput as a string, check whether it's numerical digits only, then convert it to an integer using atoi() or something.
-
i wouldnt have thought it a problem, because when i do this:
int num;
cin >> num;
cout << num;
and enter a character it just returns 0
-
The problem lies in the loop. cin << int tries to read an integer value from the instream. It encounters a character instead, thus leaving it in the keyboard buffer and moves on. In the next iteration it finds that character again and will go on doing this again and again and again and again... forever.
-
-
What I mentioned in my first reply.
-
A possible solution to your problem is to check what type of input the user gave before it is processed. A check can determine wether or not input is valid (ie is it an integer or invalid type, such as a character?).
Take this code for example:
Code:
while (cout << "Enter age: " && !(cin >> age))
{
cout << "\nInvalid input\n";
cin.clear();
cin.ignore();
}
This line prompts the user for their age. Should you enter an integer (which is the declaration of age), it will run correctly and store the number in age. If you enter something else, like a character, it will indicate invalid input, and prompt you again for an integer value.
I hope this helps,
-RoD
-
yeah i think that does what i want.
could you just run past me what exactly is happening though :)
-
My pleasure!
Code:
(cout << "Enter age: " && !(cin >> age))
The user is prompted to enter their age. After they do so and strike enter, its read into age just like any other time with the exception that its in a while statement. I'm not sure exactly how this works, but it checks to see if it is a integer. if not (!), it will enter the body of the while loop(while not an integer). At this time the user is told its invalid and prompted again. If the statement while not an integer returns false (if an int is enterd it will be false) it breaks from the loop and continues on through the program.
Hope that helps!
-RoD
-
-
Anytime, here is an example program from VC++ 6.0:
Output:
Code:
Enter age: h
Invalid input
Enter age: 6
An integer was recieved!
6
Press any key to continue
Code:
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int age;
while (cout << "Enter age: " && !(cin >> age))
{
cout << "\nInvalid input\n";
cin.clear();
cin.ignore();
}
cout << "An integer was recieved!" << endl;
cout << age << endl;
return 0;
}
-
is there a way to incorporate a range verification into that aswell?
so that it has to not only be an integer, but also, for example, between 1 and 100.
-
Of course! Just add an if statement:
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int age;
while (cout << "Enter age: " && !(cin >> age))
{
cout << "\nInvalid input\n";
cin.clear();
cin.ignore();
}
if (age >= 1 && age <= 100)
{
cout << "An integer was recieved!" << endl;
cout << age << endl;
}
else
{
cout << "Number not between 1-100" << endl;
}
return 0;
}
-RoD
-
but then the program will just end. is there no way to keep looping the "Enter age: " output while the input is either not an int or not between 1 and 100?
-
Yea we can do it that way:
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int age;
while (cout << "Enter age: " && !(cin >> age) || (age < 1 || age > 100))
{
cout << "\nInvalid input or out of range\n";
cin.clear();
cin.ignore();
}
cout << "An integer was recieved!" << endl;
cout << age << endl;
return 0;
}