User-Defined Function - part 2

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

User-Defined Function - part 2

 
0
  #1
Apr 4th, 2007
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:

  
  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. }
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: User-Defined Function - part 2

 
0
  #2
Apr 4th, 2007
Don't have time to check over the rest of the code, but I can tell you what is wrong here:
  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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: User-Defined Function - part 2

 
0
  #3
Apr 4th, 2007
Originally Posted by Duki View Post
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:

  
  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...
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: User-Defined Function - part 2

 
0
  #4
Apr 4th, 2007
As for part 1, I believe the instructions are referring to c-strings...how much do you know about c-strings?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: User-Defined Function - part 2

 
0
  #5
Apr 4th, 2007
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? =/

  
  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.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: User-Defined Function - part 2

 
0
  #6
Apr 4th, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: User-Defined Function - part 2

 
0
  #7
Apr 4th, 2007
Originally Posted by ft3ssgeek View Post
As for part 1, I believe the instructions are referring to c-strings...how much do you know about c-strings?
i don't =/
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: User-Defined Function - part 2

 
0
  #8
Apr 4th, 2007
Originally Posted by ft3ssgeek View Post
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?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: User-Defined Function - part 2

 
0
  #9
Apr 5th, 2007
Just an update, thanks to everyone who helped. Here's the finalized code. This one is solved

  
  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.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC