Using cin>>

Hi,

My instructor is requiring us to read data into an int character using cin>>. However, he says we should be able to handle input such as
asdf
We are not allowed to read data into any other type. To my knowledge, reading in data of the improper type while using cin will just yield an infinite loop. Is there a way around this? Thanks.
I don't fully understand what you mean; but cin should be fine for taking in data.

You could have something like this:

1
2
3
4
5
int asdf;
cout << "Type in a number: ";
cin >> asdf

cout << "Your number was" << asdf;


The only way it could go wrong is if you did seomthing like this:

1
2
3
int asdf;
cout << "Type in a number: ";
cin >> adfgr


As obviously, adfgr hasn't been defined.

If this doesn't answer your question, could you please sue an example of what is going wrong.
Sorry here's an example:

1
2
3
int x=0;
cout<<"Enter an integer: ";
cin>>x;


Instead of entering an int e.g. "8", the user enters a string e.g. "asdf". How do I prevent the program from crashing?
Last edited on
Hello, alexyoung1992.

I'm new to the I/O stream myself. However, cin.good( ) is what you want. It basically checks to see whether the users input is valid or not. If it returns false, the input is bad. Otherwise, true is returned to the calling routine.
Last edited on
1
2
char c;
cin >> c;

Or, if you only want to check if they are cheating:
1
2
3
4
5
6
7
8
9
int x;
cin >> x;
if( !cin.good() ) {
    cerr << "User's been bad!";
    //If you need to use cin again then:
    cin.sync();
    cin.clear();
    //(I think... ;)
}
Here's an example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <string>
#include <iomanip>

using namespace std;

int main ()
{

string name;

cout << "Enter your name";
cin >> name;

cout << "Your name is: " name;

system ("pause);

}
 


And the output would be

Your name is: (your name here)

Alright I just included a snippet of my actual code just for the sake of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<ios>
#include<iostream>

using namespace std;

int main(){

   int quantity=0;
   bool flag=false;
   do{
      cout<<"\n   Please enter the quantity to be sold: ";
      cin>>quantity;
      if(!cin.good()){
         cerr<<"\n   ERROR: You entered a negative number or a non-numerical character.\n";
	 flag=true;
	 cin.sync();
	 cin.clear();
      }else
	  flag=false;
   }while(flag);
}

quantity must be an int and I have to use the << operator. If someone types in "qwkleh;f;q" or any junk, my program shouldn't crash. Right now, if I do that, the program repeats
Please enter the quantity to be sold of that ticket:
ERROR: You entered a negative number or a non-numerical character.
in an infinite loop. It is not allowing the user to input a new response.
Last edited on
I've copied your code into my IDE and made 3 changes:

1) On line 15, I removed this line. Since flag is false already, reassigning it to false won't have an effect.
2) On line 19, I swapped the value with true.
3) On line 20, I added the ! (NOT) operator.
Last edited on
closed account (1vRz3TCk)
Here is another way...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>

int main(){

   int quantity=0;

   while ((std::cout << "\n   Please enter the quantity to be sold: ")
          && !(std::cin >> quantity) || quantity < 0 ) 
   {
     std::cout << "\n   ERROR: You entered a negative number or a non-numerical character.\n";
     std::cin.clear();
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
   }

   std::cout << "You entered " << quantity <<std::endl;

}


Last edited on
Hm...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main() {
	int quantity = 0;
	do {
		cout << "\n   Please enter the quantity to be sold: " << flush;
		cin >> quantity;
		if( cin.good() && quantity >= 0 )
			break;
		cerr << "\n   ERROR: You entered a negative number or a non-numerical character.\n";
		cin.sync();
		cin.clear();
	} while(true);

	return 0;
}
...Works for me!
Topic archived. No new replies allowed.