C++ error at the last line of code -
i'm begineer in c++ programming know basics. started writing simple game. program chooses random number (1-100) , have guess it. there 2 modes:
normal - whenever enter number program tells if it's bigger random or smaller.
hard - no clues, pure luck.
everything running ok when added fixes displayed text program won't compile. use code::blocks. screenshot: http://scr.hu/81tw/m6cm0 apreciate help.
full code below:
#include <iostream> #include <cstdlib> #include <time.h> using namespace std; int number_normal; int number_hard; int guess_normal; int guess_hard; int tries_normal=0; int tries_hard=0; int mode; int main() { { cout<<"choose mode..."<<endl; cout<<"normal (press 1) or hard (press 2)"<<endl; cin>>mode; if(mode=1) cout<<"normal mode chosen."<<endl; goto normal; if(mode=2) cout<<"hard mode chosen!"<<endl; goto hard; return 0; } { hard: cout<<"i chose random number in range 1 100, can guess it?"<<endl; srand(time(null)); number_hard = rand()%100+1; while(guess_hard!=number_hard) tries_hard++; cout<<"enter guess!(try "<<tries_hard<<"): "; cin>>guess_hard; if(guess_hard=number_normal) cout<<"respect! guessed in "<<tries_hard<<" tries!"<<endl; } { normal: cout<<"i chose random number 1 100. give clues! try guess it."<<endl; srand(time(null)); number_normal = rand()%100+1; while(guess_normal!=number_normal) tries_normal++; cout<<"enter guess!(try "<<tries_normal<<"): "; cin>>guess_normal; if(guess_normal==number_normal) cout<<"congrats! you're lucky. (won in "<<tries_normal<<" tries!)"<<endl; if(guess_normal<number_normal) cout<<"too low."<<endl; else if(guess_normal>number_normal) cout<<"that's much!"<<endl; system("pause"); return 0; }
the code works here
#include <iostream> #include <cstdlib> #include <time.h> using namespace std; int number_normal; int number_hard; int guess_normal; int guess_hard; int tries_normal=0; int tries_hard=0; int mode; int main() { { cout<<"choose mode..."<<endl; cout<<"normal (press 1) or hard (press 2)"<<endl; cin>>mode; if(mode=1) cout<<"normal mode chosen."<<endl; goto normal; if(mode=2) cout<<"hard mode chosen!"<<endl; goto hard; return 0; } { hard: cout<<"i chose random number in range 1 100, can guess it?"<<endl; srand(time(null)); number_hard = rand()%100+1; while(guess_hard!=number_hard) { tries_hard++; cout<<"enter guess!(try "<<tries_hard<<"): "; cin>>guess_hard; if(guess_hard==number_normal) cout<<"respect! guessed in "<<tries_hard<<" tries!"<<endl; } } { normal: cout<<"i chose random number 1 100. give clues! try guess it."<<endl; srand(time(null)); number_normal = rand()%100+1; while(guess_normal!=number_normal) { tries_normal++; cout<<"enter guess!(try "<<tries_normal<<"): "; cin>>guess_normal; if(guess_normal==number_normal) cout<<"congrats! you're lucky. (won in "<<tries_normal<<" tries!)"<<endl; if(guess_normal<number_normal) cout<<"too low."<<endl; else if(guess_normal>number_normal) cout<<"that's much!"<<endl; //system("pause"); } } return 0; }
well, didn't used braces across while loops, , @cowls suggested, there closing brace missing after main. else fine. used =
comparison between variables, instead of ==
, =
assignment operator, while ==
used comparison.
Comments
Post a Comment