7 Replies - 939 Views - Last Post: 09 October 2012 - 06:23 PM Rate Topic: -----

#1 jvesley  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 08-October 12

problem with a nested if using some simple logical operators

Posted 08 October 2012 - 04:46 PM

I am getting a very minor error in my program, hopefully someone will be able to fix this easily (I am definitely a beginner): I need my program to output grades for an average of four program scores, three test scores, a course average, and a letter grade for the course. Here is the grading scale for the letter grade:

>=90 :A
80-89:B (HOWEVER, if any of the test scores is >=95, the letter grade is a A-)
70-79:C (HOWEVER, if any of the test scores is >=95, the letter grade is a B-)
<70:F

My program works fine, all the letter grades output correctly except for ONE: when it is supposed to output a B-, it INSTEAD OUTPUTS AN A-. Here is the code, I assume that is an the issue with the nested if:


#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main (){
    system ("color f0");

 //Do while loop for another student
    char anotherStudent;    
    do
    {
    
    //declarations
    string fName,lName,lGrade;
    char mInitial;
    float FstPscore,SndPscore,TrdPscore,FrthPscore;
    float FstTscore,SndTscore,TrdTscore;
    float pAverage,tAverage,cAverage;
    
    //Program Input
    cout<<"Student Grade Calculator"<<endl;
    cout<<endl;
    cout<<"Please enter your first name, middle initial, and last name: "<<endl;
    cin>>fName>>mInitial>>lName;
    cout<<endl;
    cout<<"Thank you, your name is "<<fName<<" "<<mInitial<<" "<<lName<<endl;
    cout<<endl;
    cout<<"Please enter the first program score <0-100>: ";
    cin>>FstPscore;
    cout<<endl;
    cout<<"You entered "<<FstPscore<<endl;
    cout<<endl;
    cout<<"Please enter the second program score <0-100>: ";
    cin>>SndPscore;
    cout<<endl;
    cout<<"You entered "<<SndPscore<<endl;
    cout<<endl;
    cout<<"Please enter the third program score <0-100>: ";
    cin>>TrdPscore;
    cout<<endl;
    cout<<"You entered "<<TrdPscore<<endl;
    cout<<endl;
    cout<<"Please enter the fourth program score <0-100>: ";
    cin>>FrthPscore;
    cout<<endl;
    cout<<"You entered "<<FrthPscore<<endl;
    cout<<endl;
    cout<<"Please enter the first test score <0-100>: ";
    cin>>FstTscore;
    cout<<endl;
    cout<<"You entered "<<FstTscore<<endl;
    cout<<endl;
    cout<<"Please enter the second test score <0-100>: ";
    cin>>SndTscore;
    cout<<endl;
    cout<<"You entered "<<SndTscore<<endl;
    cout<<endl;
    cout<<"Please enter the third test score <0-100>: ";
    cin>>TrdTscore;
    cout<<endl;
    cout<<"You entered "<<TrdTscore<<endl;
    cout<<endl;
    
    //Average Calculations
    pAverage=(FstPscore+SndPscore+TrdPscore+FrthPscore)/4;
    tAverage=(FstTscore+SndTscore+TrdTscore)/3;
    cAverage=(pAverage+tAverage)/2;
 
    //Nested If
    if (cAverage>=90) {lGrade = "A";}                                                    
    else if (cAverage>=80&&FstTscore>=95||SndTscore>=95||TrdTscore>=95) {lGrade = "A-";} 
    else if (cAverage>=80) {lGrade = "B";}
    else if (cAverage>=70&&FstTscore>=95||SndTscore>=95||TrdTscore>=95) {lGrade = "B-";}                                              
    else if (cAverage>=70) {lGrade = "C";}
    else {lGrade = "F";}                          
    
    //Program Output 
    cout<<"123456789012345678901234567890123456789012345"<<endl;//45 spaces 
    cout<<setfill('*');
    cout<<setw(45)<<"*"<<endl;
    cout<<"Name: "<<fName<<" "<<mInitial<<" "<<lName<<endl;
    cout<<endl;
    cout<<fixed<<showpoint<<setprecision(2);
    cout<<"Program Average..........   "<<pAverage<<endl;
    cout<<endl;
    cout<<"Test Average.............   "<<tAverage<<endl;
    cout<<endl;
    cout<<"Course Average...........   "<<cAverage<<endl;
    cout<<endl;
    cout<<"Letter Grade.............   "<<lGrade<<endl;
    cout<<endl;
    cout<<setfill('*');
    cout<<setw(45)<<"*"<<endl; 
    
    //2nd part of do while loop 
    cout<<"Please enter Y for another student or N to quit:";
    cin>>anotherStudent; 
    cout<<endl;
    }while(anotherStudent=='y');
    cout<<endl;
    system ("pause");
    return 0;
}//end of main 




Is This A Good Question/Topic? 0
  • +

