You are here:

C++/C++ For Loop

Advertisement


Question
QUESTION: Hi there, I have been stuck on this for almost a day! I'm sure it's nothing that serious, but it's just finding what the problem is that's frustrating me so much!

I am building this program for my grading book. The first thing I need to put in is the "number of student" that will be graded. With that number, I want to start a loop and ask of each student what "their grade" is. Then I have my rubric conversion into a letter grade set up.
Here is what i have so far:

#include <iostream>
using namespace std;
int main ()
{int score, n, i;
cout<<"Enter Number of Students: "<<flush;
cin>>n;
for (i=1;i<=n;i++)
cout<<"Enter the Score: "<<endl;
cin>>score;
{if (score>=90)
  cout<<"The Grade is A. "<<flush;
if ((score>=80)&&(score<90))
  cout<<"The Grade is B. "<<flush;
if ((score>=70)&&(score<80))
  cout<<"The Grade is C. "<<flush;
if ((score>=60)&&(score<70))
  cout<<"The Grade is D. "<<flush;
if (score<60)
  cout<<"The Grade is F. "<<endl;
}
}

Now, when I build my program, there's a few problems:
1- When I input the number of students, the right amount of "Enter the Score" comes out. However, I was hoping that I could write their grades right after each of the sentences.
2- When I enter all the scores needed, only the first score that I imput converts. The others don't.

Ideally, I would like to have each converted scores (from # to grade letter)on each of their following lines.

I hope I did not confuse you too much...I know I am! I would so appreciate it if you could help me with this. It would be time consumming for me. I also want to program this to do other computations, but I think I'll be able to manage from there!
Thank you so much!!
Celine

ANSWER: Hi Celine,

Nope, I'm not confused.  You have given it a good shot and it's really close.  Good job.

For #1:

Use:

cout<<endl<<"Enter the Score: ";

For #2:

Move the { from the {if(score>90) to before the cout in #1:

Like this:

...
for (i=1;i<=n;i++)
{cout<<endl<<"Enter the Score: ";
cin>>score;
if (score>=90)
...

You're welcome!
Bill

---------- FOLLOW-UP ----------

QUESTION: Hi Bill!
You have been a great help so far, so I was wondering if you could help me out one more time?! I also wanted my program to be able to tell me how many of my n number of students fell in the A range, The B range, up untill my F grades.
Here's what I got so far:

#include <iostream>
using namespace std;
int main ()
{   int score, n, i, t_score=0, highest=0, lowest=100;
  cout<<"Enter Number of Students: "<<flush;
  cin>>n;
  for (i=1;i<=n;i++)
  {cout<<endl<<"Enter the Score: ";
   cin>>score;
   t_score=t_score+score;
     if (score>=90)
        cout<<"The Grade is A. "<<endl;
     if ((score>=80)&&(score<90))
        cout<<"The Grade is B. "<<endl;
     if ((score>=70)&&(score<80))
        cout<<"The Grade is C. "<<endl;
     if ((score>=60)&&(score<70))
        cout<<"The Grade is D. "<<endl;
     if (score<60)
        cout<<"The Grade is F. "<<endl;
         if (score>=highest)highest=score;
         if (score<=lowest)lowest=score;

  }
  
        long average;
         average=t_score/n;
         cout<<"The Average Score of the Class is: "<<average<<endl;
        
         cout<<"The Highest Score is: "<<highest<<endl;
         cout<<"The Lowest Score is: "<<lowest<<endl;

         int n_1=(score>=90), n_2=((score>=80)&&(score<90)), n_3=((score>=70)&&(score<80)), n_4=((score>=60)&&(score<70)), n_5=(score<60);
         cout<<"Number of Students who Received Grade A: "<<n_1<<endl;
         cout<<"Number of Students who Received Grade B: "<<n_2<<endl;
         cout<<"Number of Students who Received Grade C: "<<n_3<<endl;
         cout<<"Number of Students who Received Grade D: "<<n_4<<endl;
         cout<<"Number of Students who Received Grade F: "<<n_5<<endl;
         
}

Thanks for all the help, I can't wait to start working with this!

Answer
Hi Celine,

I have no problem helping when it's clear that effort is being put forth on the problem or program.

The only way to show the number of A's, B's ... F's is to count them as they are entered.  Then display this after n grades are entered.

After main...

int n_A = 0, n_B = 0, n_C = 0, n_D = 0, n_F = 0,

Where the tests are now:

if (score>=90) {
cout<<"The Grade is A. "<<endl; ++n_A;
} else if ((score>=80)&&(score<90)) {
cout<<"The Grade is B. "<<endl; ++n_B;
} else if ((score>=70)&&(score<80)) {
cout<<"The Grade is C. "<<endl; ++n_C;
} else if ((score>=60)&&(score<70))
cout<<"The Grade is D. "<<endl; ++n_D;
} else {
cout<<"The Grade is F. "<<endl; ++n_F;
}

<End of main>
remove int n_1 line

Add:

cout<<"Number of Students who Received Grade A: "<<n_A<<endl;
cout<<"Number of Students who Received Grade B: "<<n_B<<endl;
cout<<"Number of Students who Received Grade C: "<<n_C<<endl;
cout<<"Number of Students who Received Grade D: "<<n_D<<endl;
cout<<"Number of Students who Received Grade F: "<<n_F<<endl;

Good luck!
Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.