For loops do not have 3 semicolons. Look at the simplest case:
Code:for(int i = 0; i < something; i++)
For loops do not have 3 semicolons. Look at the simplest case:
Code:for(int i = 0; i < something; i++)
Oh, right...duh!
I knew that, but didn't remember to visualize what it would look like if it had all 3 fields.
Forget I even said that...
So I suppose if I removed that last semicolon in the first for loop I wrote, it would have worked?
correct.So I suppose if I removed that last semicolon in the first for loop I wrote, it would have worked?
So I suppose every int function is supposed to return an int then?
Just for the record, the code did compile, when I changed it to the following:
Of course it doesn't handle the cases if a user entered a char, or float. But it was only supposed to be an example anyway for my question, which was whether or not a pointer could accept user input (I assumed it could, but just wanted to make sure).Code:#include <iostream> using namespace std; void f(int* p) { if (*p == 1) cout << "You entered 1. Good guess." <<endl; else { cout << "Wrong guess, please try again." <<endl; cin >> *p; } } int main() { int x; cout << "Please input a number:"<< endl; cin >> x; f(&x); for ( int i; x != 1; ) { cout << "You entered " <<x<< ", wrong guess. Please try again"<<endl; cin >> x; } return 0; }
Thanks for the replies.
No, it returns whatever you want it to return.
Yes, it does compile, but you may as well use references instead of pointers when possible, and in this case, it is indeed possible.Just for the record, the code did compile, when I changed it to the following:
Of course it doesn't handle the cases if a user entered a char, or float. But it was only supposed to be an example anyway for my question, which was whether or not a pointer could accept user input (I assumed it could, but just wanted to make sure).Code:#include <iostream> using namespace std; void f(int* p) { if (*p == 1) cout << "You entered 1. Good guess." <<endl; else { cout << "Wrong guess, please try again." <<endl; cin >> *p; } } int main() { int x; cout << "Please input a number:"<< endl; cin >> x; f(&x); for ( int i; x != 1; ) { cout << "You entered " <<x<< ", wrong guess. Please try again"<<endl; cin >> x; } return 0; }
Thanks for the replies.
Just to be crystal clear, this is all kinds of wrong.
OP, if we consider the parts of a function prototype such as int f (int *p); then, the return value type is int, and you need to return an integer. You are only supposed to omit a return statement if the return type is void.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"