Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11830 > unrolled thread

Can't see to pass the lowestScore variable to the Main function.

Started byRussell Stanford <russell.stanford@g.austincc.edu>
First post2011-11-25 16:20 -0800
Last post2011-11-25 17:12 -0800
Articles 4 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  Can't see to pass the lowestScore variable to the Main function. Russell Stanford <russell.stanford@g.austincc.edu> - 2011-11-25 16:20 -0800
    Re: Can't see to pass the lowestScore variable to the Main function. jt@toerring.de (Jens Thoms Toerring) - 2011-11-26 00:58 +0000
    Re: Can't see to pass the lowestScore variable to the Main function. Richard Damon <news.x.richarddamon@xoxy.net> - 2011-11-25 20:04 -0500
    Re: Can't see to pass the lowestScore variable to the Main function. red floyd <no.spam.here@its.invalid> - 2011-11-25 17:12 -0800

#11830 — Can't see to pass the lowestScore variable to the Main function.

FromRussell Stanford <russell.stanford@g.austincc.edu>
Date2011-11-25 16:20 -0800
SubjectCan't see to pass the lowestScore variable to the Main function.
Message-ID<4b774690-2897-4cbf-9c09-af4492e9bd78@q9g2000yqe.googlegroups.com>
Can anyone help me figure out why the lowestScore variable is not
passed to the Main function. I can get it to return in one function
but not two functions. Any suggestions, I am sure this is a no
brainer, but I have been fiddling with this for too long now.


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

// Function prototypes
// because they happen before the main function they need a prototype.
void getScore(int &);
double calcAverage(int, int, int, int, int, int &);
int  findLowest (int, int, int, int, int, int);

//***************************************************************
//
//  Function:     main
//
//  Description:  The main function of the program
//
//  Parameters:   None
//
//  Returns:      Zero (0)
//
//**************************************************************
int main()
{
   int test1, test2, test3, test4, test5, test6;  // 6 user inputs
   double average;
   int lowestScore;

   cout << fixed
        << showpoint
        << setprecision(2);

   // Call getScore once for each test score to be input
   getScore(test1);
   getScore(test2);
   getScore(test3);
   getScore(test4);
   getScore(test5);
   getScore(test6);

   // Call calcAverage to calculate and display the average
   average = calcAverage(test1, test2, test3, test4, test5, test6);

   cout << "\nAfter dropping the lowest test score of " <<
lowestScore;
   cout << ", the test average is "
	    << average
        << "\n\n";

   system("PAUSE");
   return 0;
}// end of main function

//***************************************************************
//
//  Function:     getScore
//
//  Description:  This function is called by main once for
//                each test score to be input and validated.
//                The score is stored in a reference parameter.
//
//  Parameters:   Test score
//
//  Returns:      Zero (0)
//
//**************************************************************
void getScore(int &score)
{
     cout << "Enter the test score (0 - 100): " << endl;
     cin >> score;

}// end of function getScore

//***************************************************************
//
//  Function:     calcAverage
//
//  Description:  This function is called by main to find
//                and print the average of the best 5 out
//                of the 6 scores passed to it as arguments.
//
//  Parameters:   The six test scores
//
//  Returns:      N/A
//
//**************************************************************
double calcAverage(int s1, int s2, int s3, int s4, int s5, int &s6)
{
	double avg      = 0;

     // call the find lowest function
     lowestScore = findLowest(s1, s2, s3, s4, s5, s6);
     cout << "Subtract the lowestScore which calcAverage = " <<
lowestScore << endl;

     // calculate the average grade dropping the lowest
     avg = ((s1 + s2 + s3 + s4 + s5 + s6) - lowestScore) / 5;

    // return the average in this variable.
    return avg;
}// end of function calcAverage

