You are here:

C++/c++ drop lowest score

Advertisement


Question
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

Answer
I presume this is the relevant part of the load of code you sent with your question:

       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;

       }


Sorry if I guessed wrong but you did not exactly flag it or any other bits as the part(s) of interest for me did you?

OK here is a question:

What sort of variable is low?

Yes I know it is an int type but what is its storage class?

Indeed it is a local automatic variable - that is it is local to a call of a function and is allocated on the function call's stack frame.

In C and C++ what value are such variables initialised to if they are not explicitly initialised to a value? (As is the case here:

         int low;

)

That is correct no specific  value - they pick up whatever crud was in the memory they are allocated, i.e. to all intents and purposes they contain random rubbish. In this case I suspect your low variable acquires (by luck) the value zero.

If you replace low with 0 in your if statements' conditions will low be set to any value other than 0 if all the values entered are greater or equal to 0?

That is, assuming initially low == 0 then:

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

would be equivalent to:

       if (0 > score1)
       {
         low = score1;
       }

etc.

Hence would any of the if statements' conditions be true if score1, score2, score3 and score4 all have values of 0 or more?

Now that is all I am going to say at this stage as I hope there are enough hints in my answer to get you to see the problem. Please have a go at working out a solution to fix the problem.

If you really cannot see a solution then please post a follow up question - preferably with details of what you have tried and what you seem suck with. If possible please try to reduce the quantity of code you have to post.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.