9 Replies - 1598 Views - Last Post: 25 October 2009 - 05:19 PM Rate Topic: -----

#1 chris0812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-September 09

Void functions

Post icon  Posted 25 October 2009 - 08:40 AM

Getting this error in project:
error C2660: 'calcAverage' : function does not take 3 arguments
I need to create a program that calculates the average of 3 test scores. The program uses 2 value-returning functions and 2 void functions.
Can someone point me in the right direction?
Here is my code:
#include <iostream> 
#include <iomanip> 

using std::cin; 
using std::cout; 
using std::endl; 

//function prototype 
double getTestScore (double); 
double calcAverage (double); 

int main () 
{ 
	 //declare variables 
	 double TestScore1 = 0.0; 
	 double TestScore2 = 0.0; 
	 double TestScore3 = 0.0; 
	 double Average = 0.0;
	 double Score = 0.0;

	 //Get variables 
	 TestScore1 = getTestScore(Score); 
	 TestScore2 = getTestScore(Score); 
	 TestScore3 = getTestScore(Score); 

	 //calculate Average 
	 Average = calcAverage(TestScore1, TestScore2, TestScore3);
		  
	 cout << "The average of the test scores is: " << Average << endl; 
	  
	 return 0; 
}	 //end of main function 



//*********Extra Functions************ 

	 //getTestScore Function 
double getTestScore (double score) 
{ 
	 double Score = 0.0;
	 cout << "Enter Test score: " << endl; 
	 cin >> Score; 
	 return Score; 
}	 //end getTestScore Function 

	 //calcAverage Function 

double calcAverage (double averg); 

void getNumber (double number)
{
	 cout << "Enter your number: " << endl;
	 cin >> number;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Void functions

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,556
  • Joined: 23-August 08

Re: Void functions

Posted 25 October 2009 - 08:46 AM

How many arguments does this function take?
double calcAverage (double);


See also your classmate's post here.

Something to read here, although I suspect that this is already covered in your text if you'd read it.
Was This Post Helpful? 0
  • +
  • -

#3 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Void functions

Posted 25 October 2009 - 08:56 AM

Like Jack said, your function prototype, function call and function definition all need to have the same amount of arguments/parameters within the ()'s.

Also you have a prototype for your function before int main, but you do not have it defined after int main, like your other function.

This one, after int main
double calcAverage (double averg); 



should be done like this one

void getNumber (double number)
{
	 cout << "Enter your number: " << endl;
	 cin >> number;

}

Was This Post Helpful? 0
  • +
  • -

#4 chris0812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-September 09

Re: Void functions

Posted 25 October 2009 - 03:50 PM

Okay, but now I'm getting this error: "error C2082: redefinition of formal parameter 'score'"
#include <iostream> 
#include <iomanip> 

using std::cin; 
using std::cout; 
using std::endl; 

//function prototype 
double getTestScore (double); 
double calcAverage (double); 

int main () 
{ 
	 //declare variables 
	 double Testscore1 = 0.0; 
	 double Testscore2 = 0.0; 
	 double Testscore3 = 0.0; 
	 double average = 0.0;
	 double averg = 0.0;
	 double score = 0.0;

	 //Get variables 

	 Testscore1 = getTestScore(score); 
	 Testscore2 = getTestScore(score); 
	 Testscore3 = getTestScore(score); 

	 //calculate average 
	 average = calcAverage (averg); 
	  
	 cout << "The average of the test scores is: " << average << endl; 
	  
	 return 0; 
}	 //end of main function 



//*********Extra Functions************ 

	 //getTestScore Function 
double getTestScore (double score) 
{ 
	 double score = 0.0; 
	 cout << "Enter Test score: " << endl; 
	 cin >> score; 
	 return score; 
}	 //end getTestScore Function 

	 //calcAverage Function 

double calcAverage (double averg); 


void getNumber (double number)
{
	 cout << "Enter your number: " << endl;
	 cin >> number;

}


View PostOliveOyl3471, on 25 Oct, 2009 - 07:56 AM, said:

Like Jack said, your function prototype, function call and function definition all need to have the same amount of arguments/parameters within the ()'s.

Also you have a prototype for your function before int main, but you do not have it defined after int main, like your other function.

This one, after int main
double calcAverage (double averg); 



should be done like this one

void getNumber (double number)
{
	 cout << "Enter your number: " << endl;
	 cin >> number;

}

Was This Post Helpful? 0
  • +
  • -

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Void functions

Posted 25 October 2009 - 04:02 PM

double getTestScore (double score)
{
	 double score = 0.0;
	 cout << "Enter Test score: " << endl;
	 cin >> score;
	 return score;
}  


double getTestScore (double score) You don't need the (double score) part here since you're declaring score as a double here: double score = 0.0;

The compiler's complaining because you set two different variables with the same name (which isn't possible to have), and it has no clue which one you're refering to.

Try instead, double getTestScore(void)


Also, change the parts of your code that are like this:
Testscore1 = getTestScore(score);
	 Testscore2 = getTestScore(score);
	 Testscore3 = getTestScore(score); 

to this:
Testscore1 = getTestScore();
	 Testscore2 = getTestScore();
	 Testscore3 = getTestScore(); 

Was This Post Helpful? 0
  • +
  • -

#6 chris0812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-September 09

Re: Void functions

