Hello all,

I did this program below, it is meant to get a student name then 5 of his/her grades sums them up in a void function and calculate the average but outputs the average from the main

the problem is that the loop goes on and prints the prompt but takes no input

how can I fix that ?

# include <iostream>
# include <string>
using namespace std;

void avg (int grade[]);

int main()
{
  char name;
  double average;
  int grade[5];

  cout << "Student name" <<endl;
  cin.get(name);
  cout <<endl;

  avg (grade);
  cout << "The average is " << average <<endl;

 return 0;
}

  void avg (int grade[])
  {
	  double sum=0;
	  double average;

	  for (int i=0; i<5; i++)
	  {
		  cout << "Please enter grade" <<endl;
		  cin >> grade[i];
			sum= sum+grade[i];
			average = sum/5;
		}
  }

Recommended Answers

All 5 Replies

# include <iostream>
# include <string>

using namespace std; 

void avg (int grade[]); 
  double average; 
int main()
{ 
   char name;  
 
  int grade[5];   
  cout << "Student name" <<endl;
  cin.get(name);
  cout <<endl;
  avg (grade);
  cout << "The average is " << average <<endl;  
  return 0;
}   
void avg (int grade[])  
{	  
double sum=0;	  
	  
for (int i=0; i<5; i++)
	  {
		  cout << "Please enter grade" <<endl;
		                  cin >> grade[i];
			sum= sum+grade[i];
			average = sum/5;
		}  
}

Actually, take the final average calculation (average = sum/5) _outside_ of the for loop otherwise you'll be calculating it each time.

it still does not work

Does your function have to be void? If not change it to double and do return average; To fix it as is:

cin.ignore();
for (int i=0; i<5; i++)
	  {
            
            cout << "Please enter grade" <<endl;
            cin >> grade[i];
	    sum= sum+grade[i];
	   }  
                 average = sum/5;

Your name input was mucking up the input stream.

# include <iostream>
# include <string>
using namespace std;

// Protype for the avg function:
// Passing by reference mainAverage which is the average variable declared in 
// main the function can have a void return type and still get the average grade
// back into the main function because the value of mainAverage is directly changed
// in the avg().
void avg (double& average);

int main()
{
	// String variable for the student's name
	string name; 
	double mainAverage = 0;
	
	// Gets the name of the student
	cout << "Student name: ";
	getline(cin, name);
	
	// sends mainAverage into the avg()
	avg(mainAverage);
	
	
	cout << "The average grade for " << name << " is " << mainAverage << "."<<endl;
	
	system("PAUSE");
	return 0;
}

void avg (double& average)
{
	int grade[5];
	double sum = 0;
	
	// Extra space to make it look better
	cout << endl;
	
	// Loops through 5 times gathering a grade value and summing them all together
	for (int i=0; i<5; i++)
	{
		cout << "Please enter grade: ";
		cin >> grade[i];
		sum += grade[i];
	}
	cout << endl;
	
	// Caclulates the average after the loop is complete
	average = sum / 5;
}

You had a few problems so here is a short list of them:
1) Your name variable was declared as a single char and not a string. This is why if you gave a student name of anything more than 1 character you it would just flash the grade prompt but not take any input, because it is an error to try to put more than 1 character in a single character variable.

2) Your average calculation was inside your loop while summing.

3) Your average value was not passed by reference into your avg() so when you were getting an answer back it was some random value because it was actually displaying the average variable you declared in main() which was not declared. So I would read up on passing by reference a little if you really want to know how that works.

4) You were missing system("PAUSE"); at the bottom of main() just before the return so it would just flash your answer and then close the program.

Hope this works for you and the comments help you learn what was wrong. cyaz.

commented: Very clear help and care to deliver precise information .. all the best +1
Be a part of the DaniWeb community

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