Replies To: problem with a nested if using some simple logical operators

#2 ihatesegfault  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 47
  • Joined: 24-September 12

Re: problem with a nested if using some simple logical operators

Posted 08 October 2012 - 05:30 PM

View Postjvesley, on 08 October 2012 - 04:46 PM, said:

    //Average Calculations
    pAverage=(FstPscore+SndPscore+TrdPscore+FrthPscore)/4;
    tAverage=(FstTscore+SndTscore+TrdTscore)/3;
    cAverage=(pAverage+tAverage)/2;
 
    //Nested If
    if (cAverage>=90) {lGrade = "A";}                                                    
    else if (cAverage>=80&&FstTscore>=95||SndTscore>=95||TrdTscore>=95) {lGrade = "A-";} 
    else if (cAverage>=80) {lGrade = "B";}
    else if (cAverage>=70&&FstTscore>=95||SndTscore>=95||TrdTscore>=95) {lGrade = "B-";}                                              
    else if (cAverage>=70) {lGrade = "C";}
    else {lGrade = "F";}  




Ok. this problem is very easily fixed. The issue is that whenever the computer comes to multiple operations of the same order in a row, it executes them from left to right. so 5*8*3*2 = 40*3*2 = 120*2 = 240. Now look at your code in the quote. Lets assume that cAverage is 75, FstTscore is 93, and SndTscore is 98 and TrdTscore are 89. The first if statement fails, but the first else if doesn't. Even thought cAverage is not >= 80, SndTscore is 98. If you look at it step by step its False&&False||True||False = False||True||False = True||False = True! Now you see where it's coming from. You can easily fix this by adding parentheses around the 3 ORs on line 8. You should also do it on line 10.

One other thing I saw, and I don't know if this is how you were supposed to do this, but in formulating the averages you didn't weigh them the same. If you expand cAverage you get the program scores all over 8 but the test scores all over 6. In line 2, if you expand the /4 you get P1/4 + P2/4 + P3/4 + P4/4, and in line 3 you get T1/3 + T2/3 + T3/3. So when you expand cAverage you get P1/8 + P2/8 + P3/8 + P4/8 + T1/6 + T2/6 + T3/6. Just do you averaging to get cAverage from scratch. Sorry if this is what your instructor told you to do.

Also, use a loop and an array to do the input section. (unless you haven't gotten to arrays yet in your course)
Was This Post Helpful? 0
  • +
  • -

#3 jvesley  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 08-October 12

Re: problem with a nested if using some simple logical operators

Posted 08 October 2012 - 06:12 PM

Thanks for the heads up on the weighting! I changed it to add up the program and test scores individually, and then divide by 7.

I'm still having issues with the nested if. It won't run when I try to put parentheses around the ORs. I've tried it a bunch of different ways. Where exactly should I put them? I get a lot of different error messages depending on where I put them (I'm still very new to the syntax, lol).
Was This Post Helpful? 0
  • +
  • -

#4 jvesley  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 08-October 12

Re: problem with a nested if using some simple logical operators

Posted 08 October 2012 - 07:14 PM

Do you mean around (cAverage&&fsttscore), (sndtscore), and (tscore)? Im a little confused.
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: problem with a nested if using some simple logical operators

Posted 08 October 2012 - 07:58 PM

Hi, if the average is 80 or over (and less than 90) the grade is B or A-. The test scores can be tested to see if it is A- or B.

So not everything needs to be tested in one if condition.

  else if (cAverage>=80)
  {
    if(FstTscore>=95||SndTscore>=95||TrdTscore>=95) 
    { }
    else
    { }
  }


This post has been edited by #define: 08 October 2012 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

#6 jvesley  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 08-October 12

Re: problem with a nested if using some simple logical operators

Posted 09 October 2012 - 06:15 AM

  else if (cAverage>=80)
  {
    if(FstTscore>=95||SndTscore>=95||TrdTscore>=95) 
    { }
    else
    { }
  }


[/quote]

I had tried that originally, but I couldn't get it to work. Probably just a syntactical error. I got it to work finally by adding a set of parentheses around logical expressions in the A- and B- lines:

else if (cAverage>=80&&(FstTscore>=95||SndTscore>=95||TrdTscore>=95))


Was This Post Helpful? 1
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: problem with a nested if using some simple logical operators

Posted 09 October 2012 - 06:19 AM

I always recommend parentheses with logical operators, especially when switching between them, if for no other reason than to clarify intent. Using multiple logical operators in the same statement without parentheses, should be avoided.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 ihatesegfault  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 47
  • Joined: 24-September 12

Re: problem with a nested if using some simple logical operators

Posted 09 October 2012 - 06:23 PM

View Postjvesley, on 09 October 2012 - 06:15 AM, said:

else if (cAverage>=80&&(FstTscore>=95||SndTscore>=95||TrdTscore>=95))



This is exactly what I meant.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1