I agree, and you need to rethink the goto's as well, you must be coming from a BASIC background. While there are some legitimate uses of goto in c++, it should be avoided.
I compiled it and it worked fine for me, even when I guessed right on the last guess. So not sure what's happening with yours.
btw check out the nice formatting courtesy of Code::Blocks
Code:
#include <iostream>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
using namespace std;
int rand_0toN1(int n);
int main()
{
double lev_dif, lev_left, lev_play, lev_guess, lev_rand, goto_starter;
srand(time(NULL)); // Set a seed for random-num. generation.
souldnt_go:
for(goto_starter=(99);goto_starter>50;)
{
clrscr();
cout<<"Choose level difficultey:"<<endl;
cout<<endl;
cout<<"1 - Super Hard"<<endl;
cout<<"2 - Hard"<<endl;
cout<<"3 - Medium"<<endl;
cout<<"4 - Easy"<<endl;
cout<<"5 - Super Easy"<<endl;
cout<<"6 - Exit"<<endl;
cout<<"\nOption: ";
cin>>lev_dif;
if(lev_dif == 6)
{
clrscr();
return 0;
}
else if(lev_dif < 1)
{
goto_starter = (99);
}
else if(lev_dif > 5)
{
goto_starter = (99);
}
else
{
goto go_break;
}
}
go_break:
clrscr();
lev_play=(lev_dif + 2);
lev_rand = rand_0toN1(99) + 1;
cout<<"Guess a number bettween one and a hundred."<<endl;
cout<<"Beginning Level "<<lev_dif;
cout<<"\n"<<endl;
for(lev_left = lev_play;lev_left > 0;lev_left = (lev_left - 1))
{
cout<<"Guess "<<lev_left<<" of "<<lev_play<<": ";
cin>>lev_guess;
if(lev_guess > lev_rand)
{
cout<<"Wrong, guess lower."<<endl;
}
else if (lev_guess < lev_rand)
{
cout<<"Wrong, guess higher."<<endl;
}
else if (lev_guess == lev_rand)
{
cout<<"Right, you won!"<<endl;
cout<<"The number was "<<lev_rand<<endl;
getch( );
goto souldnt_go;
}
else
{
clrscr();
return 0;
}
}
cout<<"\nSorry, but you lost!"<<endl;
cout<<"The right number was "<<lev_guess<<endl;
getch( );
goto souldnt_go;
}
int rand_0toN1(int n)
{
return rand() % n;
}