inputing char into a int variable c++

how can i make an error message if the user accidentally input a char into a int variable??
1
2
3
4
5
6
7
int main()
{
  int a;
  cin >> a;
  if (cin.fail())
    cout << "Error message" << endl;
}
the loop is continious.. why?!

#include <iostream>
using namespace std;


int main()
{
int a;
do{
cin >> a;
if (cin.fail())
cout << "Error message";
} while(cin.fail());
}
while(cin.fail()); This means the loop will run as long as the failure flag is set.
You probably meant while(!cin.fail());


You have to clear the input stream like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;


int main()
{
	int a = 0;
	bool bad = false;

	do{
		cout << "Enter number: ";
		cin >> a;

		bad = cin.fail();
		if (bad)
			cout << "Error message" << endl;
		cin.clear();
		cin.ignore(10, '\n');
	} while(bad);
}
and could possibly be inputing a integer and character?

like i have choices
1-me
2-you
c-cancel

input: <your input>

you input should be declared as what?
it is uncommon to have that kind of input, usually we'll use:

1. yes
2. no
0. cancel


but if you insist, then:

1
2
3
4
5
6
7
8
9
int choice = 0;

cout << "1. me" << endl << "2. you" << endl << "C. cancel" << endl << "your input: ";

if (!cin >> choice) {
   cout << "exit...";
   cin.get();
   return 0;
}
isnt it possible if it is a char??
oh yeah right, i just know that you can submit an integer value to a char and still considered as a character...

 
char choice = '1';


but the following code doesn't work, can somebody explain this?

1
2
3
4
5
6
char choice;

cout << "enter: ";
cin >> choice //enter 49 (ascii code for '1')
if (choice == '1')
   //doesn't execute 
Last edited on
could somebody please explain this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;


int main()
{
  char choice;

cout << "enter: ";
cin >> choice; //enter 49 (ascii code for '1')
  if (choice == '1')
  {
    cout << "You entered the number one";
    
  }
return 0;

}

Totally works.
A simple Loop will help u in this case!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
    int no=0;
    
    while(true)
    {
        cout<<"\nEnter any Integer No. ";
        cin>>no;
        if(cin.good())
        {
            break;
        }
        else
        {
            cin.clear();
            cout<<"\nInvalid Output!";
        }
    }
   
    return 0;
}
@moschops: it doesn't work... i just tested it...

enter: 49
//the program exits

@suh: it doesn't seem related with my question...
but the following code doesn't work, can somebody explain this?


1
2
3
4
5
6
char choice;

cout << "enter: ";
cin >> choice //enter 49 (ascii code for '1')
if (choice == '1')
   //doesn't execute  


It doesn't work because you're using a char for input, and 49 is two chars.

1
2
3
4
5
6
7
8
int input ;

std::cout << "Enter numbers: " ;
while ( std::cin >> input )
{
     char as_char = input ;
     std::cout << input << " is " << as_char << '\n' ;
}
Last edited on
that's my confusion, i don't know will it be translated into a char (49 converted to ascii and read as a single char) or it just take the first digit... thanks for the explanation... my bad... i should've give a simple test at the first place...
Last edited on
If it's being input to a char, then you'll get the ascii code of the first character that you enter.

If it's being input to a string, then you'll get the ascii codes of each character that you enter

If it's being input to an int, or short, or long, or float, or double, then you'll get the value represented by a set of digits (including the period when applicable)
Last edited on
Topic archived. No new replies allowed.