without messing with the class, cause i cant modify it , i was wondering if there is a way to return thefunction calcscore as a float. I cant modify the class, where i call the function

"This contestants average score was..." it always just returns a .000 is there a way to pass it as a float without messin with the class?

//Jeremy Rice
// CSCI 111
// FALL 2007

#include <iomanip>  // for setw(), setprecision()
#include "Stat.h"
#include <iostream>
using namespace std;
//Function Prototypes
void getJudgeData(int &);
double calcScore(Stat s);

const unsigned W=25; // argument for setw()
const unsigned P= 3; // argument for setprecision()

void showStat( Stat ); // displays statistics from Stat object

int main()
{
    //Initilizing variables and class
    Stat s;
    
    int score1 = 0, score2 = 0, score3=0, score4=0, score5=0;
    int x = 1;
    double total = 0;
    double winner = 0;
    double contest;
    char z;

cout << "This program assignment number #4"<<endl;
cout<< "THE NEXT AMERICAN IDOL"<<endl;
cout<< "By Jeremy Rice of CSCI 111"<<endl;
cout<<endl;

cout<< "Please enter your scores below!"<<endl;
//Ask each of the Judges for a score from 1-10
//anything less than 0 and greater than 10 will not be accepted

 
do
 {    
      s.reset();
cout<< "Enter contestant #"<<x<< " scores!"<<endl;

cout<<setw(W)<<"\tJudge #1 what is your score [0...10] : ";
               getJudgeData(score1);  //calling function for score1
               s.setNum(score1);
cout<<setw(W)<<"\tJudge #2 what is your score [0...10] : ";
               getJudgeData(score2);  //score2
               s.setNum(score2);
cout<<setw(W)<<"\tJudge #3 what is your score [0...10] : ";
               getJudgeData(score3); //score3
               s.setNum(score3);
cout<<setw(W)<<"\tJudge #4 what is your score [0...10] : ";
               getJudgeData(score4); //score4 
               s.setNum(score4);
cout<<setw(W)<<"\tJudge #5 what is your score [0...10] : ";
               getJudgeData(score5); //score5
               s.setNum(score5);    //storying it into class
  
cout<<"Talent score for contestant #"<<x<<" is: ";
cout<<setprecision(P)<< setw(W+P+1) <<fixed <<calcScore(s)<<endl;
 
 
// Initilizing total to the Average
// This is to determine the winner
// If total is greater than winner set the winner as total
// and the winning contestant to variable x                    
                
total = calcScore(s);
  
if (total > winner )
  {
  winner = total;
  contest = x;
  }
// increase variable for the next contestant   
 x++;         
  
  cout<<"Is there another contestant? (Y/N): ";
  cin>>z;
  
//Asking if there is another contestant  
}while ((z == 'y')||(z == 'Y'));

cout<< "The Next American Idol is Contestant "<<contest<<"!!!"<<endl;
cout<< "With a winning score of "<<winner<<"!!!"<<endl; 
  system("pause");
  return 0;

}
// If judge score is less then 0 and grester than 10 reask for score
//return number to score1-5

void getJudgeData(int &getNum)
{
     Stat s;
    cin>> getNum;
while( (getNum < 0 ) || (getNum > 10))
       {
        cout<<"That is not a digit between 0 and 10"<<endl;
        cout<< "Please re-enter your score: ";
        cin>>getNum;
       }
                    return;
}
//calculating average
//average is the sum subtracted by the min and max of the stat class
double calcScore(Stat s)
{
     float avg = 0;
     avg = (s.getSum() - s.getMax() - s.getMin()) / 3;
     return avg;
      
     
}
Ancient Dragon commented: Thanks for using code tags correctly :) +20

1. getScore() is supposed to return a double, but is actually returning a float. If you want it to return a double then declare avg as a double.

2. The calculation is doing integer arithmetic because 3 is an integer. If you want it to do floating point then it needs to be 3.0. Since we don't know what those functions from class Stat return you should probably typecast the whole thing to a double.

avg = (double)(s.getSum() - s.getMax() - s.getMin()) / 3.0);

seems to run ok when i go this

avg = (float)(s.getSum() - s.getMax() - s.getMin()) / 3.0);

i can compile it on my schools unix server without any problems and returns the decimal at the end... that would work or like later will someone mess up haha

A float does not have the precision of a double so if you give that function a large enough number typecasting the float to a double will fail. Again, if the function is supposed to return a double then return a double not a float. Your compiler does not complain because there promoting a float to a double is not a problem -- its the other way around that's the problem.

If float is really what you want then make the function's return type a float and not a double. It's a matter of consistency. Just because it compiles cleanly and works (for now) doesn't make it right.

Requirement for homework is a double calcScore function, and i think it will work for this program because the number will never be larger than 10...

>>and i think it will work for this program because the number will never be larger than 10...

You are missing the point!! Since its supposed to return a double than make the dam thing a double and not a float.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.