//***************************************************************
//
//  Function:     findLowest
//
//  Description:  This function is called by calcAverage to
//                determine which of the 6 scores passed to it
//                as arguments is the lowest.  The lowest score
//                is returned.
//
//  Parameters:   The six test scores
//
//  Returns:      Lowest
//
//**************************************************************
int findLowest(int s1, int s2, int s3, int s4, int s5, int s6)
{
     // the the lowest test score to the first test
	int lowest = s1;

     // if score 1 is greater than score 2 then set the lowest to
score 2
     if (s1 > s2)
     {
        lowest = s2;
        cout << " s1 = " << s1  << " and is > s2 = " << s2 << endl;
     }
     if (s2 > s3)
     {
        lowest = s3;
        cout << " s2 = " << s2  << " and is > s3 = " << s3 << endl;
     }
     if (s3 > s4)
     {
        lowest = s4;
        cout << " s3 = " << s3  << " and is > s4 = " << s4 << endl;
     }
     if (s4 > s5)
     {
        lowest = s5;
        cout << " s4 = " << s4  << " and is > s5 = " << s5 << endl;
     }
     if (s5 > s6)
     {
        lowest = s6;
        cout << " s5 = " << s5  << " and is > s6 = " << s6 << endl;
     }
     cout << "The lowest is =  " << lowest << endl;

     // returns the lowest value
	return lowest;

}// end of function findLowest

[toc] | [next] | [standalone]


#11836

Fromjt@toerring.de (Jens Thoms Toerring)
Date2011-11-26 00:58 +0000
Message-ID<9jartsFedlU1@mid.uni-berlin.de>
In reply to#11830
Russell Stanford <russell.stanford@g.austincc.edu> wrote:
> Can anyone help me figure out why the lowestScore variable is not
> passed to the Main function. I can get it to return in one function
> but not two functions. Any suggestions, I am sure this is a no
> brainer, but I have been fiddling with this for too long now.

<code snipped>

Perhaps you should first figure out why your program doesn't
compile. The most revealing one is a complaint of the compiler
about 'lowestScore' being not declared in the calcAverage()
function. It's defined in main() (not Main()) and thus it exists
only in that function (main()) and nowhere else.

Out of curiosity, why do you pass the first five arguments to
calcAverage() as ints but the last one as a reference?

Are you aware of the difference between normal and integer di-
vision? If you're not the line

  avg = ((s1 + s2 + s3 + s4 + s5 + s6) - lowestScore) / 5;

from calcAverage() will be the next one that will get you into
trouble: you add (and subtract) a bunch of integers, resulting
in another integer, which you then divide by an integer (and
assign the resuly to a floating point variable). Do you know
that the result of e.g.

   17 / 5

will be 3 even if you assign it to a floating point variable?
You might expect to get 3.4 but that's not what you will get
because dividing an integer by an integer will always result
in another integer (the one you will get when you through away
everything after the decimal point). If you want a floating point
result you need at least the divisor or the dividend to be a floa-
ting point value, i.e.

   17.0 / 5  or  17 / 5.  (or 17. / 5.0)

Assigning the result of an integer division" to a floating
point variable won't change anything about the trunation to
an integer that already happens when the division of two
integers is done.
                             Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

[toc] | [prev] | [next] | [standalone]


#11838

FromRichard Damon <news.x.richarddamon@xoxy.net>
Date2011-11-25 20:04 -0500
Message-ID<japdvk$jp4$1@dont-email.me>
In reply to#11830
On 11/25/11 7:20 PM, Russell Stanford wrote:
> Can anyone help me figure out why the lowestScore variable is not
> passed to the Main function. I can get it to return in one function
> but not two functions. Any suggestions, I am sure this is a no
> brainer, but I have been fiddling with this for too long now.
> 
> 

Why are you expecting it to make it to main?

You explicitly return it from findlowest, and calcAverage gets that
value and saves it by


lowestScore - findLowest(s1, s2, s3, s4, s5, s6);

but calcAverage doesn't ever send it back to main.

Note, you should be getting a diagnostic (or you made a copy/paste
error) in calcAverage, because it doesn't declare the variable
lowestScore, which is uses.

[toc] | [prev] | [next] | [standalone]


#11840

Fromred floyd <no.spam.here@its.invalid>
Date2011-11-25 17:12 -0800
Message-ID<japedm$kuc$1@dont-email.me>
In reply to#11830
On 11/25/2011 4:20 PM, Russell Stanford wrote:

>
> // Function prototypes
> // because they happen before the main function they need a prototype.
> void getScore(int&);
> double calcAverage(int, int, int, int, int, int&);
> int  findLowest (int, int, int, int, int, int);
>

In addition to the comments addressing your question,
the comment here is inaccurate and incorrect.

Incorrect documentation is worse than no documentation.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web