Check if x is not an integer

I need my program to shut down if x does not equal a number but i cant figure out how to check for it.
What is the data type of "x"?
A number. I am declaring it with int x = 0;
closed account (z05DSL3A)
Can you explain what you are trying to do a bit more clearly?

at the moment I'm just thinking if (x != a_number) exit(1);
If x is an int it is always a number, so I am not really sure what exactly you are trying to check here.
I have a simple code which lets you input two numbers and it finds the sum. I want to stop people entering a letter when they are suppose to enter a number, so i want it to shut down if a letter is entered. I tried checking if x is not 0 through to 9, but if i inputted a two digit number, it would also shutdown. Here is the code.

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
#include <iostream>
#include <conio.h>


int main()
{
	char vari = 0;
	do
	{
		int x = 0;
		int y = 0;
		system("cls");
		std::cout << "Choose a number: ";
		std::cin >> x;
		system("cls");
		std::cout << "Choose a second number: ";
		std::cin >> y;
		system("cls");
		int sum = x + y;
		std::cout << "The sum of " << x << " and " << y << " is " << sum << std::endl << std::endl;
		std::cout << "Press ENTER to restart or ESC to exit.";
		
		if ( vari == 27 )
		{
			return 0;
		}
		
		std::cin.clear();
		std::cin.ignore(255, '\n');

		vari = _getch();
	}
	while( vari == 13 );
	return 0;
}
Relatively simple - cin>> fails if you enter a letter when an integer is expected. So you just have to check if cin.fail() is true after cin>>.
Thankyou it worked perfectly.
Here's a simple template to check the data type of your input:

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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

template< typename T >
T grab(const char * prompt);

int main(int argc, char** argv)
{
    int x = grab<int>( "Enter an integer: " );

    cout << "You entered: " << x << endl;

    return 0;
}

template< typename T>
T grab(const char * prompt)
{
    T thing;
    std::istream::iostate old_state = cin.exceptions();
    cin.exceptions(std::istream::failbit);
    while(true)
    {
        try {
        cout << prompt;
        cout.flush();
        cin >> thing;
        cin.exceptions(old_state);

        return thing;
        } catch(std::istream::failure &e) {
        cout << "Input failure. Please try again." << endl;
        cin.clear();
        cin.ignore(1024, '\n');
        }
    }
    return thing;
}
Last edited on
closed account (z05DSL3A)
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
#include <iostream>
#include <conio.h>
#include <string>
#include <sstream>

int GetNumber(std::string prompt = "Please enter a valid number: ")
{
    std::string input;

    int number = 0;

    while (true) 
    {
        std::cout << prompt;
        getline(std::cin, input);

        std::stringstream ss(input);
        if (ss >> number)
            break;
        std::cout << "Invalid number, please try again" << std::endl;
    }
    return number;
}

int main()
{
	char vari = 0;
	do
	{
		int x = 0;
		int y = 0;
		
		x = GetNumber();
		y = GetNumber("Please enter a second number: ");
		
		int sum = x + y;
		std::cout << "The sum of " << x << " and " << y << " is " << sum << std::endl << std::endl;
		std::cout << "Press ENTER to restart or ESC to exit.";
		
		if ( vari == 27 )
		{
			return 0;
		}
		
		std::cin.clear();
		std::cin.ignore(255, '\n');

		vari = _getch();
	}
	while( vari == 13 );
	return 0;
}
Grey Wolf, your method does not allow for other data types. My method allows for any data type. The only problem is that with strings, it will not act like getline() and will only get the first piece of your string.
it will not act like getline() and will only get the first piece of your string.

You can just provide an overload for string though.
closed account (z05DSL3A)
packetpirate wrote:
Grey Wolf, your method does not allow for other data types. My method allows for any data type.
So? I never made any claims about the code. However it is rather straight forward for a beginner to understand.

Edit:
How about:
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
#include <iostream>
#include <string>
#include <sstream>

template<typename Type>
bool GetInput(Type &destination, std::istream &stream = std::cin)
{
	std::string temporaryHolder;
	std::stringstream stringStream;
	
	std::getline(stream, temporaryHolder);
	stringStream << temporaryHolder;
	
	return (stringStream >> destination);
}

int main()
{
	
    int x = 0;
		
    std::cout << "Enter an integer: ";
    while (!GetInput(x)) 
    {
        std::cout << "Input failure. Please try again. ";
    };
    
    std::cout << "You entered: " << x << std::endl;
        
	return 0;
}

Last edited on
Topic archived. No new replies allowed.