+ 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*'|
6 Réponses
+ 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;
}
+ 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;
}
+ 2
hey your function doesn't return any value.(You have written void). Write there int and just give a return statement.
+ 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;
}
0
I can't understand the build message :(
0
Now I can't get the code to print the result right.