954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ drop lowest score

I can not seem to figure out how to drop the lowest score. When I run it it says the lowest score dropped was 0. How do I fix this?

#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 ;

cout << "For each student, you will be prompted for a name and "<< NUMBER << " test scores\n";
while ( moreStudents =='y' || moreStudents == 'Y') 

{ 
who = getName () ;
testAverage = averageScores ( NUMBER);
printMessage ( who, testAverage ); 
cout << "\n\nMore students ? Y or N \t\t" ;
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 << "\nEnter student name " ;
getline ( cin, name);
return name ;
} 
////////////////////////////////////////…
// this function inputs and averages scores. Parameter howmany tells the 
// number of scores to be input.

int averageScores( int howmany )
{
int sum;
int score1, score2, score3, score4 ;
double decimalAverage;
int average ; // integer average 
int low;

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

cout << "The test scores enetered are: " << score1 << " " << score2 << " " << score3 << " " << score4 << endl;
sum = score1 + score2 + score3 + score4;


if (low > score1)
{
low = score1;

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

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

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

}

cout << "The lowest score of " << low << " is dropped." << endl;
average = (sum - low) / 3;

cout << "The average with the lowest score dropped is " << average << "." << endl;
return average; 
} // 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 << "Average for " << name << " is " << average << " \t\t" << message; 

} //printMessage
plang007
Junior Poster in Training
64 posts since Feb 2011
Reputation Points: 5
Solved Threads: 0
 

Putting this in CODE tags would make this a lot easier to read...

MasterGberry
Posting Whiz in Training
246 posts since Nov 2010
Reputation Points: 7
Solved Threads: 8
 

how do you put it in code tags. I am new to this website.

plang007
Junior Poster in Training
64 posts since Feb 2011
Reputation Points: 5
Solved Threads: 0
 

Click Edit on your post, then highlight all of your code, and push the "CODE" button on the options above.

MasterGberry
Posting Whiz in Training
246 posts since Nov 2010
Reputation Points: 7
Solved Threads: 8
 

Here is the problem.

int low;  // You never set low equal to any value

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

cout << "The test scores enetered are: " << score1 << " " << score2 << " " << score3 << " " << score4 << endl;
sum = score1 + score2 + score3 + score4;
low = score1; // set low equal to the first score
// now test the rest of them and change if needed.
if(low > score2)
  low = score2;
if(low > score3)
  low = score3;
if(low > score4)
  low = score4;
MasterGberry
Posting Whiz in Training
246 posts since Nov 2010
Reputation Points: 7
Solved Threads: 8
 

I can not seem to figure out how to drop the lowest score. When I run it it says the lowest score dropped was 0. How do I fix this?

#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 ;

cout << "For each student, you will be prompted for a name and "<< NUMBER << " test scores\n";
while ( moreStudents =='y' || moreStudents == 'Y') 

{ 
who = getName () ;
testAverage = averageScores ( NUMBER);
printMessage ( who, testAverage ); 
cout << "\n\nMore students ? Y or N \t\t" ;
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 << "\nEnter student name " ;
getline ( cin, name);
return name ;
} 
////////////////////////////////////////…
// this function inputs and averages scores. Parameter howmany tells the 
// number of scores to be input.

int averageScores( int howmany )
{
int sum;
int score1, score2, score3, score4 ;
double decimalAverage;
int average ; // integer average 
int low;

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

cout << "The test scores enetered are: " << score1 << " " << score2 << " " << score3 << " " << score4 << endl;
sum = score1 + score2 + score3 + score4;


if (low > score1)
{
low = score1;

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

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

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

}

cout << "The lowest score of " << low << " is dropped." << endl;
average = (sum - low) / 3;

cout << "The average with the lowest score dropped is " << average << "." << endl;
return average; 
} // 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 << "Average for " << name << " is " << average << " \t\t" << message; 

} //printMessage
plang007
Junior Poster in Training
64 posts since Feb 2011
Reputation Points: 5
Solved Threads: 0
 

