Struggling with this code

This code is supposed to list if I'm a freshman, a sophmore, etc, depending on numCredits for the students. What happens is, is that for some strange reasons, it can do Freshman and Sophmore, but it wont do the others, because it keeps claiming that Junior/Seniors are all Sophmores regardless of the credit count, I'm not sure what I'm doing wrong?

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;


int main() 
{
    int numCredits;
    char ch;
    
    cout << "How many credits did you complete: ";
    cin >> numCredits;
    
    if (numCredits < 1 || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
        cout << "Wrong input please run the program again." << endl;
    else 
    {
    
    
    if (numCredits < 32)
    {
        cout << numCredits << " completed credits makes you a Freshman" << endl;
    } 
    else 
    {
        if (numCredits = 32 || numCredits <= 63)
        {
        cout << numCredits << " completed credits makes you a Sophmore" << endl;
        } 
        else    
        {
            if (numCredits >= 64 || numCredits < 95)
                {
                cout << numCredits << " completed credits makes you a Junior" << endl;
                } 
                else 
                {
                    if (numCredits >= 96)
                    {
                    cout << numCredits << " completed credits makes you a Senior" << endl;
                    }
                }
        }
    }   
    
    }
}
Updated version, I saw another error.


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
34
35
36
37
38
39
40
41
42
43

#include <iostream>

using namespace std;

int main() 
{
    int numCredits;
    char ch;
    
    cout << "How many credits did you complete: ";
    cin >> numCredits;
    
    if (numCredits < 1 || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
        cout << "Wrong input please run the program again." << endl;
    
    
    if (numCredits < 32)
    {
        cout << numCredits << " completed credits makes you a Freshman" << endl;
    } 
    else 
    {
        if (numCredits >= 32 || numCredits < 64)
        {
        cout << numCredits << " completed credits makes you a Sophmore" << endl;
        } 
        else    
        {
            if (numCredits >= 64 || numCredits < 95)
                {
                cout << numCredits << " completed credits makes you a Junior" << endl;
                } 
                else 
                {
                    if (numCredits >= 96)
                    {
                    cout << numCredits << " completed credits makes you a Senior" << endl;
                    }
                }
        }
    }   
}
What is char ch for? You are not calling this in the program, yet you are using this in your first if-statement. Also, for the use of if. Here's how you could list your logic:

1
2
3
4
5
6
7
8
9
if (... something ....){
      ... do something ....
}
else if (... something ...){
      ... do something ....
}
else if (... something ...){
      ... do something ....
}


Try and see if that helps
Last edited on
reverse logic is often simpler in these types of exercises.
if numcred >95
sr
else
if numcred > 63
jr
else
...

you don't need bounds this way, the bounds are 'natural' (if its bigger than 95, it cant be less than 95. If its not bigger than 95, but its greater than 63, it can't be greater than 95... the chained elses prevent that...)

doing it the other way works, it just requires checking conditions more carefully.
also missing a couple return lines. Remember that returning from main() method exits the program. So in your first check for bad input, change it to something like
1
2
3
4
5
if (numCredits < 1)
{
  cout << "Wrong input please run the program again." << endl;
  return -1;
}

Your last line of main() method should be return 0; . 0 typically signifies that the method did not return any errors -- if you ever come across methods that return status integers, non-zero usually indicates an error. Not sure how you managed to compile the program otherwise; after all, main() returns an integer.

As for the main logic, you can continue in the same vein counting upwards, too. But yeah, use else if -- this will drop into only one of the branches in a top-down manner:
1
2
3
4
5
6
7
8
9
10
11
12
if (numCredits < 32)
{
}
else if (numCredits < 64)
{
}
else if (numCredits < 96)  // Double-check this -- everything appears incrementing by 32's
{
}
else // Senior
{
}


counting up or counting down does not matter here. The only real difference would be if you wanted to have a default that you don't bother to check. Eg..

rank = freshman;
if()
rank = sr;
else if ()
rank = jr;
..
//don't check freshman. If nothing changed the value, the default is good.

Sometimes one or the other extreme condition (here, seniors and freshmen) makes a better default value for whatever reason, and that can govern which way you test.




Topic archived. No new replies allowed.