handeling int's being put in as letters...

Started by
7 comments, last by Washu 19 years, 4 months ago
right...so here is a code:

#include <iostream.h>
#include <stdlib.h>

int main()
{
  int a;
  while (a != 1) {
    system("CLS");
    std::cout << "Please enter 1" << std::endl;
    std::cin >> a;
    if (a >= 2 || a <= 0) {
      std::cout << "Error...please enter 1!" << std::endl;
      system("PAUSE");
    }
  }
  std::cout << "Thank you!" << std::endl << std::endl;
  system("PAUSE");
  return 0;
}
very simple...my question is...how do i make an if statment to handel if the person running it would put in, say, "a"... when i do test it and put in "a" then it just goes freaky and i have to close out of the actual program... thanks! -Ajain
...though i do not believe in what you are saying, I will defend your right to say it to my death!(no source sited)
Advertisement
Accept the input as a char or a string, and then check/convert it to number.
umm...in nwb...please?

-Ajain
...though i do not believe in what you are saying, I will defend your right to say it to my death!(no source sited)
The simplest way is to synch the stream and clear all errors before reading.

std::cin.sync();std::cin.clear();if(std::cin >> a){	// valid input}else{	// invalid input}

I don't get it...? what is that? I plugged it into the program both directly before and directly after the input but it still doesn't do anything...

thanks but...

-Ajain
...though i do not believe in what you are saying, I will defend your right to say it to my death!(no source sited)
I think what the anonymous poster was trying to say is that cin returns a bool. True means that the input was valid, false means that it is invalid. If you put the call cin in a conditional statement, you can test whether it was successful or not.
Instead of making a int, make it char. Now if you check the ASCII table you'll see that '1' equals 49.

Explanation: a char is actually a value converted to a sign by the console. So if you set a char to 49 without quotes it will become '1' as a character. But if you put 1 without quotes, you'll get SOH-character which is of no use.

In C++ you can check chars with singlequotes '1' or with the int-value which is 49.

char a;cin >> a;if(a == '1' && a == 49) {    // Yes! It rocks!}



Note: with character-strings (char*) it does not work. You'll need to use a function like atol() to convert the character-string to an integer. But in your example, you're only checking a single-digit number, so my above example should work in this case.
Here's how I would do it.

char input;
int a = 0;
while(a != 1) {
...std::cin >> input; // Read character in. WARNING: the input should probably be a character array, to prevent more than one character being entered.
...a = atoi(input); // Convert first character to a number.
}
1) include <iostream> and <cstdlib>. iostream.h is depreciated, and cstdlib is the C++ version of stdlib.h. (you will need to either fully qualify the namespace, or stick a using namespace std; right after the header includes)
2) you need to initialize a, right now it could contain anything, even a 1.
#include <iostream>#include <cstdlib>#include <string>//this:using std::cin;using std::string;using std::system;//or this://using namespace std;int main() {  int a = 0; //initialized to 0  while(a != 1) {    while(!(cin>>a)) { //while the stream is not good (aka, they enter in a character)      string chuckable;      cin.clear(); //clears the error flags, returns the stream to a readable state      cin>>chuckable; //will read up to the next whitespace.    }    //... your code here...  }}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement