I need to enter 10 students names I did a loop but it does not work well

# include <iostream>
# include <string>

using namespace std; 

//functions prototype
void avg (double& average); 
char grade(double& average);

int main()
{	
	// String variable for the student's name
	string name; 

	double theAverage = 0; 	

	// Prompts the name of the student
	cout << "Student name: ";	

	//Reads from keyboard
	getline(cin, name); 

	// sends theAverage into the avg()	
	avg(theAverage);  	
	cout << "The average grade for " 
		 << name << " is " 
		 << theAverage << "."<<endl; 	
	cout << "And his/her grade is " << grade(theAverage) <<endl;
		
	return 0;
} 

//the average calculating function
void avg (double& average)
{	
	int grade[5];	
	double sum = 0; 
	cout << endl; 

	// The loop to get the grades and sum them 
	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 ends
	average = sum / 5;
}

char grade(double& average)
{
	
	if (average >=90 && average <=100) 
		 return 'A';

	if (average >=80 && average <90)
		 return 'B';

	if (average >=70 && average <80)
		 return 'C';

	if (average >=60 && average <70)
		return 'D';

	else 
	{
		return 'F';
	}
}

You could have one array[10] of strings that holds the name and another array[10] that holds the averages that come back from the function. Then just iterate to 10 with a cout that displays both name and average.

Encase the whole of main(minus your array declarations) in a for loop (or if you wanted to verify input use a while).

This article has been dead for over six months. Start a new discussion instead.