code is at bottom

i have to make the program below display lettergrade next to average and ask for the grades in a loop can anyuone help me please

btw this is how im getting graded

(70 is all i need)

100: do the 85 except:

Code:

``````#include <iostream>

using namespace std;

void output(double avg1, char lg)
{
}

double calc(int numg, double s){
return s/numg;
}
void input( int &numg, double &s){

int counter=0;

do{
cout<<"enter the number of grades you will be entering"<<endl;
cin>>numg;

s=s+numg;

}
while (counter<numg);
}

if(avg>=93){
return ('A');
}
else if((avg>=85) && (avg<=93)){
return ('B');
}
else if(avg>=77 && (avg<=85)){
return ('C');
}
else if((avg>=70) && (avg<=77)){
return ('D');
}
else{
return ('F');
}
}

int main(){
double average;
double sum;
return 0;

}
``````

## All 6 Replies

The only problem I spotted after a very very quick look at your code was that you never incremented "Counter" in your Input function. Thus you were stuck in an infinite loop.

I haven't tested this but meh.. Since I see you already posted and shown an attempt, I'll show a suggestion. Now this code has no error handling and is definitely error prone. I'll leave that up to you to do. The idea is fairly simple. You get the user input and store how many values they entered. Sum them up on the spot. Return both of those. Finally, Calculate the average. Check the range of the average and return the letter that matches. Do some error checking of course!

``````#include <iostream>
#include <windows.h>

using namespace std;

int Input(double &Num)  //Returns Number of Values entered. Num = Sum of values entered.
{
int Result = 0, Args = 0;
cout<<"Enter the Number Of Grades You'll be Entering: "<<endl;
cin>>Args;
cin.ignore();

for(Result = 0; Result < Args; ++Result)
{
int X = 0;
cin>>X;
cin.ignore();

Num += X;
}

return Result;
}

double Calc(int Args, double Sum)
{
if (!Args)
return 0;  //This is so we cannot divide by 0.

return ((Sum / Args) * 1.0);
}

{
if (!Average)
return '0';

int Rounded = static_cast<int>(Average);

if (Rounded >= 93)
return 'A';
else if (Rounded >= 85)
return 'B';
else if (Rounded >= 77)
return 'C';
else if (Rounded >= 70)
return 'D';
else
return 'F';
}

int main()
{
int Num = 0;
int Args = Input(Sum);
}
``````

can you keep it like a computer science I beginner level with the same functions and variables as i had up top

Let me know which part you didn't understand? I'll break it down or change it. I only wrote that quickly and didn't give it much thinking.

the loop part

can you keep it like a computer science I beginner level with the same functions and variables as i had up top

So what you want is to for him rewrite your homework for you so it looks like you wrote it, not an expert?

Wouldn't it be better if you actually wrote it, being guided by help so you'd actually learn to do it?

I'd suggest you read a tutorial on C++, etc.. After you get the basics down, you'll realize what I did wasn't pro or anything and it was in fact basic. Now your teacher may not have taught you for-loops yet and I'm sorry about that but I will teach you it now so that you understand it. You can of course convert it to a while loop if you like but I suggest you learn a for-loop because it is the most important loop you'll ever learn. Well at least it's the most used/useful.

Tutorial sites will usually teach you this around lesson 4-5 I believe.

Anyway..

A for-loop is like a while loop except that it increments the values you specify. The for-loop I used says:

1. Enter the number of values you will be entering. (This is the amount of grades the user wants to calculate the average from.)

2. While "SomeVariable" is less than the number they entered, keep looping. Increment "SomeVariable" at the end of the loop.

Thus these two are equivalent:

``````int Result = 0, NumberOfGrades = 0;
double Sum = 0;

cout<<"Enter the Number of Grades you wish to calculate the average with: "<<endl;
cin.ignore();

{
double X = 0;
cout<<"Enter Grade # 1 (2,3,4,5, etc..): "<<endl;  //I did "Enter Grade # "<<Result <<"endl;
cin>>X;
cin.ignore();   //Ignore the enter button/new-line character.

Sum = Sum + X;  //This is the same as Sum += X;

++Result;   //This is the same as Result = Result + 1.
}

//The above is the same as:

for (Result = 0; Result < NumberOfGrades; ++Result) //While Result < Number Of Grades, Keep looping. Increment Result at the end of the loop.
{
double X = 0;
cout<<"Enter Grade #"<< Result + 1<<endl;   //Enter Grade #1, #2, #3, #4, etc.. until the loop is done. This lets the user keep entering grades.
cin>> X;
cin.ignore();

Sum += X;      //Sum = Sum + X;
}
``````

Thus the above was done like so:

``````int Input(double &Num)  //Returns Number of Values entered. Num = Sum of values entered.
{
int Result = 0, Args = 0;  //Some variables.
cout<<"Enter the Number Of Grades You'll be Entering: "<<endl;
cin>>Args;       //Args holds the number of values the user will be entering.
cin.ignore();    //Ignore the Enter button.

for(Result = 0; Result < Args; ++Result) //While Result is less than the number of values they said they'd enter..
{
int X = 0;                          //Set X to 0 every loop.
cout<<"\n\nPlease Enter Grade #"<<Result + 1<<": ";  //Tell them to enter the next number.
cin>>X;                            //Store what they entered in X.
cin.ignore();

Num += X;                         //Add the previous number + the one they just entered.
}

return Result;                      //Return the Sum of all the numbers they entered.
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.