944,050 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2981
  • C++ RSS
Apr 4th, 2007
0

User-Defined Function - part 2

Expand Post »
Hey everyone, this is a big one.

The question has asked that I define functions and then write the function main to test the functions I wrote. I keep getting the following error at my first cout in main:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

I'm not sure what this is saying.
In addition, I need help with defining two of my functions and writing code to test them. They are as follows:

1. Write the definition of the function nextChar that sets the value of z to the next character stored in z.

2. Write the definition of the function funcOne that prompts the user to input a number. The function then changes the value of x to 2 times the old value of x plus the value of y minus the value entered by the user.

Now, with #2 I'm pretty sure I have the function defined right, though I don't know how to test it. #1 though, I have no clue how to define that function, though I did attempt it =/.

Could someone look through my code, and let me know if they notice any huge mistakes? This is our second chapter on user-defined functions, so I'm still learning about call by reference and whatnot. I have commented my code minimaly to assist you in finding the parts I'm having a tough time with.

Thanks guys! :mrgreen:

code:

  
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std ;
  4.  
  5. void initialize ( int& , int& , char& ) ;
  6. void getHoursRate ( double& , double& ) ;
  7. double payCheck ( double& , double& ) ;
  8. void printCheck ( double , double, double ) ;
  9. int main()
  10. {
  11. int x ;
  12. int y ;
  13. int w ;
  14.  
  15. char z ;
  16.  
  17. double rate ;
  18. double hours ;
  19. double amount ;
  20.  
  21. cout << initialize( x , y , z ) << endl ; //error is here
  22. cout << getHoursRate() << endl ;
  23. cout << payCheck( hours , rate ) << endl ;
  24. printCheck( hours, rate, amount ) << endl ;
  25. cout << funcOne( w, x, y ) ; //confusion starts here
  26. cout <<
  27. }
  28.  
  29. void initialize ( int& x, int& y, char& z )
  30. {
  31. x = y = 0 ;
  32. z = ' ' ;
  33. }
  34.  
  35. void getHoursRate ( double& hours , double& rate )
  36. {
  37. cout << "Hours worked: " << endl ;
  38. cin >> hours ;
  39. cout << "Rate: " << endl ;
  40. cin >> rate ;
  41. }
  42.  
  43. double payCheck ( double& hours, double& rate )
  44. {
  45. double pay ;
  46.  
  47. int ot ;
  48.  
  49. if ( (hours - 40 <= 0 ) )
  50. pay = rate * hours ;
  51. else
  52. {
  53. ot = hours - 40 ;
  54. pay = (40 * rate) + (ot * rate * 1.5) ;
  55. }
  56. return pay ;
  57. }
  58.  
  59. void printCheck ( double hours , double rate , double pay )
  60. {
  61. cout << setprecision(2) << fixed << showpoint << setfill('.') << left ;
  62. cout << "Hours" << setw(15) << "Rate" << setw(15) << "Amount Due" << endl << endl ;
  63. cout << hours << setw(15) << "$" << rate << setw(15) << "$" << pay << endl ;
  64. }
  65.  
  66. void funcOne ( double in, int x, int y ) //number 2 in my post
  67. {
  68. cout << "Input a value: " << endl ;
  69. cin >> in ;
  70.  
  71. x = x * x + ( y - in ) ;
  72. }
  73.  
  74. char nextChar ( char ch ) //number 1 in my post
  75. {
  76. ch = ch++ ;
  77. }
Similar Threads
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Apr 4th, 2007
0

Re: User-Defined Function - part 2

Don't have time to check over the rest of the code, but I can tell you what is wrong here:
C++ Syntax (Toggle Plain Text)
  1. cout << initialize( x , y , z ) << endl ; //error is here
initialize returns nothing, so cout has nothing to print. If you want to initalize the variables, try calling the function in a separate line instead of mixing it in with the cout call.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 4th, 2007
0

Re: User-Defined Function - part 2

Click to Expand / Collapse  Quote originally posted by Duki ...
2. Write the definition of the function funcOne that prompts the user to input a number. The function then changes the value of x to 2 times the old value of x plus the value of y minus the value entered by the user.

Now, with #2 I'm pretty sure I have the function defined right, though I don't know how to test it.

code:

  
C++ Syntax (Toggle Plain Text)
  1. void funcOne ( double in, int x, int y ) //number 2 in my post
  2. {
  3. cout << "Input a value: " << endl ;
  4. cin >> in ;
  5.  
  6. x = x * x + ( y - in ) ; // <----Wrong
  7. }
