I want to check if the user's input is a INT type. When I ask them to input a value for A (INT ONLY), what can I do in my IF statement to make sure it is INT and if it isn't how do I catch it? Also if the user inputs a wrong value I want the program to ask them again and re-take the value for A.
My code is working fine, it's just I want to try out every possibility just in case so I can learn more.
Here is the IOAA (Input, Output, Analysis, Algorithm) my professor makes us write.
// Input:
Variable Name | Data Type
A | int
B | int
C | int
discriminant | double
Xpos | double
Xneg | double
Imaginary | double
ImaginaryB4 | double
Imaginary AF | double
// Output:
1. Inform user what the program is doing
2. Ask user the value of A (integer only)
3. Ask user the value of B (integer only)
4. Ask user the value of C (integer only)
5. Print one of the following
1) Two real solutions
2) One real solution
3) Two imaginary solutions
4) No solution possible
6. Goodbye
// Algorithm:
1. Include all necessary #includes (header files)
2. Inform user what the program is doing
3. Declare any constants
4. Declare any formatting flags for floating point or other data
5. Declare all your variables
6. Ask user to input values (int only) for A, B, and C
7. Write an "if" statement for A to make sure A != 0
8. Print out solution(s)
9. Use if/else statements for printing output for
1) Two real solutions
2) One real solution
3) Two imaginary solutions
4) No solution possible
9. Goodbye
int a;
cout << "Enter an integer value between 1 and 20: ";
cin >> a; // yeah sorry completely forgot this first time around lol
while(!a || a > 20)
{
cout << a << " is not a valid value!" << endl;
cout << "Please enter an integer value between 1 and 20: ";
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); // flush chars out of cin buffer
cin.clear(); // reset cin error flags
cin >> a;
}
No idea what Texan40 is wanting you to do up there.. Instead, try this:
1 2 3 4 5 6 7 8 9 10 11 12 13
cout << "Enter an integer value between 1 and 20: "; //Prompt
cin >> a; //Get input
//While the input is NOT an integer, OR is > 20. go into the loop body (i.e. the algorithm), otherwise proceed.
while(!cin || a > 20)
{
cout << a << " is not a valid value!" << endl;
cout << "Please enter an integer value between 1 and 20: ";
cin.clear(); //Clear the buffer of the BAD input
cin.ignore(256,'\n'); //^
cin >> a;
}