I'm new to programming and this really is driving me crazy. I need to make this if statement work, but I keep getting an error that says, "ISO C++ forbids comparison between pointer and integer." I can't seem to find a definite answer to my problem. Could somebody please help me?
It's stopping my entire project. I can't even compile. I have identified the problem inside of the if statement.
1 //Sam Vernaza CS 162 Lab #2
2 //Program that writes out and compiles employees initials
3
4 //The first section of this program is going to take initials and cap them.
5
6 #include <iostream>
7 #include <cctype>
8
9 using namespace std;
10
11 int main()
12 {
13 char initials[4];
14 cout << "Please enter your initials:" << endl;
15 cin >> initials;
16 cin.ignore (100, '\n');
17 cout << "You entered, " << initials << endl;
18 if(initials < 'A' || initials > 'Z') // need help here
19 {
20 cout << "You've entered: " << initials << endl;
21 }
22 else
23 {
24 cout << "Enter a valid entery" << endl;
25 }
26
27 return 0;
28 }
What part(s) of the initials should be alphabetical?
If I enter "A B", "A.B.", "AB", "ab", and "a@B$", which ones should be accepted as valid initials?
You can check if the first character (and only the first character) is alphabetical using if (std::isalpha(initials[0]))
or if ((initials[0] >= 'A' && initials[0] <= 'Z') || (initials[0] >= 'a' && initials[0] <= 'z'))
or if (std::toupper(initials[0]) >= 'A' && std::toupper(initials[0]) <= 'Z')
Isn't there an easier way to do that? If I were to convert my cin >> initials; statement to be uppercase upon entry, wouldn't I only have to type either side of the bottom if statement?
Yeah, if you convert it to uppercase after the user enters it, you would just need if (initials[0] >= 'A' && initials[0] <= 'Z')
to check if the first character is alphabetical.
If you need to check the other characters as well, you'll want to use a loop.
Probably you should give a try with loop.Thats seems a good option.And plz ask your teacher for a proper explaination cuz whats written here cannot really be explained. :D
looks like i have to make sure one thing:
do you understand all the previous posts above, already?
because i don't see you understand those posts
if you don't understand, feel free to ask :)
lol.I see the same thing here.
@SVcpp: you need to understand the difference between string and character.u r performing operation on string that are meant only for characters.
And writing
char first = toupper(initials[4]);
doesnt mean it will convert the whole string to upper case.Its gonna convert only 5th character in initials.