If else problems or something else i'm lost

I posted about this last night however a new problem has happened. I want the code to be able to understand when i enter a lower case letter that it goes in the "if statement" for lower case and same for upper case and numbers between 0-9 however when the code is run, you get the proper ASCII value for the character that was input but it always puts it in the 'numbers group' statement. How can I fix this? also sorry if re-posting is not allowed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;

int main()
{
  	
	cout<< "Enter either a upper case letter, a lower case letter, or a number between 0-9: ";
	char nInput = '0';
	nInput != 'A', 'a';
	char Ucap = 'A';
	Ucap != '0', 'a';
	char Lcap = 'a';
	Lcap != '0', 'A';
	
	char answer;
	cin>> nInput && Ucap && Lcap; /* I know this is wrong but unsure how to fix*/
	
	if(nInput){
		cout<<"You entered " << nInput << " which has a ASCII value of " << (int)nInput << ". " <<endl;	
		cout<< nInput << " is part of the Numbers Group."<<endl;
	}else if(Ucap){
		cout<<"You entered " << Ucap << " which has a ASCII value of " << (int)Ucap << ". " <<endl;
		cout<< Ucap << " is part of the Upper Case Group."<<endl;		
	}else if(Lcap){
		cout<<"You entered " << Lcap << " which has a ASCII value of " << (int)Lcap << ". " <<endl;	
		cout<< Lcap << " is part of the Lower Case Group."<<endl;
	}
	
	system("pause");
	return 0;
}
Last edited on
There are ways to test a character.
See http://www.cplusplus.com/reference/cctype/isalpha/
You misunderstand the comma operator. nInput != 'A', 'a'; is equivalent to
1
2
nInput != 'A';  // evaluates whether nInput is NOT equal to A, then throws away the result;  
'a';  // evaluates 'a' and throws away the result 


cin>> nInput && Ucap && Lcap;
You have to read a character and then check to see if it's a letter or number:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cctype>

int main()
{
    char nInput;
    std::cout<< "Enter either a upper case letter, a lower case letter, or a number between 0-9: ";
    std::cin >> nInput;

    std::cout << "You entered \"" << nInput << "\" ";
    if (isupper(nInput)) {
	std::cout << "which is upper case\n";
    } else if (islower(nInput)) {
	std::cout << "which is lower case\n";
    } else if (isdigit(nInput)) {
	std::cout << "which is a digit\n";
    } else {
	std::cout << "which isn't a letter or digit\n";
    }
    
}

I'm super new so I don't really understand what it is you did to make this work. is there any way you could explain what the std::cout<< means? thanks
it is just cout.
your program has "using namespace std". If you don't do that, you have to invoke the namespace to use cout, and that uses the :: operator.
so putting it all together, you say std::cout << "text"; //this is the same as cout << "text"; in your program.


The standard namespace has gotten so large (namespace std) that it has so many things in it that people sometimes accidentally re-create an entity that is in it, and this causes strange bugs. It is considered better to qualify each item with std:: instead of using the huge namespace and risking those kinds of errors.

Topic archived. No new replies allowed.