I am doing a program for school that lets the user guess the number. I am to be tested for all situations which include the user pressing a "letter" on the keyboard. I believe that I have all of the other errors handled, except for if the user enters a letter, and then the program will not execute anymore. Any help is greatly appreciated.

Recommended Answers

All 4 Replies

There is a standard function IsAlpha() that i have seen before in a previous post i will look it up for you

i believe its in <cctype> and it returns 0 if it is numerical

alternatively there is the isdigit() function which can do all the error testing at once (it returns 0 if the character is a digit)

#include <cctype>
using namespace std;

char input;

cout << "Type in a number";
cin >> input;

if(!isdigit(input))
{
    // process the number
}

example testing code from a site i found

http://www.cse.msu.edu/~cse231/Examples/CoursePack/Example04.htm

/**********************************************************************

 Example #4 -- Boolean expressions

**********************************************************************/

 

#include <iostream>

#include <cctype>

using namespace std;

 

int main()

{

  const bool A = true, B = false;

  

  const int C = 3, D = 8;

  

  const char E = 'z', F = '5';

  

  cout << endl;

  cout << "bool A: " << A << endl;

  cout << "bool B: " << B << endl << endl;

  

  cout << boolalpha;

  cout << "bool A: " << A << endl;

  cout << "bool B: " << B << endl << endl;

  

  cout << "int C: " << C << endl;

  cout << "int D: " << D << endl << endl;

  

  cout << "C == D: " << (C == D) << endl;

  cout << "C != D: " << (C != D) << endl;

  cout << "C < 3:  " << (C <  3) << endl;

  cout << "C <= 3: " << (C <= 3) << endl;

  cout << "C > 3:  " << (C >  3) << endl;

  cout << "C >= 3: " << (C >= 3) << endl << endl;

  

  cout << "C >= 5 || D <= 9: " << (C >= 5 || D <= 9) << endl;

  cout << "C >= 0 && D >= 4: " << (C >= 0 && D >= 4) << endl << endl;

  

  cout << "C != D || C < -6: " << (C != D || C < -6) << endl;

  cout << "C <= 0 && D > -6: " << (C <= 0 && D > -6) << endl << endl;

  

  cout << "1 <= C && C <= 5: " << (1 <= C && C <= 5) << endl;

  cout << "1 <= D && D <= 5: " << (1 <= D && D <= 5) << endl << endl;

  

  cout << "char E: " << E << endl;

  cout << "char F: " << F << endl << endl;

  

  cout << "isalnum( E ): " << isalnum( E ) << endl;

  cout << "isalpha( E ): " << isalpha( E ) << endl;

  cout << "islower( E ): " << islower( E ) << endl;

  cout << "isdigit( E ): " << isdigit( E ) << endl;

  cout << "toupper( E ): " << static_cast<char>( toupper( E ) )

    << endl << endl;

  

  cout << "isalnum( F ): " << isalnum( F ) << endl;

  cout << "isalpha( F ): " << isalpha( F ) << endl;

  cout << "islower( F ): " << islower( F ) << endl;

  cout << "isdigit( F ): " << isdigit( F ) << endl << endl;

  

  return 0;

}

Thank you for all of your help everybody, it is greatly appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.