+ 2

Isn't it possible to call a function ?!!! :( RSVP

Can someone explain why this code isn't working. I thought it was possible to call a function with console output (cout <<). If this is a problem with the editor, I use Code::Blocks Please recommend something else. I use Windows. This is my code: #include <iostream> using namespace std; void crappyPrint() { int x; cout << "How old are you ?" << endl; cin >> x; } int main() { crappyPrint(); cout << "So you are" << crappyPrint(); return 0; } The build log is telling me |note: cannot convert 'crappyPrint()' (type 'void') to type 'const unsigned char*'|

20th Mar 2017, 10:19 PM
ΞⓀⒾⓃⒼΞ
ΞⓀⒾⓃⒼΞ - avatar
6 Respuestas
+ 16
You can't have a function do cin in a cout statement inside main, and you need a parameter in your function to pass the value back to main. In most cases, you cannot cout a void function because there isn't a return value. // fix #include <iostream> using namespace std; void crappyPrint(int& x) { cout << "How old are you ?" << endl; cin >> x; } int main() { int x; crappyPrint(x); cout << "So you are " << x << " years old"; return 0; }
20th Mar 2017, 11:40 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
There is nothing to print, its a void function. You need to return something to print out the function. (just call it instead of printing it if its void). If you want the function to give you the age you can say: int crappyPrint(){ //other code here return x; }
20th Mar 2017, 10:40 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
hey your function doesn't return any value.(You have written void). Write there int and just give a return statement.
21st Mar 2017, 4:14 AM
Aswin A
Aswin A - avatar
+ 1
there needs to be an actual object for it to write, try this. int crappyPrint() { int x; cout << "How old are you ?" << endl; cin >> x; return x; }
20th Mar 2017, 11:25 PM
Daniel Kahle
Daniel Kahle - avatar
0
I can't understand the build message :(
20th Mar 2017, 10:20 PM
ΞⓀⒾⓃⒼΞ
ΞⓀⒾⓃⒼΞ - avatar
0
Now I can't get the code to print the result right.
20th Mar 2017, 11:15 PM
ΞⓀⒾⓃⒼΞ
ΞⓀⒾⓃⒼΞ - avatar