With this function you are squaring x instead of doubling it (ie use 2*x instead of x*x). To test it, simply do some simple math in your head, using different values of x and y....then implement that in your code...if your code gives you different answers, then, unless you did the math wrong yourself, you know there is a problem with your code...
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Apr 4th, 2007
0

Re: User-Defined Function - part 2

As for part 1, I believe the instructions are referring to c-strings...how much do you know about c-strings?
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Apr 4th, 2007
0

Re: User-Defined Function - part 2

Ok, I've modified my code, and am getting an error that says:

error C2563: mismatch in formal parameter list

This is at the same line with the comment //error is here

I've called the void functions outside of a cout, and have put cout statements inside of the function to test them. What am I doing wrong? =/

  
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std ;
  4. void initialize ( int& , int& , char& ) ;
  5. void getHoursRate ( double& , double& ) ;
  6. double payCheck ( double& , double& ) ;
  7. void printCheck ( double , double, double ) ;
  8. void funcOne ( doulbe, int, int ) ;
  9. void nextChar ( char ) ;
  10. int main()
  11. {
  12. int x ;
  13. int y ;
  14. int w ;
  15.  
  16. char z ;
  17. double rate ;
  18. double hours ;
  19. double amount ;
  20. initialize( x , y , z ) << endl ; //error is here
  21. getHoursRate() << endl ;
  22. cout << payCheck( hours , rate ) << endl ;
  23. printCheck( hours, rate, amount ) << endl ;
  24. funcOne( w, x, y ) ; //confusion starts here
  25. nextChar ( z ) ;
  26. }
  27. void initialize ( int& x, int& y, char& z )
  28. {
  29. x = y = 0 ;
  30. z = ' ' ;
  31. cout << "Inside initialize-function: " << endl ;
  32. cout << "The values of 'x', 'y' and 'z', respectively are: " x << "\t" << y << "\t" << z << endl << endl ;
  33. }
  34. void getHoursRate ( double& hours , double& rate )
  35. {
  36. cout << "Hours worked: " << endl ;
  37. cin >> hours ;
  38. cout << "Rate: " << endl ;
  39. cin >> rate ;
  40. cout << "Inside getHoursRate-function: " << endl ;
  41. cout << "The values of 'hours' and 'rate', respectively are: " << hours << "\t" << rate << endl << endl ;
  42. }
  43. double payCheck ( double& hours, double& rate )
  44. {
  45. double pay ;
  46.  
  47. int ot ;
  48. if ( (hours - 40 <= 0 ) )
  49. pay = rate * hours ;
  50. else
  51. {
  52. ot = hours - 40 ;
  53. pay = (40 * rate) + (ot * rate * 1.5) ;
  54. }
  55. return pay ;
  56. }
  57. void printCheck ( double hours , double rate , double pay )
  58. {
  59. cout << setprecision(2) << fixed << showpoint << setfill('.') << left ;
  60. cout << "Hours" << setw(15) << "Rate" << setw(15) << "Amount Due" << endl << endl ;
  61. cout << hours << setw(15) << "$" << rate << setw(15) << "$" << pay << endl ;
  62. }
  63. void funcOne ( double in, int x, int y ) //number 2 in my post
  64. {
  65. cout << "Input a value: " << endl ;
  66. cin >> in ;
  67. x = x * 2 + ( y - in ) ;
  68. cout << "Inside funcOne-function: " << endl ;
  69. cout << "The value of 'x' is: " << x << endl << endl ;
  70. }
  71. char nextChar ( char ch ) //number 1 in my post
  72. {
  73. cout << "Inside nextChar-function: " << endl ;
  74. cout << "Initial character stored in 'ch' is: " << ch << endl ;
  75. ch = ch++ ;
  76. cout << "The character stored in 'ch' after incriment is: " << ch << endl << endl ;
  77. }
Last edited by Duki; Apr 4th, 2007 at 4:06 pm.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Apr 4th, 2007
0

Re: User-Defined Function - part 2

initialize( x , y , z ) << endl ;
You can't do this in C++ esp when initialize doesn't return anything and even if it did, you aren't specifying the stream to which the output should be redirected.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 4th, 2007
0

Re: User-Defined Function - part 2

Click to Expand / Collapse  Quote originally posted by ft3ssgeek ...
As for part 1, I believe the instructions are referring to c-strings...how much do you know about c-strings?
i don't =/
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Apr 4th, 2007
0

Re: User-Defined Function - part 2

Click to Expand / Collapse  Quote originally posted by ft3ssgeek ...
As for part 1, I believe the instructions are referring to c-strings...how much do you know about c-strings?
I don't think so. Although, I don't know what the instructions mean, either.

