detecting data type the user inputs and closing console within the code

am still new to c++ and am trying to make a code that prompts the user to enter a number from 1-4, 5 to exit the program

my first question is: how can i detect data type from the user input?

say the user inputs char instead of an int i want the default statement(from switch) to be displayed . my problem is ... it's displayed only if the user inputs the char the first time , after that it just keeps whatever appeared before it!

my second question is:is there a way to display the the default statement(from switch) if the user doesnt intput anything and just hits enter?


here is my 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
	
cout<<"Please enter a number from 1-4, 5 to exit"<<endl;
int choice;
cin>>choice;
while(choice!=5)
	{
		system("cls");
		cin.sync();
		cin.clear();


		switch(choice)
		{
		case 1:
			cout<<"1\n";
			break;
		case 2:
			cout<<"2\n";
			break;
		case 3:
			cout<<"3\n";
			break;
		case 4:
			cout<<"4\n";
			break;
		default:
			cout<<"invalid\n";
		}
		cout<<"Please enter a number from 1-4, 5 to exit"<<endl;
		cin>>choice;
	}


am sorry for the long question but i looked everywhere and didnt find anything helpful!!
i really appreciate any help i can get
THANK YOU ^_^
Last edited on
my first question is: how can i detect data type from the user input?


You can get the user input into a string and parse the string to evaluate the type of input the user has given you, but that seems like it might be a little more work than you want to do.

my second question is:is there a way to display the the default statement(from switch) if the user doesnt intput anything and just hits enter?


Yes there is. You should check out http://www.parashift.com/c++-faq/input-output.html

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
#include <iostream>
#include <string>
using namespace std ;

int main()
{
    const std::string prompt = "Please enter a number for 1-4, 5 to exit\n" ;
    int choice ;

    do
    {
        std::cout << prompt ;
        while ( !(std::cin >> choice) || (choice < 1 || choice > 5) )
        {
            std::cin.clear() ;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') ;

            std::cout << "Invalid input!\n\n" << prompt ;
        }

        if ( choice != 5 )
            std::cout << "Your choice was " << choice << '\n' ;

    } while ( choice != 5 ) ;
}


cin.sync() may work like the cin.ignore line on some systems, but there is nothing in the standard that says that it should, and on some systems it does absolutely nothing. On systems where it does something, you still need to clear the error state of the stream before you can expect your stream manipulations to be successful.
when i tried this code it there was an error "numeric_limits is not a member of std"
if i user cin.ignore() would it be the same as the one u tried??

also if i wanted to use string then parse it to evaluate type of input, how can i do that?!!!!!
Last edited on
#include <limits>
Topic archived. No new replies allowed.