How to check if input is a INT type?

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

==================================================

// Analysis:
-Discriminant = B^2 - 4AC
discriminant = (B*B)-(4*A*C);
-Addition X value = (-B+(B^2-4AC)^0.5)/2A
Xplus = (-B + (sqrt(discriminant))) / (2 * A);
-Subraction X value = (-B-(B^2-4AC)^0.5)/2A
Xsub = (-B - (sqrt(discriminant))) / (2 * A);
-Imaginary Value before +/- sign
ImaginaryB4 = (-double(1) * B) / (2 * A);
-Imaginary Value before +/- sign
ImaginaryAF = (sqrt((-1)*discriminant)) / (2 * A);

==================================================

// 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

==================================================
*/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

// Prototypes
void Print();
void EndPrint();

int main()
{
	int A, B, C ;
	double discriminant, Xplus, Xsub, ImaginaryB4, ImaginaryAF;
	char choice;

	// Input
	Print();
	do
	{
		cout << fixed << showpoint << scientific << setprecision(4);
		cout << "\nEnter a value for A (int only): " << flush;
		cin >> A;
		if (A == 0.0)
		{
			cout << "No Solution is possible for leading coefficient with zero value.\n\n";
			EndPrint();
			return 0;
		}
		else
		{
			if (A > 0.0 || A < 0.0)
			{
				cout << "Enter a value for B (int only): " << flush;
				cin >> B;
				cout << "Enter a value for C (int only): " << flush;
				cin >> C;
			}
			
			// Analysis
			discriminant = (B*B) - (4 * A*C);
			Xplus = (-B + (sqrt(discriminant))) / (2 * A);
			Xsub = (-B - (sqrt(discriminant))) / (2 * A);
			ImaginaryB4 = (-double(1) * B) / (2 * A);
			ImaginaryAF = (sqrt((-1)*discriminant)) / (2 * A);

			// Output
			if (discriminant > 0.0)
			{
				cout << "The two real solutions are" << setw(6) << "" << "X=" << Xplus
					<< "\n" << setw(23) << "" << "And" << setw(8) << "X=" << Xsub << "\n\n";

			}
			else if (discriminant == 0.0)
			{
				cout << "The one real solution is" << setw(8) << "" << "X=" << Xplus << "\n\n";
			}
			else if (discriminant < 0.0)
			{
				cout << "The two imaginary solutions are" << setw(6) << "" << "X=" << ImaginaryB4 << " + (" << ImaginaryAF << ")*I"
					<< "\n" << setw(28) << "" << "And" << setw(6) << "" << "X=" << ImaginaryB4 << " - (" << ImaginaryAF << ")*I\n\n";
			}
		}
			cout << "Continue program? (Y/N): " << flush;
			cin >> choice;	
	} while (choice == 'Y' || choice == 'y');
	EndPrint();
	cin.get();
	return 0;
}

// Constructors
void Print()
{
	cout << setfill('*') << setw(80) << "" << setfill(' ')
		<< "\t\tEL CAMINO QUADRATIC EQUATION SOLVER\n"
		<< setfill('*') << setw(80) << "" << setfill(' ')
		<< "This program will provide solutions for an equation of the form"
		<< "\n\t\t\t Ax^2 + Bx + C = 0,"
		<< "\nWhere A, B, and C are integers, and A is not equal to zero.\n\n"
		<< setfill('*') << setw(80) << "" << setfill(' ');
}

void EndPrint()
{
	cout << setfill('*') << setw(80) << "" << setfill(' ')
		<< "Thanks for using El Camino Quadratic Equation Solver.\n"
		<< setfill('*') << setw(80) << "" << setfill(' ') << "\n";
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14

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;
  }

Last edited on
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;
}
Last edited on
Topic archived. No new replies allowed.