Can you give us an example of the operation?
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Apr 5th, 2007
0

Re: User-Defined Function - part 2

Just an update, thanks to everyone who helped. Here's the finalized code. This one is solved

  
c++ Syntax (Toggle Plain Text)
  1. /*****************************************************
  2. * COSC 230 - Structured Programming
  3. * Chapter 7: Programming Exercise 1
  4. * Apr. 4, 2007
  5. *
  6. * Discription:
  7. * blah
  8. ******************************************************/
  9.  
  10. //header files
  11. #include <iostream>
  12. #include <iomanip>
  13. using namespace std ;
  14.  
  15. //function prototypes
  16. void initialize ( int& , int& , char& ) ;
  17. void getHoursRate ( double& , double& ) ;
  18. double payCheck ( double& , double& ) ;
  19. void printCheck ( double , double, double ) ;
  20. void funcOne ( double, int, int ) ;
  21. void nextChar ( char ) ;
  22.  
  23. int main()
  24. {
  25. int x ;
  26. int y ;
  27. int w ;
  28.  
  29. char z ;
  30.  
  31. double rate ;
  32. double hours ;
  33. double amount ;
  34.  
  35. initialize( x , y , z ) ; //test initialize-func.
  36. getHoursRate(hours, rate) ; //test initialize-func.
  37. amount = payCheck( hours, rate ) ; //initialize amount to paycheck-func.
  38. cout << "*** Inside payCheck-function ***" << endl << payCheck( hours , rate ) << endl ; //test payCheck-func.
  39. printCheck( hours, rate, amount ) ; //test printCheck-func.
  40. funcOne( w, x, y ) ; //test funcOne-func.
  41. nextChar ( z ) ; //test nextChar-func.
  42. }
  43.  
  44. void initialize ( int& x, int& y, char& z )
  45. {
  46. x = y = 0 ;
  47. z = ' ' ;
  48.  
  49. cout << "*** Inside initialize-function ***" << endl ;
  50. cout << "The values of 'x' and 'y', respectively are: " << x << " and " << y << endl ;
  51. cout << "The character stored in z is: " << z << "(space character)" << endl << endl ;
  52. }//end void initialize
  53.  
  54. void getHoursRate ( double& hours , double& rate )
  55. {
  56. cout << "Hours worked: " << flush ;
  57. cin >> hours ;
  58. cout << "Rate: " << flush ;
  59. cin >> rate ;
  60.  
  61. cout << endl << "*** Inside getHoursRate-function ***" << endl << endl ;
  62. cout << "The values of 'hours' and 'rate', respectively are: " << hours << "\t" << rate << endl << endl ;
  63. }//end getHoursRate
  64.  
  65. double payCheck ( double& hours, double& rate )
  66. {
  67. double pay ;
  68.  
  69. int ot ;
  70.  
  71. if ( (hours - 40 <= 0 ) )
  72. pay = rate * hours ;
  73. else
  74. {
  75. ot = hours - 40 ;
  76. pay = (40 * rate) + (ot * rate * 1.5) ;
  77. }
  78. return pay ;
  79. }//end double payCheck
  80.  
  81. void printCheck ( double hours , double rate , double amount )
  82. {
  83. cout << "*** Inside printCheck-function ***" << endl ;
  84. cout << setprecision(2) << fixed << showpoint << setfill('.') ;
  85. cout << "Hours" << setw(20) << right << "Rate" << setw(25) << right << "Amount Due" << endl ;
  86. cout << hours << setw(15) << "$" << rate << setw(15) << "$" << amount << endl << endl ;
  87. }//end void printCheck
  88.  
  89. void funcOne ( double in, int x, int y )
  90. {
  91. cout << "Input a value: " << flush ;
  92. cin >> in ;
  93. cout << endl ;
  94.  
  95. x = x * 2 + ( y - in ) ;
  96.  
  97. cout << "*** Inside funcOne-function ***" << endl ;
  98. cout << "The value of 'x' is: " << x << endl << endl ;
  99. }//end void funcOne
  100.  
  101. void nextChar ( char ch )
  102. {
  103. cout << "*** Inside nextChar-function ***" << endl ;
  104. cout << "Initial character stored in 'ch' is: " << ch << endl ;
  105.  
  106. ch = ch++ ;
  107.  
  108. cout << "The character stored in 'ch' after increment is: " << ch << endl << endl ;
  109. }//end nextChar
Last edited by Duki; Apr 5th, 2007 at 10:42 am.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: priority queue using array!!!
Next Thread in C++ Forum Timeline: pragma





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC