| | |
User-Defined Function - part 2
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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:
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)
#include <iostream> #include <iomanip> using namespace std ; void initialize ( int& , int& , char& ) ; void getHoursRate ( double& , double& ) ; double payCheck ( double& , double& ) ; void printCheck ( double , double, double ) ; int main() { int x ; int y ; int w ; char z ; double rate ; double hours ; double amount ; cout << initialize( x , y , z ) << endl ; //error is here cout << getHoursRate() << endl ; cout << payCheck( hours , rate ) << endl ; printCheck( hours, rate, amount ) << endl ; cout << funcOne( w, x, y ) ; //confusion starts here cout << } void initialize ( int& x, int& y, char& z ) { x = y = 0 ; z = ' ' ; } void getHoursRate ( double& hours , double& rate ) { cout << "Hours worked: " << endl ; cin >> hours ; cout << "Rate: " << endl ; cin >> rate ; } double payCheck ( double& hours, double& rate ) { double pay ; int ot ; if ( (hours - 40 <= 0 ) ) pay = rate * hours ; else { ot = hours - 40 ; pay = (40 * rate) + (ot * rate * 1.5) ; } return pay ; } void printCheck ( double hours , double rate , double pay ) { cout << setprecision(2) << fixed << showpoint << setfill('.') << left ; cout << "Hours" << setw(15) << "Rate" << setw(15) << "Amount Due" << endl << endl ; cout << hours << setw(15) << "$" << rate << setw(15) << "$" << pay << endl ; } void funcOne ( double in, int x, int y ) //number 2 in my post { cout << "Input a value: " << endl ; cin >> in ; x = x * x + ( y - in ) ; } char nextChar ( char ch ) //number 1 in my post { ch = ch++ ; }
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
-Edsger Dijkstra
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)
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."
•
•
•
•
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)
void funcOne ( double in, int x, int y ) //number 2 in my post { cout << "Input a value: " << endl ; cin >> in ; x = x * x + ( y - in ) ; // <----Wrong }
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? =/
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)
#include <iostream> #include <iomanip> using namespace std ; void initialize ( int& , int& , char& ) ; void getHoursRate ( double& , double& ) ; double payCheck ( double& , double& ) ; void printCheck ( double , double, double ) ; void funcOne ( doulbe, int, int ) ; void nextChar ( char ) ; int main() { int x ; int y ; int w ; char z ; double rate ; double hours ; double amount ; initialize( x , y , z ) << endl ; //error is here getHoursRate() << endl ; cout << payCheck( hours , rate ) << endl ; printCheck( hours, rate, amount ) << endl ; funcOne( w, x, y ) ; //confusion starts here nextChar ( z ) ; } void initialize ( int& x, int& y, char& z ) { x = y = 0 ; z = ' ' ; cout << "Inside initialize-function: " << endl ; cout << "The values of 'x', 'y' and 'z', respectively are: " x << "\t" << y << "\t" << z << endl << endl ; } void getHoursRate ( double& hours , double& rate ) { cout << "Hours worked: " << endl ; cin >> hours ; cout << "Rate: " << endl ; cin >> rate ; cout << "Inside getHoursRate-function: " << endl ; cout << "The values of 'hours' and 'rate', respectively are: " << hours << "\t" << rate << endl << endl ; } double payCheck ( double& hours, double& rate ) { double pay ; int ot ; if ( (hours - 40 <= 0 ) ) pay = rate * hours ; else { ot = hours - 40 ; pay = (40 * rate) + (ot * rate * 1.5) ; } return pay ; } void printCheck ( double hours , double rate , double pay ) { cout << setprecision(2) << fixed << showpoint << setfill('.') << left ; cout << "Hours" << setw(15) << "Rate" << setw(15) << "Amount Due" << endl << endl ; cout << hours << setw(15) << "$" << rate << setw(15) << "$" << pay << endl ; } void funcOne ( double in, int x, int y ) //number 2 in my post { cout << "Input a value: " << endl ; cin >> in ; x = x * 2 + ( y - in ) ; cout << "Inside funcOne-function: " << endl ; cout << "The value of 'x' is: " << x << endl << endl ; } char nextChar ( char ch ) //number 1 in my post { cout << "Inside nextChar-function: " << endl ; cout << "Initial character stored in 'ch' is: " << ch << endl ; ch = ch++ ; cout << "The character stored in 'ch' after incriment is: " << ch << endl << endl ; }
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
-Edsger Dijkstra
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
-Edsger Dijkstra
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Just an update, thanks to everyone who helped. Here's the finalized code. This one is solved 