Do you get any run time errors ? I didn't go all over youre code but it seems that variable low hasn't been initialized.

Crutoy
Junior Poster in Training
64 posts since Jan 2011
Reputation Points: 10
Solved Threads: 6
 

I did not get any run time errors

plang007
Junior Poster in Training
64 posts since Feb 2011
Reputation Points: 5
Solved Threads: 0
 

Read my post up above, you are not initializing the int variable low.

MasterGberry
Posting Whiz in Training
246 posts since Nov 2010
Reputation Points: 7
Solved Threads: 8
 

Thanks for all your help. I got it to work.

#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  ;
        
         cout << "For each student, you will be prompted for a name and "<< NUMBER << " test scores\n";
         while ( moreStudents =='y' || moreStudents == 'Y') 
         
         {               
            who = getName () ;
            testAverage = averageScores ( NUMBER);
            printMessage ( who, testAverage );       
            cout << "\n\nMore students ? Y or N \t\t" ;
            cin >> moreStudents ;
            cout << endl;
            cin.ignore (10, '\n');
         }      
         
    cout << endl;
    cout << "Driver Name: Peter Langlands" << endl;
    cout << "Navigator Name: Tyler Bazan" << endl;
    cout << "Date: Febrauary 10, 2011" << endl;
    cout << "Lab CRN 26682\n" << endl;     
         
         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 howmany tells the 
   // number of scores to be input.
   
   int  averageScores( int howmany )
   {
          int sum;
          int score1, score2, score3, score4 ;
          double decimalAverage;
          int average ;      // integer average 
          int low;
   
               cout << "Enter four interger scores with a space in between: " ;
               cin >> score1 >> score2 >> score3 >> score4 ;
               
               cout << "The 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 << "The lowest score of " << low << " is dropped." << endl;
          average = (sum - low) / 3;

          cout << "The average with the lowest score dropped is " << average << "." << endl;
          return average;  
  } // averageScores  /
  
  //////////////////////////////////////////////////////////////////////
  // adds to the appropriate count depeding on the score  
  void tallyScores ( int average, int& repeatIt,int& wow, int& passing)
plang007
Junior Poster in Training
64 posts since Feb 2011
Reputation Points: 5
Solved Threads: 0
 
#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  ;
        
         cout << "For each student, you will be prompted for a name and "<< NUMBER << " test scores\n";
         while ( moreStudents =='y' || moreStudents == 'Y') 
         
         {               
            who = getName () ;
            testAverage = averageScores ( NUMBER);
            printMessage ( who, testAverage );       
            cout << "\n\nMore students ? Y or N \t\t" ;
            cin >> moreStudents ;
            cout << endl;
            cin.ignore (10, '\n');
         }      
         
    cout << endl;
    cout << "Driver Name: Peter Langlands" << endl;
    cout << "Navigator Name: Tyler Bazan" << endl;
    cout << "Date: Febrauary 10, 2011" << endl;
    cout << "Lab CRN 26682\n" << endl;     
         
         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 howmany tells the 
   // number of scores to be input.
   
   int  averageScores( int howmany )
   {
          int sum;
          int score1, score2, score3, score4 ;
          double decimalAverage;
          int average ;      // integer average 
          int low;
   
               cout << "Enter four interger scores with a space in between: " ;
               cin >> score1 >> score2 >> score3 >> score4 ;
               
               cout << "The 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 << "The lowest score of " << low << " is dropped." << endl;
          average = (sum - low) / 3;

          cout << "The average with the lowest score dropped is " << average << "." << endl;
          return average;  
  } // 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 <<  "Average for  "  <<  name  << " is  " << average << " \t\t" << message; 
       
  }          //printMessage
plang007
Junior Poster in Training
64 posts since Feb 2011
Reputation Points: 5
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: