geckocode666
geckocode666

Reputation: 3

I'm having a problem with reading a string in an if statement

I'm a beginner in C++ coding and I have a problem with an assignment question:

Write a C++ program that prompts the user to enter two characters and displays the major and status represented in the characters. The first character indicates the major and the second is number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. Suppose the following characters are used to denote the majors:
M: Mathematics
C: Computer Science
I: Information Technology

This is what I tried so far:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string i;
    cout <<"Enter two characters: ";
    cin>> i;
    i[0]=toupper(i[0]);
    if (i[0]=='M')
    {
        if (i[1]==1)
        {
            cout<<"Mathematics Freshman."<<endl;
        }
        else if (i[1]==2)
        {
            cout<<"Mathematics Sophomore."<<endl;
        }
        else if (i[1]==3)
        {
            cout<<"Mathematics Junior."<<endl;
        }
        else if (i[1]==4)
        {
            cout<<"Mathematics Senior."<<endl;
        }
        else
        {
            cout<<"Invalid status code."<<endl;
        }
    }
    return 0;
}

When I run and type for example "m1" it should print "Mathematics Freshman.", but it prints "invalid status code", which is the output from the else statement, and I don't know why.

Upvotes: 0

Views: 191

Answers (2)

teoscuraa
teoscuraa

Reputation: 1

What you got inside your string is a list of characters, remember that in C and C++, char variables aren't equivalent to what they display! For example: '1' and 1 are two different things! As '1', when being treated as a integer, doesn't equal to 1, but to the ASCII code for the character '1'.

From if (i[1]==1) to if (i[1]=='1').

What you should do is instead replace the conditions in your if statements to comparisons with the number characters, and not the standalone numbers. Or better yet, make use of switch(operator), you can read more about it here, as it will scale much better as your list of cases grows bigger, both in convenience as a code writer and to the people who are gonna read the code later.

Upvotes: 0

user11247278
user11247278

Reputation:

You should compare a character to character instead of the integer. Try this

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string i;
    cout <<"Enter two characters: ";
    cin>> i;
    i[0]=toupper(i[0]);
    if (i[0]=='M')
    {
        if (i[1]=='1')
        {
            cout<<"Mathematics Freshman."<<endl;
        }
        else if (i[1]=='2')
        {
            cout<<"Mathematics Sophomore."<<endl;
        }
        else if (i[1]=='3')
        {
            cout<<"Mathematics Junior."<<endl;
        }
        else if (i[1]=='4')
        {
            cout<<"Mathematics Senior."<<endl;
        }
        else
        {
            cout<<"Invalid status code."<<endl;
        }
    }
     return 0;
}

Upvotes: 2

Related Questions