When it tells you that cout was not declared, it is because cout is not a global function, parameter or other kind of thing you can do. For example, this program will not work:
1 2 3 4 5
|
#include <iostream>
int main(){
cout << "Hello world" << endl;
return 0;
}
| |
Because, cout is part of the std library so you must either change it to this:
1 2 3 4 5 6
|
#include <iostream>
using namespace std;
int main(){
cout << "Hello world" << endl;
return 0;
}
| |
or this:
1 2 3 4 5
|
#include <iostream>
int main(){
std::cout << "Hello world" << endl;
return 0;
}
| |
Note that in the above one, it uses std:: as a prefix, this means that you are looking for a function in the std class. using the command
using namespace std
automatically adds an imaginary std:: to all the commands that you call after that, that use std:: as their prefix.
also, to properly format your code to look like
this
type ["code"] ;; ["/code"] where " is removed, and ;; is just your code.
As to why your code isn't working, try an approach more like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
start:
int guessed = 0, generated = rand() % 10;
cout << endl << endl << "Guess a number from 1 to 10" << endl << endl;
cin >> guessed;
if(generated == guessed){
cout << endl << endl << "Congratulations, that is the secret number";
}
else{
cout << endl << endl << "Guess again please";
goto start;
}
return 0;
}
| |