if else statement question

I have my program running but now my actions aren't running. I don't understand why they wouldn't run so can I have some insight as to how these if actions are not working. Here is the code:

if (((gender=M)&&(grade>=90))||((gender=F)&&(grade>=80)))
cout << name << " is in Class A." << endl;
if (((gender=M)&&(grade>=70&&grade<=90))||((gender=F)&&(grade>=70&&grade<=80)))
cout << name << "is in Class B." << endl;
if ((gender=M)&&(grade<70)) cout << name << " is in Class C." << endl;
if ((gender=F)&&(grade<70)) cout << name << " is in class D." <<endl;
return 0;
}


But if you want to see the full code, you can search for unknown error in program. Thank you
Post your full code.
You're using >= and <=, wich makes that a student with a grade of 70 will be set in both class A and B. And anywhere where you have gender=M it should be gender==M; otherwise it's an assigment, not an comparison, and will always return true (and the gender will always be F after all if-statements have been executed).
Try or this solve all the problems. I may have missed something, the code is kinda messy. Try to use this lay-out in longer conditions:
1
2
3
if ( (CONDITION && CONDITION) ||
     ...
     (CONDITION || CONDITION) ) {}


When posting code, you can use the code-format (#format); see on your right when posting.
the full code is here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# include <iostream>
using namespace std;
int main(){
int grade;
char gender;
string name;
cout << "What is your name?";
cin >> name;
cout << "What is your gender (M or F)?";
cin >> gender;
cout << "What is your grade?";
cin >> grade;
if (((gender=='M')&&(grade>=90))||
((gender=='F')&&(grade>=80)))
{ cout << name << " is in Class A." << endl;}
if(((gender=='M')&&(grade>=70&&grade<=90))||
((gender=='F')&&(grade>=70&&grade<=80)))
{ cout << name << "is in Class B." << endl;}
if ((gender=='M')&&(grade<70)){ cout << name << " is in Class C." <<
endl;}
if ((gender=='F')&&(grade<70)){ cout << name << " is in class D." <<
endl;}
return 0;
}
Last edited on
Use getline for input: http://www.cplusplus.com/forum/articles/6046/, and pause the program in the end ( cin.ignore() ). The if-statements seems to work good, aldo you still use both <= and >= (you should use < and >= or > and <=, to avoid more then one statement will return true).
I'm using getline, but i get an error that states invalid conversion from 'void*' to 'char*' to 'size_t*'.
Here is an example of what I put for getline:
getline (cin, name);
getline (cin, gender);
getline (cin, grade);
closed account (z05DSL3A)
A better structure for your ifs would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    if (gender=='M')
    {
        if(grade >= 90)
        {
            cout << name << " is in Class A." << endl;
        }
        else if (grade>=70 && grade<=90)
        {
            cout << name << "is in Class B." << endl;
        }
        else
        {
            cout << name << " is in Class C." <<endl;
        }
    }
    else if (gender=='F')
    {
        // process female grades
    }
    else
    { 
        cout << name << " your gender is in question." << endl;
    }
ive done everything and it seems like my program is correct, but can someone copy it and run it and see why the actions aren't coming out. it takes the input but doesn't seem to want to type out the name and wat class your in. here is the final code for the program:
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
# include <iostream>
using namespace std;
int main(){
int grade;
char gender;
string name;
cout << "What is your name?";
cin>>name;
cout << "What is your gender (M or F)?";
cin>>gender;
cout << "What is your grade?";
cin>>grade;
if (gender=='M')
    {
        if(grade >= 90)
        {
            cout << name << " is in Class A." << endl;
        }
        else if (grade>=70 && grade<90)            
        {    
            cout << name << "is in Class B." << endl;
        }  
        else
        {
            cout << name << " is in Class C." <<endl;
        }   
    }
if (gender=='F')
    {
        if(grade >= 90)
        {
            cout << name << " is in Class A." << endl;
        }   
        else if (grade>=70 && grade<80)
        {
            cout << name << "is in Class B." << endl;
        }
        else
        {

            cout << name << " is in Class D." <<endl;
        }
    }
            
return 0;
}
That structure from Gray Wolf is much better, now it's acually understandable :)

Your program does print the class, but it determinates without pausing. This could be solved by putting system("pause") before return 0, but that's ugly, just like using cin>>int is ugly ;). Read that link i posted before: you can use getline(cin,...) only for strings, but you can convert strings into integers etc. (for explanations: see link). http://www.cplusplus.com/forum/articles/6046/
If you use getline() everywhere for input, you can use cin.ignore() to pause the program before ending. (mixing cin.ignore() and cin>> up gives problems).
Topic archived. No new replies allowed.