Okay, so my problem is that I'm making a program that identifies whether the user input is a letter, a number between 0-9 or a symbol.

This is what I have so far:

#include <iostream>
#include <iomanip>
#include <windows.h>
#include <string>
using namespace std;

int main()
{
    char variable;
    
    cout << "Enter a letter, symbol or number between 0-9: ";
    cin >> variable;
    cout << endl;
    

    if(( variable >= '0') && (variable <= '9')) //Identifies if its a number between 0-9
    {

         cout << "You have entered: # " << variable << endl << endl;
         
    }else{ 
    (int)variable;
    if( variable == 'a', 'z') //Identifies if its a letter
    {
          cout << "You have entered: " << variable << " à" << endl;
    
    }
    }
    system("pause");
    return 0;
}

Now what I need help with is two things. First, how do I identify if the input is a ASCII symbol. And second, since I'm using a variable that is a char, when the user enters a number longer than one digit it only records the first one, and if i change it to int, then I can't test whether its a letter or a ASCII symbol.

Please if anybody could answer ASAP. I really need this for tomorrow.

Edited 5 Years Ago by Kron: n/a

Oh... Search for comparing text in c++ (I dont know how);
Why couldn't you ask the person what they are entering and then make a piece of code for each option.
I think I made a post on ASCII earlyer it should be one or two pages down in the forum I think it's called ASCII. Also~ If you converted it to a int you could chak whether or not it was a ASCII symbol because there is a (I think) a line in ASCII between letters and symbols.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
 
int main()
{
    char variable;
 
    cout << "Enter a letter, symbol or number between 0-9: ";
    cin >> variable;
    cout << endl;
	
 
    if(( variable >= 48) && (variable <= 57)) //Identifies if its a number between 0-9
    {
 
         cout << "You have entered: # " << variable << endl << endl;
 
    }
	else
	{ 
    
    if( (variable>65&&variable<90)||variable>97&&variable<122) //Identifies if its a letter
    {
          cout << "You have entered: " << (char)variable << " " << endl;
 
    }
	else
	{
		cout<<"You have entered: "<<variable<<endl;
	}
    }
    system("pause");
    return 0;
}

The whole code could be just ten statements. Your biggest mistake is at the "condition".

if(( variable >= '0') && (variable <= '9')) //Identifies if its a number between 0-9
//write something here

else if ((variable >='a') &&(variable <='z'))//Identifies if its lower case letter
//write something here
//to check for upper case, write another statement as lower case above and replace with A and Z
else
		cout <<"invalid";
This article has been dead for over six months. Start a new discussion instead.