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.

Recommended Answers

All 4 Replies

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;
}

http://www.cplusplus.com/reference/clibrary/cctype/

Those are character functions.

Also, I would suggest including the domain of the problem (the alphabet you intend to work with) in the program rather than relying on the character set.

example: std::string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";

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";
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.