Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been working on a grade system but I am not sure whats wrong with the code.

C
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main()
{
    int aCount = 0, bCount = 0, cCount = 0, fCount = 0;
    int name = 0;
    int name2 = 0;
    string one = "";

    printf("Enter your grade: ");

    scanf("%d",&name);

    if (name >=90) {
        // one = one + "A";
        name2 = 1;
    }
    else if (name >= 80) {
        // one = one + "B";
        name2 = 2;
    }
    else if (name >=70) {
        // one = one + "C";
        name2 = 3;
    }
    else if (name <=69) {
        // one = one + "F";
        name2 = 4;
    }

    //----------------------------------------------

    switch( name2 ) {
        case '1':
            ++aCount;
            break;
        case '2':
            ++bCount;
            break;
        case '3':
            ++cCount;
            break;
        case '4':
            ++fCount;
            break;
    }

    printf( "A: %d\n", aCount );

    printf( "B: %d\n", bCount );

    printf( "C: %d\n", cCount );

    printf( "F: %d\n", fCount );

    getchar();
    getchar();
    return 0;
}



the task of the above code is that is first asks for your grades number example: 90. then using the if else statement makes name2 variable to 1 if name is >= 90, 2 if name >= 80, 3 if name >= 70 and 4 if name <=69. After doing that it uses the switch case statement, to check if the variable called name2 is 1 or 2 or 3 or 4. After that its adds ++1 into counter and shows me the result. The problem currently is that it is not showing anything other then all values = 0.

I am not sure what exactly I am doing wrong here. Any examples would be great. :)
Posted
Updated 8-Sep-13 2:40am
v2

1 solution

To solve your problem remove the '' from the case values.
C
switch( name2 ) {
    case 1:
        ++aCount;
        break;
    case 2:
        ++bCount;
        break;
    case 3:
        ++cCount;
        break;
    case 4:
        ++fCount;
    break;
}


May I suggest you merge the if- and switch-statement.
C
if (name >= 90)
{
    ++aCount;
}
else if (name >= 80)
{
    ++bCount;
}
else if (name >= 70)
{
    ++cCount;
}
else if (name <= 69)
{
    ++fCount;
}
 
Share this answer
 
v2
Comments
Ammar_Ahmad 8-Sep-13 7:10am    
That didn't work D:
But Ive found something interesting.

scanf("%d",&name);

if (name >=90)
{
// one = one + "A";
name2 = 1;
}
else if (name >= 80)
{
// one = one + "B";
name2 = 2;
}
else if (name >=70)
{
// one = one + "C";
name2 = 3;
}
else if (name <=69)
{
// one = one + "F";
name2 = 4;
}
printf("%d", &name2);

putting printf("%d", &name2); and giving it any input gives me a value: 2293576. Not sure why it is not giving that. When it should be giving me 1 or 2 or 3 or 4 in that variable.
André Kraak 8-Sep-13 7:20am    
2293576 is the pointer value, because you are using '&name2'.
Use 'name2' without '&' instead.
Ammar_Ahmad 8-Sep-13 7:37am    
Actually that worked :D thanks for the tip. Too bad they don't teach these sort of stuff in school D:

Here is the code in case anyone else wanted to do this:

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main()
{
int aCount = 0, bCount = 0, cCount = 0, fCount = 0;
int name = 0;
int name2 = 0;
string one = "";

printf("Enter your grade: ");

scanf("%d",&name);



if (name >=90)
{
// one = one + "A";
name2 = 1;
switch( name2 ) {
case 1:
++aCount;
break;
}
}
else if (name >= 80)
{
// one = one + "B";
name2 = 2;
switch( name2 ) {

case 2:
++bCount;
break;
}
}
else if (name >=70)
{
name2 = 3;

// one = one + "C";
switch( name2 ) {
case 3:
++cCount;
break;
}
}
else if (name <=69)
{
name2 = 4;

switch( name2 ) {
case 4:
++fCount;
break;
// one = one + "F";
}

}
printf("%d", name2);



//----------------------------------------------





printf( "A: %d\n", aCount );

printf( "B: %d\n", bCount );

printf( "C: %d\n", cCount );

printf( "F: %d\n", fCount );

getchar();
getchar();
return 0;

}
Ammar_Ahmad 8-Sep-13 7:28am    
I have done it using if else if before but I am required to do this using the case switch. Which I am unable to understand how to do.. Any suggestion on how to use it in switch case would be great.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900