After your program works correctly for one student, add a loop so the user can process any number of students. Prompt the user for the number of students to process; use that value to control the loop.

*How do I prompt the user for the number of students to process and use that value to control the loop.

``````#include<iostream>
#include <string>
#include <cmath>

using namespace std ;

string getName ();
int averageScores( int);
void printMessage (string, int);
void tallyScores ( int,int&,int&,int&);

int main()
{
const int NUMBER = 4;        // number of scores for each student
char moreStudents = 'y';
string who;                 //name of the student
int testAverage;
int students;

cout << "For each student, you will be prompted for a name and "<< NUMBER << " test scores\n" << endl;

while ( moreStudents =='y' || moreStudents == 'Y'){

who = getName () ;
testAverage = averageScores ( NUMBER);
printMessage ( who, testAverage );
cout << "\n\nMore students? Y or N: " ;
cin >> moreStudents ;
cout << endl;
cin.ignore (10, '\n');
}

system ("pause");
return 0;
}     // end main
/////////////////////////////////////////////////////////////////////
// Prompts the user to input the student's name
string getName ()
{
string name ;

cout << "Enter student name " ;
getline ( cin, name);
return name ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// this function inputs and averages scores. Parameter sum adds the scores up.

int  averageScores( int sum )
{
int score1, score2, score3, score4 ;
int low;
int rounded;
float average;

cout << "\nEnter four interger scores with a space in between: " ;
cin >> score1 >> score2 >> score3 >> score4 ;

cout << "\nThe test scores enetered are: " << score1 << " " << score2 << " " <<         score3 << " " << score4 << endl;

sum = score1 + score2 + score3 + score4;

low = score1;

if(low > score2){
low = score2;
}

if(low > score3){
low = score3;

}
if(low > score4){
low = score4;

}

cout << "\nThe lowest score of " << low << " is dropped." << endl;

average = (sum - low) / 3.0;
cout << "\nThe average is " << average << " with three scores and the lowest score dropped." << endl;
rounded =  static_cast<int>(average + 0.5);
cout << "\nThis average is then rounded to " << rounded << "." << endl;
return rounded;
} // averageScores  /

//////////////////////////////////////////////////////////////////////
// adds to the appropriate count depeding on the score
void tallyScores ( int average, int& repeatIt,int& wow, int& passing)
{

if(average <60)
repeatIt++;

else if(average >= 95)
wow++;

else if(60 <= average < 95)
passing++;

}
/////////////////////////////////////////////////////
// Prints out a message for the student depending on the score
void printMessage (string name, int average)
{
string message ;
if(average <60)
message = ("Gotta repeat it");

else if(average >= 95)
message = ("WOW");

else if(60 <= average < 95)
message = ("passing");

cout <<  "\nAverage for "  <<  name  << " is " << average << "; " << message;

}          //printMessage``````

## All 9 Replies

``````#include<iostream>
#include <string>
#include <cmath>

using namespace std ;

string getName ();
int averageScores( int);
void printMessage (string, int);
void tallyScores ( int,int&,int&,int&);

int main()
{
const int NUMBER = 4;        // number of scores for each student
char moreStudents = 'y';
string who;                 //name of the student
int testAverage;
int students;

cout << "For each student, you will be prompted for a name and "<< NUMBER << " test scores\n" << endl;

while ( moreStudents =='y' || moreStudents == 'Y'){

who = getName () ;
testAverage = averageScores ( NUMBER);
printMessage ( who, testAverage );
cout << "\n\nMore students? Y or N: " ;
cin >> moreStudents ;
cout << endl;
cin.ignore (10, '\n');
}

system ("pause");
return 0;
}     // end main
/////////////////////////////////////////////////////////////////////
// Prompts the user to input the student's name
string getName ()
{
string name ;

cout << "Enter student name " ;
getline ( cin, name);
return name ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// this function inputs and averages scores. Parameter sum adds the scores up.

int  averageScores( int sum )
{
int score1, score2, score3, score4 ;
int low;
int rounded;
float average;

cout << "\nEnter four interger scores with a space in between: " ;
cin >> score1 >> score2 >> score3 >> score4 ;

cout << "\nThe test scores enetered are: " << score1 << " " << score2 << " " <<         score3 << " " << score4 << endl;

sum = score1 + score2 + score3 + score4;

low = score1;

if(low > score2){
low = score2;
}

if(low > score3){
low = score3;

}
if(low > score4){
low = score4;

}

cout << "\nThe lowest score of " << low << " is dropped." << endl;

average = (sum - low) / 3.0;
cout << "\nThe average is " << average << " with three scores and the lowest score dropped." << endl;
rounded =  static_cast<int>(average + 0.5);
cout << "\nThis average is then rounded to " << rounded << "." << endl;
return rounded;
} // averageScores  /

//////////////////////////////////////////////////////////////////////
// adds to the appropriate count depeding on the score
void tallyScores ( int average, int& repeatIt,int& wow, int& passing)
{

if(average <60)
repeatIt++;

else if(average >= 95)
wow++;

else if(60 <= average < 95)
passing++;

}
/////////////////////////////////////////////////////
// Prints out a message for the student depending on the score
void printMessage (string name, int average)
{
string message ;
if(average <60)
message = ("Gotta repeat it");

else if(average >= 95)
message = ("WOW");

else if(60 <= average < 95)
message = ("passing");

cout <<  "\nAverage for "  <<  name  << " is " << average << "; " << message;

}          //printMessage``````

instead of using a while loop ask the user for the number of students and then use that as the stop condition in a for loop

``````// in main
cout << "How many students do you want to enter: ":
cin >> numberOfStudents;
for (int i = 0; i < numberOfStudents; i++)
{
// code for getting students goes here
}
// more code``````

This is my new code, but when I compile it run it, it skips over the line where you have to enter a name. DO you know what to do?

``````#include<iostream>
#include <string>
#include <cmath>

using namespace std ;

string getName ();
int averageScores( int);
void printMessage (string, int);
void tallyScores ( int,int&,int&,int&);

int main()
{
const int NUMBER = 4;        // number of scores for each student
char moreStudents = 'y';
string who;                 //name of the student
int testAverage;
int students;

cout << "How many students do you want to enter: ";
cin >> students;
cout << "\nFor each student, you will be prompted for a name and "<< NUMBER << " test scores\n" << endl;

for (int i = 0; i < students; i++)
{
who = getName () ;
testAverage = averageScores ( NUMBER);
printMessage ( who, testAverage );
cin.ignore (10, '\n');
}

system ("pause");
return 0;
}     // end main
/////////////////////////////////////////////////////////////////////
// Prompts the user to input the student's name
string getName ()
{
string name ;

cout << "Enter student name " ;
getline ( cin, name);
return name ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// this function inputs and averages scores. Parameter sum adds the scores up.

int  averageScores( int sum )
{
int score1, score2, score3, score4 ;
int low;
int rounded;
float average;

cout << "\nEnter four interger scores with a space in between: " ;
cin >> score1 >> score2 >> score3 >> score4 ;

cout << "\nThe test scores enetered are: " << score1 << " " << score2 << " " << score3 << " " << score4 << endl;

sum = score1 + score2 + score3 + score4;

low = score1;

if(low > score2){
low = score2;
}

if(low > score3){
low = score3;

}
if(low > score4){
low = score4;

}

cout << "\nThe lowest score of " << low << " is dropped." << endl;

average = (sum - low) / 3.0;
cout << "\nThe average is " << average << " with three scores and the lowest score dropped." << endl;
rounded =  static_cast<int>(average + 0.5);
cout << "\nThis average is then rounded to " << rounded << "." << endl;
return rounded;
} // averageScores  /

//////////////////////////////////////////////////////////////////////
// adds to the appropriate count depeding on the score
void tallyScores ( int average, int& repeatIt,int& wow, int& passing)
{

if(average <60)
repeatIt++;

else if(average >= 95)
wow++;

else if(60 <= average < 95)
passing++;

}
/////////////////////////////////////////////////////
// Prints out a message for the student depending on the score
void printMessage (string name, int average)
{
string message ;
if(average <60)
message = ("Gotta repeat it");

else if(average >= 95)
message = ("WOW");

else if(60 <= average < 95)
message = ("passing");

cout <<  "\nAverage for "  <<  name  << " is " << average << "; " << message;

}          //printMessage``````

NathanOliver has a good point, just a for-loop.

Just in case you haven't done for-loops just yet, you can do basically the same thing with a while loop. Ask for the number of students, create an index for the current loop count, then increment the index once the loop ends. If the index is less than or equal to the amount of students, do the loop again; else the loop ends.

How would you write that as a while loop?

After line 21 write `cin.get()` . The problem comes from having mixed input. For more information check this out.

You need to read this thread. It will explain why you are having the error with the string input and it's function.

~~~~~~~~~~~~~~~~~~~~
For the while loop, I just explained it in a very detailed fashion.

~~~~~~~~~~~~~~~~~~~~~
Lastly, check out your expressions such as (60 <= average < 95). You can't do this with the expected results of normal arithmetic. You need to change this to (60 <= average && average < 95)

Nathan, I did what you said but it still skips the line where it asks fo student name, but then that line comes last. like this

Output:

How many students do you want to enter: 4

For each student, you will be prompted for a name and 4 test scores

Enter student name:
Enter four interger scores with a space in between: 99 97 75 86

The test scores enetered are: 99 97 75 86

The lowest score of 75 is dropped.

The average is 94 with three scores and the lowest score dropped.

This average is then rounded to 94.

Average for is 94; passing

Enter student name:

My code.

``````#include<iostream>
#include <string>
#include <cmath>

using namespace std ;

string getName ();
int averageScores( int);
void printMessage (string, int);
void tallyScores ( int,int&,int&,int&);

int main()
{
const int NUMBER = 4;        // number of scores for each student
char moreStudents = 'y';
string who;                 //name of the student
int testAverage;
int students;

cout << "How many students do you want to enter: ";
cin.get();
cout << "\nFor each student, you will be prompted for a name and "<< NUMBER << " test scores\n" << endl;

for(int i = 0; i < students; i++)
{
who = getName () ;
testAverage = averageScores ( NUMBER);
printMessage ( who, testAverage );
cin.ignore (10, '\n');
}

system ("pause");
return 0;
}     // end main
/////////////////////////////////////////////////////////////////////
// Prompts the user to input the student's name
string getName ()
{
string name ;

cout << "\nEnter student name: " ;
getline ( cin, name);
return name ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// this function inputs and averages scores. Parameter sum adds the scores up.

int  averageScores( int sum )
{
int score1, score2, score3, score4 ;
int low;
int rounded;
float average;

cout << "\nEnter four interger scores with a space in between: " ;
cin >> score1 >> score2 >> score3 >> score4 ;

cout << "\nThe test scores enetered are: " << score1 << " " << score2 << " " << score3 << " " << score4 << endl;

sum = score1 + score2 + score3 + score4;

low = score1;

if(low > score2){
low = score2;
}

if(low > score3){
low = score3;

}
if(low > score4){
low = score4;

}

cout << "\nThe lowest score of " << low << " is dropped." << endl;

average = (sum - low) / 3.0;
cout << "\nThe average is " << average << " with three scores and the lowest score dropped." << endl;
rounded =  static_cast<int>(average + 0.5);
cout << "\nThis average is then rounded to " << rounded << "." << endl;
return rounded;
} // averageScores  /

//////////////////////////////////////////////////////////////////////
// adds to the appropriate count depeding on the score
void tallyScores ( int average, int& repeatIt,int& wow, int& passing)
{

if(average <60)
repeatIt++;

else if(average >= 95)
wow++;

else if(60 <= average < 95)
passing++;

}
/////////////////////////////////////////////////////
// Prints out a message for the student depending on the score
void printMessage (string name, int average)
{
string message ;
if(average <60)
message = ("Gotta repeat it");

else if(average >= 95)
message = ("WOW");

else if(60 <= average < 95)
message = ("passing");

cout <<  "\nAverage for "  <<  name  << " is " << average << "; " << message << endl;

}          //printMessage``````

I had meant for you to do this

``````//...
cout << "How many students do you want to enter: ";
cin >> students;
cin.get();  // or cin.ignore (80, '\n');
cout << "\nFor each student, you will be prompted for a name and "<< NUMBER << " test scores\n" << endl;
/...``````
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.