Posted 25 October 2009 - 04:16 PM

No, that didn't work, the compiler gave me an error: getTestScore' : function does not take 0 arguments

View Posterik.price, on 25 Oct, 2009 - 03:02 PM, said:

double getTestScore (double score)
{
	 double score = 0.0;
	 cout << "Enter Test score: " << endl;
	 cin >> score;
	 return score;
}  


double getTestScore (double score) You don't need the (double score) part here since you're declaring score as a double here: double score = 0.0;

The compiler's complaining because you set two different variables with the same name (which isn't possible to have), and it has no clue which one you're refering to.

Try instead, double getTestScore(void)


Also, change the parts of your code that are like this:
Testscore1 = getTestScore(score);
	 Testscore2 = getTestScore(score);
	 Testscore3 = getTestScore(score); 

to this:
Testscore1 = getTestScore();
	 Testscore2 = getTestScore();
	 Testscore3 = getTestScore(); 

Was This Post Helpful? 0
  • +
  • -

#7 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Void functions

Posted 25 October 2009 - 04:42 PM

Did you change the function prototype as well?

You would need to change:
//function prototype
double getTestScore (double);


to:
//function prototype
double getTestScore (void);

Was This Post Helpful? 0
  • +
  • -

#8 chris0812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-September 09

Re: Void functions

Posted 25 October 2009 - 04:52 PM

Yes, I changed the function prototype to (void) but I got this:
Ch10AppE05.obj : error LNK2028: unresolved token (0A0002C5) "double __cdecl calcAverage(void)" (?calcAverage@@$$FYANXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
Ch10AppE05.obj : error LNK2019: unresolved external symbol "double __cdecl calcAverage(void)" (?calcAverage@@$$FYANXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
I:\CS 115 Computer Programming\Chapter 10\Ch10AppE05 Solution\Debug\Ch10AppE05 Project.exe : fatal error LNK1120: 2 unresolved externals

View Posterik.price, on 25 Oct, 2009 - 03:42 PM, said:

Did you change the function prototype as well?

You would need to change:
//function prototype
double getTestScore (double);


to:
//function prototype
double getTestScore (void);

Was This Post Helpful? 0
  • +
  • -

#9 chris0812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-September 09

Re: Void functions

Posted 25 October 2009 - 04:58 PM

Okay, this is the LAST error I'm receiving after messing with this!
error C2660: 'calcAverage' : function does not take 0 arguments
One I get this one fixed, it should run properly!

#include <iostream> 
#include <iomanip> 

using std::cin; 
using std::cout; 
using std::endl; 

//function prototype 
double getTestScore (void); 
double calcAverage (double); 

int main () 
{ 
	 //declare variables 
	 double TestScore1 = 0.0; 
	 double TestScore2 = 0.0; 
	 double TestScore3 = 0.0; 
	 double Average = 0.0;
	 double Score = 0.0;

	 //Get variables 
	 TestScore1 = getTestScore(); 
	 TestScore2 = getTestScore(); 
	 TestScore3 = getTestScore(); 

	 //calculate Average 
	 Average = calcAverage(double);
		  
	 cout << "The average of the test scores is: " << Average << endl; 
	  
	 return 0; 
}	 //end of main function 



//*********Extra Functions************ 

	 //getTestScore Function 
double getTestScore (void)
{ 
	 double Score = 0.0;
	 cout << "Enter Test score: " << endl; 
	 cin >> Score; 
	 return Score; 
}	 //end getTestScore Function 

	 //calcAverage Function 

double calcAverage (void); 

void getNumber (double number)
{
	 cout << "Enter your number: " << endl;
	 cin >> number;

}


View Postchris0812, on 25 Oct, 2009 - 03:52 PM, said:

Yes, I changed the function prototype to (void) but I got this:
Ch10AppE05.obj : error LNK2028: unresolved token (0A0002C5) "double __cdecl calcAverage(void)" (?calcAverage@@$$FYANXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
Ch10AppE05.obj : error LNK2019: unresolved external symbol "double __cdecl calcAverage(void)" (?calcAverage@@$$FYANXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
I:\CS 115 Computer Programming\Chapter 10\Ch10AppE05 Solution\Debug\Ch10AppE05 Project.exe : fatal error LNK1120: 2 unresolved externals

View Posterik.price, on 25 Oct, 2009 - 03:42 PM, said:

Did you change the function prototype as well?

You would need to change:
//function prototype
double getTestScore (double);


to:
//function prototype
double getTestScore (void);

Was This Post Helpful? 0
  • +
  • -

#10 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Void functions

Posted 25 October 2009 - 05:19 PM

But you still don't have your calcAverage function doing anything. And I think you DO want to have arguments passed into it. You just need to make sure the amount of arguments/parameters for your function definition, declaration and call all match.

I think you were on the right track when you had three arguments passed in.

If you do it this way, you can make the program not only compile, but also calculate & return the average:

//function prototype 
double calcAverage (double, double, double); 

int main () 
{ 
	 //your code here

	 //calculate Average 
	 Average = calcAverage(TestScore1, TestScore2, TestScore3);

	 //your code here

}	 //end of main function 

	 //your code here

	 //calcAverage Function 
double calcAverage (double test1, double test2, double test3){ //pass your 3 values in
	   double ave = //you should know what to put here
	   return ave;
	   } 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1