c++ Syntax (Toggle Plain Text)
/***************************************************** * COSC 230 - Structured Programming * Chapter 7: Programming Exercise 1 * Apr. 4, 2007 * * Discription: * blah ******************************************************/ //header files #include <iostream> #include <iomanip> using namespace std ; //function prototypes void initialize ( int& , int& , char& ) ; void getHoursRate ( double& , double& ) ; double payCheck ( double& , double& ) ; void printCheck ( double , double, double ) ; void funcOne ( double, int, int ) ; void nextChar ( char ) ; int main() { int x ; int y ; int w ; char z ; double rate ; double hours ; double amount ; initialize( x , y , z ) ; //test initialize-func. getHoursRate(hours, rate) ; //test initialize-func. amount = payCheck( hours, rate ) ; //initialize amount to paycheck-func. cout << "*** Inside payCheck-function ***" << endl << payCheck( hours , rate ) << endl ; //test payCheck-func. printCheck( hours, rate, amount ) ; //test printCheck-func. funcOne( w, x, y ) ; //test funcOne-func. nextChar ( z ) ; //test nextChar-func. } void initialize ( int& x, int& y, char& z ) { x = y = 0 ; z = ' ' ; cout << "*** Inside initialize-function ***" << endl ; cout << "The values of 'x' and 'y', respectively are: " << x << " and " << y << endl ; cout << "The character stored in z is: " << z << "(space character)" << endl << endl ; }//end void initialize void getHoursRate ( double& hours , double& rate ) { cout << "Hours worked: " << flush ; cin >> hours ; cout << "Rate: " << flush ; cin >> rate ; cout << endl << "*** Inside getHoursRate-function ***" << endl << endl ; cout << "The values of 'hours' and 'rate', respectively are: " << hours << "\t" << rate << endl << endl ; }//end getHoursRate double payCheck ( double& hours, double& rate ) { double pay ; int ot ; if ( (hours - 40 <= 0 ) ) pay = rate * hours ; else { ot = hours - 40 ; pay = (40 * rate) + (ot * rate * 1.5) ; } return pay ; }//end double payCheck void printCheck ( double hours , double rate , double amount ) { cout << "*** Inside printCheck-function ***" << endl ; cout << setprecision(2) << fixed << showpoint << setfill('.') ; cout << "Hours" << setw(20) << right << "Rate" << setw(25) << right << "Amount Due" << endl ; cout << hours << setw(15) << "$" << rate << setw(15) << "$" << amount << endl << endl ; }//end void printCheck void funcOne ( double in, int x, int y ) { cout << "Input a value: " << flush ; cin >> in ; cout << endl ; x = x * 2 + ( y - in ) ; cout << "*** Inside funcOne-function ***" << endl ; cout << "The value of 'x' is: " << x << endl << endl ; }//end void funcOne void nextChar ( char ch ) { cout << "*** Inside nextChar-function ***" << endl ; cout << "Initial character stored in 'ch' is: " << ch << endl ; ch = ch++ ; cout << "The character stored in 'ch' after increment is: " << ch << endl << endl ; }//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
-Edsger Dijkstra
![]() |
Similar Threads
- User-defined functions (C++)
- using(STL)function object (bind2nd) with a user defined function object (C++)
Other Threads in the C++ Forum
- Previous Thread: priority queue using array!!!
- Next Thread: pragma
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






