User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,375 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,051 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1368 | Replies: 8 | Solved
Reply
Join Date: Jun 2006
Location: USA
Posts: 1,040
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Rep Power: 7
Solved Threads: 4
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Help User-Defined Function - part 2

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,472
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 16
Solved Threads: 269
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: User-Defined Function - part 2

  #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.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Mar 2007
Location: Aptos, CA
Posts: 118
Reputation: ft3ssgeek is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: User-Defined Function - part 2

  #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  
Join Date: Mar 2007
Location: Aptos, CA
Posts: 118
Reputation: ft3ssgeek is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: User-Defined Function - part 2

  #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  
Join Date: Jun 2006
Location: USA
Posts: 1,040
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Rep Power: 7
Solved Threads: 4
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Help Re: User-Defined Function - part 2

  #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 3: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  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: User-Defined Function - part 2

  #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."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Jun 2006
Location: USA
Posts: 1,040
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Rep Power: 7
Solved Threads: 4
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: User-Defined Function - part 2

  #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  
Join Date: May 2006
Posts: 2,700
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: User-Defined Function - part 2

  #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?
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Jun 2006
Location: USA
Posts: 1,040
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Rep Power: 7
Solved Threads: 4
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: User-Defined Function - part 2

  #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 9: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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum