Troubles with making functions work, StarSearch Project

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3104 Views - Last Post: 14 November 2012 - 09:39 AM Rate Topic: -----

#1 phanton612   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-November 12

Troubles with making functions work, StarSearch Project

Posted 13 November 2012 - 10:44 PM

Class C++ Programming 1 (new to C++), and first time posting on this website

The problem is asking

talent competition has 5 judges, each of whom awards a score between 0 and 10 to each performer. Fractional scores, such as 8.3 are allowed. A performer's final score is determined by dropping the highest and lowest score received, then averaging the 3 remaining scores. Write a program that uses this method to calculate a contestant's score. It should include the following functions:

void getJudgeData() -- This should ask the user for a judge's score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the 5 judges.

void calcScore() -- This should calculate and display the average of the 3 scores that remain after dropping the highest and lowest scores the performer received. This function should be called just once by main, and should be passed the 5 scores.

The last two functions, described below, should be called by calcScore, which uses the returned information to determine which of the scores to drop.

int findLowest() -- This should find and return the lowest of the 5 scores passed to it.

int findHighest() -- This should find and return the highest of the 5 scores passed to it.

Finally, Input Validation: Do not accept judge scores lower than 0 or higher than 10.


  • The teacher also wants do/while loop for input validation
  • have the int maiin, and four other functions
  • use pass by value and pass by reference as needed.




For some reason it won't compile at ( score1 = getJudgeData(); ) Did I do something wrong with the code?

/*******************************************************************************
name        =   name
date        =   11/7/2012
Program     =   name_Starsearch.cpp
assignment  =   challenge #11
Purpose     =   Write a program that uses this method to calculate a
                contestant's score.
*******************************************************************************/
#include <iostream>
using namespace std;

void getJudgeData();
void calcscore();
int findlowest();
int findhighest();

double lowNum, highNum, num1, num2, num3;
double score1, score2, score3, score4, score5;

int main()
{
    double score1, score2, score3, score4, score5;
    
    score1 = getJudgeData();
    score2 = getJudgeData();
    score3 = getJudgeData();
    score4 = getJudgeData();
    score5 = getJudgeData();
    
    cout << " Judge One score is : " << score1 << endl;    
    cout << " Judge Two score is : " << score2 << endl;
    cout << " Judge Three score is : " << score3 << endl;
    cout << " Judge Four score is : " << score4 << endl;
    cout << " Judge Five score is : " << score5 << endl;






    system("pause");
	return 0;
}

////////////////////////////////////////////////////////////////////////////////
void getJudgeData()
{
   double Score;
   do{
        
    cout << " A Judges score is: "
    cin >> Score
    }while((Score<=0)&&(Score>=10))
    
    return Score
};

////////////////////////////////////////////////////////////////////////////////

void calcscore()
{

}




I tried putting (score1 = getJudgeData(Score);)/> hoping that'd do it but no :(/>/>


Please help, been staring at this thing for over 5 hours.

Is This A Good Question/Topic? 0
  • +

Replies To: Troubles with making functions work, StarSearch Project

#2 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Troubles with making functions work, StarSearch Project

Posted 13 November 2012 - 11:19 PM

Your missing a whole bunch of semi colons

void getJudgeData()
{
   double Score;
   do{
        
    cout << " A Judges score is: " / <---
    cin >> Score //<---
    }while((Score<=0)&&(Score>=10)) //<---
    
    return Score //<---
};  //<-- Don't need semicolon


In addition, you cannot return a value from function of type void. I'm guessing you mean't to declare this function to return type double.

This post has been edited by jjl: 13 November 2012 - 11:20 PM

Was This Post Helpful? 1
  • +
  • -

#3 phanton612   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-November 12

Re: Troubles with making functions work, StarSearch Project

Posted 13 November 2012 - 11:30 PM

Quote

In addition, you cannot return a value from function of type void. I'm guessing you mean't to declare this function to return type double.




Thanks a lot, I added the semi's and changed getJudgeData() to a double and it now compiles good, but is there a way to keep it a void? because the question says the ( it should include the following functions: void getJudgeData() ....) or should i just keep it a double and hope to get away with it?
Was This Post Helpful? 0
  • +
  • -

#4 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Troubles with making functions work, StarSearch Project

Posted 13 November 2012 - 11:45 PM

If you cannot return a value, then you need to pass a parameter by reference to get the score.

example of passing by reference
void getInput(int &x) {
	std::cin>>x;
}

int main() {
   int x;
   getInput(x);
   std::cout<<"You entered "<<x<<std::endl;
   
   return 0;
}



More on passing by reference can be found here:
http://www.learncpp....s-by-reference/

Quote

or should i just keep it a double and hope to get away with it?

No, I think the point of that function restraint is to learn "passing by reference"

This post has been edited by jjl: 13 November 2012 - 11:46 PM

Was This Post Helpful? 0
  • +
  • -

#5 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Troubles with making functions work, StarSearch Project

Posted 14 November 2012 - 12:41 AM

Quote

or should i just keep it a double and hope to get away with it?


Yeah, right! ;)

ROFL!!
Was This Post Helpful? 0
  • +
  • -

#6 phanton612   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-November 12

Re: Troubles with making functions work, StarSearch Project

Posted 14 November 2012 - 12:41 AM

/*******************************************************************************
name        =   name
date        =   11/7/2012
Program     =   name_Starsearch.cpp
assignment  =   challenge #11
Purpose     =   Write a program that uses this method to calculate a
                contestant's score.
*******************************************************************************/
#include <iostream>
using namespace std;
double getJudgeData();
void calcscore();
int findlowest();
int findhighest();

double Score, Score1, Score2, Score3, Score4, Score5;

int main()
{    
    getJudgeData();    
    
    cout << "\n s3 "<< Score3 << " test\n\n\n";
     



    system("pause");
	return 0;
}

////////////////////////////////////////////////////////////////////////////////
double getJudgeData()
{

	double Score, Score1, Score2, Score3, Score4, Score5;
	do{
        cout << "Enter the judge One score: ";
	    cin >> Score1;

	if((Score<=0)||(Score>=10))	
	    cout << "\nThe Score has to be 0 or greater and has to be either 10 or less.";
	}while((Score<=0)&&(Score>=10));
	
	    
    do{
        cout << "Enter the judge Two score: ";
	    cin >> Score2;

	if((Score2<=0)||(Score2>=10))	
	    cout << "\nThe Score has to be 0 or greater and has to be either 10 or less.";
	}while((Score2<=0)&&(Score2>=10));
	
	
    
    do{
        cout << "Enter the judge Three score: ";
	    cin >> Score3;

	if((Score3<=0)||(Score3>=10))	
	    cout << "\nThe Score has to be 0 or greater and has to be either 10 or less.";
	}while((Score3<=0)&&(Score3>=10));
	
	
	do{
        cout << "Enter the judge Four score: ";
	    cin >> Score4;

	if((Score4<=0)||(Score4>=10))	
	    cout << "\nThe Score has to be 0 or greater and has to be either 10 or less.";
	}while((Score4<=0)&&(Score4>=10));
	
	
	do{
        cout << "Enter the judge Five score: ";
	    cin >> Score5;

	if((Score5<=0)||(Score5>=10))	
	    cout << "\nThe Score has to be 0 or greater and has to be either 10 or less.";
	}while((Score5<=0)&&(Score5>=10));
	
	
			     
	return Score, Score1, Score2, Score3, Score4, Score5; 
	
}

////////////////////////////////////////////////////////////////////////////////

void calcscore()
{

}




can someone tell/show me how to make it so the variables in the (double getJudgeData()) can transfer back to the int main()
Was This Post Helpful? 0
  • +
  • -

#7 phanton612   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-November 12

Re: Troubles with making functions work, StarSearch Project

Posted 14 November 2012 - 01:03 AM

Well I stared down this thing for atleast 10 hours plus, i'm going to sleep and hope i can some how figure it out before i go to college tomorrow. :(
Was This Post Helpful? 0
  • +
  • -

#8 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: Troubles with making functions work, StarSearch Project

Posted 14 November 2012 - 01:51 AM

In this situation you can use those globals you have, can't you? It's usually not a good practice to use globals - but since you can't change that code, then them's the rules.
But since you shadow those global variable names with local variable names in your function body... What would be the solution, think for a minute. ;)

This post has been edited by Xupicor: 14 November 2012 - 01:53 AM

Was This Post Helpful? 0
  • +
  • -

