I'm having a lot of trouble with a project for school. The project calls for me to create a random guessing game with three different levels. Each level should consist of a random number between 1 and 30. I have set my code to show between 1 and 5 for the purpose of seeing how well it works. The problem I'm having comes after the second correct guess because it seems that the third guess is the exact same as the 2nd guess. If you could help I'd greatly appreciate it seeing as this project is due in 2 days.

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()

{

int number, guess;
number = rand () % 5+1;



for(int i=0;i<3;i++)// first level

{	
	cout <<"Please enter your guess: ";
	cin>>guess;
	
	/* loop through 3 attempts */
	if (guess==number) {
		cout<<"Good guess!\n";
		break; }
	else
	cout<<"Please try again!\n";
	
	
	if (i == 2)
		return 0; // if failed 3 times, end program
	} 

int option, a;
number = rand () % 5+1;
bool finished = false;
while (!finished)
{
	cout<<"Would you like to continue? \n";
	cout<<"\n";
	cout<<"[1] Yes \n"
		<<"[2] No \n";
	cin>>option;
	
	switch(option)
	{
case 1:
{
	for(int i=0;i<3;i++)// second level

		{	
		cout <<"Please enter your guess: ";
		cin>>guess;
	
		/* loop through 3 attempts at pin */
		if (guess==number) 
		{
		cout<<"Good guess!\n";
		finished = true;
		goto test3;
		break;
		}
		else
		cout<<"Please try again!\n";
	
	
		if (i == 2)
		return 0; // if failed 3 times, end program
		} /* end password loop */
}

break;

case 2:
	{
		finished = true;
		{
			cout<<"Thanks for playing!";
		}
		return 0;
	}
	break;

test3: 
int choice;
number = rand () % 10+1;
bool finished = false;
while (!finished)
{
	cout<<"Would you like to continue? \n";
	cout<<"\n";
	cout<<"[1] Yes \n"
		<<"[2] No \n";
	cin>>choice;
	
	switch(choice)
	{
case 1:
{
	for(int i=0;i<3;i++)// third level

		{	
		cout <<"Please enter your guess: ";
		cin>>guess;
	
		/* loop through 3 attempts at pin */
		if (guess==number) 
		{
		cout<<"Good guess!\n";
		finished = true;
		break;
		}
		else
		cout<<"Please try again!\n";
		if (i == 2)
		return 0; // if failed 3 times, end program
		} /* end password loop */
}

break;

case 2:
	{
		finished = true;
		{
			cout<<"Thanks for playing!";
		}
		return 0;
	}
	break;
	}
	return 0;
}
}
}
}

Please For god sake someone edit his thread and put code tags... I cant read a thing.. Your problem is the GOTO... You shouldn't use that as the stream is still full and wasn't cleared (side effect of using goto).. Try to make three functions and call them instead.

Again I will say: PLEASE USE CODE TAGS... For this reason, I didn't read through the whole thing just because of code tags.. u hurt my eyes without it.

Anyway. Get rid of the Goto test3 and instead do:

#include .....etc......

void test1();                       //These are all called function-prototypes.. look it up.
void test2();                       //They also don't have to be void.. they can return an int, return true or false.. whatever u need.
void test3();                       //Again Next time PLEASE USE CODE TAGS FOR A FASTER + BETTER RESPONSE!!

int main()
{
   test1();                       //This is how u call the function.. meaning it will do this then it will do test2, then test3..
   test2();
   test3();
   return 0;                      
}

void test1()
{
  //do stuff here
}

void test2()
{
  //do test 2 code here..
}

void test3()
{
  //something here too..
}

Your code is below.. next time use code tags. Note: I didnt change a thing.. I just put it in tags thats all.

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()

{

int number, guess;
number = rand () % 5+1;



for(int i=0;i<3;i++)// first level

{
cout <<"Please enter your guess: ";
cin>>guess;

/* loop through 3 attempts */
if (guess==number) {
cout<<"Good guess!\n";
break; }
else
cout<<"Please try again!\n";


if (i == 2)
return 0; // if failed 3 times, end program
}

int option, a;
number = rand () % 5+1;
bool finished = false;
while (!finished)
{
cout<<"Would you like to continue? \n";
cout<<"\n";
cout<<"[1] Yes \n"
<<"[2] No \n";
cin>>option;

switch(option)
{
case 1:
{
for(int i=0;i<3;i++)// second level

{
cout <<"Please enter your guess: ";
cin>>guess;

/* loop through 3 attempts at pin */
if (guess==number)
{
cout<<"Good guess!\n";
finished = true;
goto test3;
break;
}
else
cout<<"Please try again!\n";


if (i == 2)
return 0; // if failed 3 times, end program
} /* end password loop */
}

break;

case 2:
{
finished = true;
{
cout<<"Thanks for playing!";
}
return 0;
}
break;

test3:
int choice;
number = rand () % 10+1;
bool finished = false;
while (!finished)
{
cout<<"Would you like to continue? \n";
cout<<"\n";
cout<<"[1] Yes \n"
<<"[2] No \n";
cin>>choice;

switch(choice)
{
case 1:
{
for(int i=0;i<3;i++)// third level

{
cout <<"Please enter your guess: ";
cin>>guess;

/* loop through 3 attempts at pin */
if (guess==number)
{
cout<<"Good guess!\n";
finished = true;
break;
}
else
cout<<"Please try again!\n";
if (i == 2)
return 0; // if failed 3 times, end program
} /* end password loop */
}

break;

case 2:
{
finished = true;
{
cout<<"Thanks for playing!";
}
return 0;
}
break;
}
return 0;
}
}
}
}

awesome thank you so much! sorry about the code tags i'll be sure to use them next time! thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.