HELPP!

Hello,

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 exactly is your if statement trying to do?
I need my if statement to display the initials if they are alphabetical, if not, I need it to print out, "Enter a valid entry."

The if statement is exactly how the teacher wrote it.
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')
Last edited on
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.
Last edited on
would I do it like this?

#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 char first = toupper(initials[4]);
16 cin >> initials;
17 cin.ignore (100, '\n');
18 if (initials[0] >= 'A' && initials[0] <= 'Z')
19
20 {
21 cout << "You've entered: " << initials << endl; // it's not printing in caps
22 }
23 else
24 {
25 cout << "Enter a valid entery" << endl;
26 }
27
28 return 0;
29 }
~
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
ok, last question

why isn't it capitalizing the initials?

should i be able to convert the code topper and it will print initials in caps?
What you're doing is capitalizing the 5th character (initials[4], which is out-of-bounds).
To capitalize all of them, you need to use a loop.
Last edited on
Now my code isn't returning the if statement, it's only returning the else???


AHHH!!
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 char first = toupper(initials[4]);
16 cin >> initials;
17 cin.ignore (100, '\n');
18 if (initials[0] <= 'a' && initials[0] >= 'z')
19
20 {
21 cout << "You've entered: " << initials << endl;
22 }
23 else
24 {
25 cout << "Enter a valid entery" << endl;
26 }
27
28 return 0;
29 }
closed account (j3Rz8vqX)
if (initials[0] >= 'a' && initials[0] <= 'z')
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 :)

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.
Topic archived. No new replies allowed.