i am having a problem in my game where i start to count from 1 to 10, can someone help me improve the codes? whenever i start to loop, it couts every "sorry start again!" and whenever i answer it correctly it still couts the sorry start again.. sorry for the noob codes.
cout<<"HELLO!!!! PLEASE ENTER YOUR NAME: ";
gets(name);
clrscr();
cout<<"Hello "<<name<<", this is a game about counting and questions about life.";
cout<<"\nyou have to count in correct order for you to continue to the next level,";
cout<<"\nevery level has a question about the creator of this game, if you answer it \ncorrectly,you will proceed to the next level";
cout<<"\nAre you ready? START!!";
cout<<" Please enter the first number: ";
cin>>num;
do{
if (num != 1)
cout<<"Sorry! start again!";
else
{
cout<<" please enter the next number: ";
cin>>num;
}
if (num !=2)
cout<<"Sorry! start again!!";
else
{
cout<<" please enter the next number: ";
cin>>num;
}
if (num !=3)
cout<<"Sorry! start again!!!";
else
{
cout<<" please enter the next number: ";
cin>>num;
}
if (num !=4)
cout<<"Sorry! start again!!!!";
else
{
cout<<" please enter the next number: ";
cin>>num;
}
if (num !=5)
cout<<"Sorry! start again!!!!";
else
{
cout<<" please enter the next number: ";
cin>>num;
}
if (num !=6)
cout<<"Sorry! start again!!!!!";
else
{
cout<<" please enter the next number: ";
cin>>num;
}
if (num !=7)
cout<<"Sorry! start again!!!!!";
else
{
cout<<" please enter the next number: ";
cin>>num;
}
if (num !=8)
cout<<"Sorry! start again!!!!!";
else
{
cout<<" please enter the next number: ";
cin>>num;
}
if (num !=9)
cout<<"Sorry! start again!!!!!";
else
{
cout<<" please enter the last number of level 1: ";
cin>>num;
}
if (num == 10)
{
cout<<"YOU'RE GOOD!! But To finish the game, answer this question first";
cout<<"\nWhat is the Middle initial of Napoleon Basan?: ";
cin>>sub;
if (sub == 'A')
cout<<"CONGRATULATIONS!! YOU FINISHED THE GAME!!";
After you win the game you need a way of jumping out of the loop. You can use either return 0 or continue to get out of the loop. f You would place these statements just below line cout<<"CONGRATULATIONS!! YOU FINISHED THE GAME!!";. Also, if you have knowledge of arrays you could shorten down this code a whole lot using for loops.
uhm, when i placed return or continue, it says that "Misplaced else in function main" and also, whenever i write a number then if i input it not in order it displays all the sorry start again..
if( some_condition )
{
// here, you put whatever code you want run when
// 'some_condition' is true
}
else
{
// here, you put whatever code you want run when
// 'some_condition' is false
}