Input Validation in C++

I am new in C++ and I have an assignment to make a calculator. My teacher wants to have a validation every time that the user entered wrong. For example, it asks for a number and when the user enters a number it should re-prompt the user to enter again until correct. But I have this problem that when I enter a number and there is a letter on it, it does not validate right. Example inputs: 234ea1234, 4e14, ew2314. Can someone help me on how to make the correct code on how to do this. How can I re-prompt to ask the user again for every wrong answer.

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
#include<iostream>
using namespace std;
class Numbers{
	public:
		int num1, num2;
		float result;
};
int main(){
	Numbers input;
	int *ptr1 = &input.num1;
	int *ptr2 = &input.num2;
	float *ptr3 = &input.result;
	int r = 0;
	char answer, op;
	char *ptr4 = &op;
	
 

 
	cout<<" =CHOOSE THE OPERATION TO BE USED=\n"
		<<"  |------------------------------|\n"
		<<"  |   +   --- Addition           |\n"
		<<"  |   -   --- Subtraction        |\n"
		<<"  |   *   --- Multiplication     |\n"
		<<"  |   /   --- Division           |\n"
		<<"  |   %   --- Remainder          |\n"
		<<"  |   ^   --- Square             |\n"
		<<"  |   #   --- Square Root        |\n"
		<<"  |------------------------------|\n\n";  
	cout<<"Answer: ";
	cin>>*(ptr4+r);

	
	cout<<"Enter First Number: ";
	cin>>*(ptr1+r);
	if(cin.fail()){
		cout<<"That is not a number\n";
	
	}
	cout<<"Enter First Number: ";
	cin>>*(ptr2+r);
	if(cin.fail()){
		cout<<"That is not a number\n";
		
	}
}
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
#include <iostream>
#include <cctype>

int get_int( const char* prompt )
{
    if(prompt) std::cout << prompt ;
    std::cout << ": " ;
    int number ;

    // if the user entered a valid integer and the next char entered is a white space
    // https://en.cppreference.com/w/cpp/io/basic_istream/peek
    // https://en.cppreference.com/w/cpp/string/byte/isspace
    if( std::cin >> number && std::isspace( std::cin.peek() ) )
        return number ; // return the integer

    // if we reach here, the return was not executed; input failed
    // handle the error and retry

    // https://en.cppreference.com/w/cpp/io/basic_ios/clear
    std::cin.clear() ; // clear a possible failed state of stdin

    // extract and discard the bad input line
    // https://en.cppreference.com/w/cpp/io/basic_istream/ignore
    std::cin.ignore( 1000, '\n' ) ;

    // inform the user and try again
    std::cout << "bad input. try again\n" ;
    return get_int(prompt) ;
}

int main()
{
    const int a = get_int( "enter an integer" ) ;
    const int b = get_int( "enter another integer" ) ;

    if( b != 0 ) std::cout << a << " / " << b << " == " << double(a) / b << '\n' ;
    else std::cout << "division by zern" ;
}
Topic archived. No new replies allowed.