#9 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Troubles with making functions work, StarSearch Project

Posted 14 November 2012 - 02:05 AM

View Postphanton612, on 14 November 2012 - 01:03 AM, said:

Well I stared down this thing for atleast 10 hours plus, i'm going to sleep and hope i can some how figure it out before i go to college tomorrow. :(/>/>


jjl showed you how to do this in #4.


Call it:

getJudgeData(score1);

then, receive score1 as a reference

void getJudgeData(&score1) {
//your code in here to get the data into score1

}
Since you need to call getJudgeData 5 times according to the assignment you posted, you probably don't want 5 separate variables, but an array scores[5], so you can do it all in a loop:

[code]
int scores[5];
for(i=0;i<5;i++) {
   getJudgeData(scores[i]);


then
void getJudgeData(&scores[i]) {
   //your code here
}



I'm not sure if you need to use () around the array references in C++. I'm a C guy, just trying to help out.

This post has been edited by Adak: 14 November 2012 - 02:06 AM

Was This Post Helpful? 0
  • +
  • -

#10 phanton612   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-November 12

Re: Troubles with making functions work, StarSearch Project

Posted 14 November 2012 - 05:43 AM

[quote name='Adak' date='14 November 2012 - 02:05 AM' timestamp='1352883929' post='1746006']

View Postphanton612, on 14 November 2012 - 01:03 AM, said:

Well I stared down this thing for atleast 10 hours plus, i'm going to sleep and hope i can some how figure it out before i go to college tomorrow. :(/>/>/>/>/>


jjl showed you how to do this in #4.


Call it:

getJudgeData(score1);

then, receive score1 as a reference

void getJudgeData(&score1) {
//your code in here to get the data into score1

}
Since you need to call getJudgeData 5 times according to the assignment you posted, you probably don't want 5 separate variables, but an array scores[5], so you can do it all in a loop:

[code]
int scores[5];
for(i=0;i<5;i++) {
   getJudgeData(scores[i]);


then
void getJudgeData(&scores[i]) {
   //your code here
}



I'm just confused on is the functions main body? or would this go in the int main()
then
void getJudgeData(&scores[i]) {
   //your code here
}

Was This Post Helpful? 0
  • +
  • -

#11 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: Troubles with making functions work, StarSearch Project

Posted 14 November 2012 - 06:27 AM

Wait, are you building the whole program by yourself, or are you just filling in gaps? If the latter, then globals seems like the only way, if the former, then just pass things to functions and don't bother with globals.

Read tutorials about functions and arrays.

This post has been edited by Xupicor: 14 November 2012 - 06:28 AM

Was This Post Helpful? 0
  • +
  • -

#12 phanton612   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-November 12

Re: Troubles with making functions work, StarSearch Project

Posted 14 November 2012 - 06:45 AM

I am suppose to write a program from scratch in (Dev c++) and I am in dire need of help.


I have changed this code around tons and tons of time according to what people were saying and just being straight up confused, now i have less than 2 hours before it's due.

i ended up rewriting the code for probably the last time, i know it does not work and i am hoping someone can help out.

Please if possible and be straight up and show what needs to be done, rather than tell me to go on a 30 min hunt for info on the web that I'd end up prob not finding (actually don't have the time)


right now it says it won't compile at the line with ( get judgedata (s1) )

#include <iostream>
using namespace std;


void getjudgedata(int & score);
void calcscore();
int findlowest();
int findhighest();



int main()
{ 
int s1, s2, s3, s4, s5;


get judgedata (s1);
get judgedata (s2);
get judgedata (s3);
get judgedata (s4);
get judgedata (s5);




    system("pause");
	return 0;
}


////////////////////////////////////////////////////////////////////////////////
void getjudgedata(int & score) 
{
do{
    cout << "\n Enter A judges score";
    cin >> score;
    
    if((score<0)||(score>10);
        cout << " score must be a score of 0 and 10 or between.";
}while((score<0)||(score>10));

return score;

}	 
////////////////////////////////////////////////////////////////////////////////

void calcscore( int s1, s2, s3 ,s4, s5, int & ave)
{
int tot;
tot= s1+s2+s3+S4+S5;
tot-=lowest(s1,s2,s3,s4,s5);
tot-=highest(s1,s2,s3,s4,s5);
ave=tot/3;

}


Was This Post Helpful? 0
  • +
  • -

#13 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: Troubles with making functions work, StarSearch Project

Posted 14 November 2012 - 07:15 AM

You are a bit stressed, I wager. Do you not see the space character that does not belong there?
get judgedata (s1);
   ^
what is that?

Was This Post Helpful? 0
  • +
  • -

#14 phanton612   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-November 12

Re: Troubles with making functions work, StarSearch Project

Posted 14 November 2012 - 07:56 AM

View PostXupicor, on 14 November 2012 - 07:15 AM, said:

You are a bit stressed, I wager. Do you not see the space character that does not belong there?
get judgedata (s1);
   ^
what is that?

you're a life saver :)/>

lol thanks, ya i got it to work now, though my math is off on the average don't exactly know why. Though my teacher won't care to much about that( he just wants to make sure we're getting the functions right.


Final
#include <iostream>
using namespace std;


void getjudgedata(int & score);
void calcscore();
int findlowest();
int findhighest();



int main()
{ 
    int s1, s2, s3, s4, s5, ave, lowest, highest;
    int findlowest();
    int findhighest(); 
    double tot;
    
    
    getjudgedata (s1);           
    getjudgedata (s2);
    getjudgedata (s3);
    getjudgedata (s4);
    getjudgedata (s5);
    
    tot = (((s1 + s2 + s3 + s4 + s5) - lowest)- highest);         
    void calcscore();
    cout << "\n\n The Average of the Scores is " << ave << endl << endl; 

    system("pause");
	return 0;
}


////////////////////////////////////////////////////////////////////////////////
void getjudgedata(int & score) 
{
do{
    cout << "\n Enter A judges score: ";
    cin >> score;
    

            if((score<0)||(score>10))
                cout << "score must be a score of 0 and 10 or between.";                     
}while((score<0)||(score>10));

}
	 
////////////////////////////////////////////////////////////////////////////////

void calcscore()
{

int findlowest;
int findhighest;
int tot, lowest, highest, ave;


ave = tot/3;



}

////////////////////////////////////////////////////////////////////////////////

int findlowest()
{
 
        int s1, s2, s3, s4, s5, lowest;

        if((s1<s2)&&(s1<s3)&&(s1<s4)&&(s1<s5))
        {
            s1=lowest;
        }   
         else
            if((s2<s1)&&(s2<s3)&&(s2<s4)&&(s1<s5))
                { 
                s2=lowest;
                }    
                else
                    if((s3<s1)&&(s3<s2)&&(s3<s4)&&(s3<s5))
                        {    
                            s3=lowest;
                        }
                            else
                                if((s4<s1)&&(s4<s2)&&(s4<s3)&&(s4<s5))
                                {
                                    s4=lowest;
                                }
                                    else
                                        if((s5<s1)&&(s5<s2)&&(s5<s3)&&(s5<s4))
                                         {
                                            s5=lowest;
                                        }  
                                
                                
                               
                                              
    return lowest;

}

////////////////////////////////////////////////////////////////////////////////
int findhighest()
 {
        int s1, s2, s3, s4, s5, highest;
        
        if((s1>s2)&&(s1>s3)&&(s1>s4)&&(s1>s5))
            s1=highest;
            else
                if((s2>s1)&&(s2>s3)&&(s2>s4)&&(s1>s5))
                    s2=highest;
                    else
                        if((s3>s1)&&(s3>s2)&&(s3>s4)&&(s3>s5))
                            s3=highest;
                            else
                                if((s4>s1)&&(s4>s2)&&(s4>s3)&&(s4>s5))
                                    s4=highest;
                                    else
                                        if((s5>s1)&&(s5>s2)&&(s5>s3)&&(s5>s4))
                                            s5=highest;
                                            
                                            
    return highest;

}

Was This Post Helpful? 0
  • +
  • -

#15 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: Troubles with making functions work, StarSearch Project

Posted 14 November 2012 - 08:16 AM

Sorry, but bad news is - you're not getting it yet. You really should get to read those tutorials on functions in Basic C++ Tutorials section of DIC.

Most of your functions are not making sense, really. It won't do if we just fix it up for you. Grades aren't as important as understanding, but I think in this case they will reflect it. The good news is, once you get the latter (not that hard, really) you will raise the former.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2