How do i delete invalid input until correct input is entered

Program is to add two fractions together like so..


This program adds fractions. ‘Y’ continues, any other key exits program
===================================================
Enter numerator 1 ==> 1
Enter denominator 1 ==> 3
Enter numerator 2 ==> 1
Enter denominator 2 ==> 6
 1     1     1
--- + --- = ---
 3     6     2
-----------------------------------------------------
Continue? Y or N! ==> n


My question is that i need each input to be a number.
if a character is entered i want it to output something like..



This program adds fractions. ‘Y’ continues, any other key exits program
===================================================
Enter numerator 1 ==> 1
Enter denominator 1 ==> a
You need to enter a number here.
Press any key to continue.


and then clear the part where the letter was entered.



This program adds fractions. ‘Y’ continues, any other key exits program
===================================================
Enter numerator 1 ==> 1
Enter denominator 1 ==> 

Last edited on
To clear it you just need to use
 
x.clear();

To check if it is a letter I believe you can use
1
2
3
4
5
6
#include <locale> //need this to make it work

  if (!isdigit(x))
{
cout<< "You need to enter a number here";
}


That would make an infinite loop... and does not make sense
Last edited on
1
2
3
4
5
6
7
8
9
10
11
	cout << "This program adds fractions. 'Y' continues, any other key exits program\n";
	cout << "=======================================================================\n";

	cout << "Enter " << setw(18) << "numerator 1 ==> ";
        cin >> Num1;
		while (isalpha(Num1))
		{	
			cout << "you need to enter a number here.";
			cin.clear();
			cin >> Num1;
		}
i still need help! when i enter a letter for Num1 i get "Debug Assertion Failed" unless i got through the whole program once and then when i enter a letter for Num1 the second time around it works? however when i enter a letter for Den1 i get everything erased up to enter Num1 again. how can i just delete the cin for Den1, Num2, Den2??


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
	cout << "This program adds fractions. 'Y' continues, any other key exits program\n";
	cout << "=======================================================================\n";

	cout << "Enter " << setw(18) << "numerator 1 ==> ";
        cin >> Num1;
		while (isalpha(Num1))
		{	
			cin.clear(Num1);
			cin >> Num1;
		}

	cout << "Enter denominator 1 ==> ";
        cin >> Den1;
		while (isalpha(Den1))
		{	
			cin.clear(Den1);
			cin >> Den1;
		}
    cout << endl;

	cout << "Enter " << setw(18) << "numerator 2 ==> ";
        cin >> Num2;
		while (isalpha(Num2))
		{	
			cin.clear(Num2);
			cin >> Num2;
		}

	cout << "Enter denominator 2 ==> ";
	   cin >> Den2;
		while (isalpha(Den2))
		{	
			cin.clear(Den2);
			cin >> Den2;
		}
	   cout << endl;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{

	cout << "Please enter a number: " << flush;
	double num;
	while (!(cin >> num))
	{
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << "Invalid input" << endl;
		cout << "Please enter a number: " << flush;
	}
	cin.ignore(INT_MAX, '\n');


	cout << "You entered the number " << num << endl;

	cin.ignore(INT_MAX, '\n');
	return 0;

}
Last edited on
Topic archived. No new replies allowed.