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
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:
# include <iostream>
usingnamespace 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;
}
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);
if (gender=='M')
{
if(grade >= 90)
{
cout << name << " is in Class A." << endl;
}
elseif (grade>=70 && grade<=90)
{
cout << name << "is in Class B." << endl;
}
else
{
cout << name << " is in Class C." <<endl;
}
}
elseif (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:
# include <iostream>
usingnamespace 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;
}
elseif (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;
}
elseif (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).