My program is a game. Two numbers are randomly generated until they match, and the player must guess where these two numbers will match. However, at line 111 after I get the player's guess, the program creates crazy numbers within the hundred-thousands range and then shuts off after it gets a match. It'd be very helpful to get some advice on this.
Thanks everybody.
Oh, and don't irk me for using Sleep. I know it's a bad thing to do but this is a just for fun assignment.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <windows.h> // ALWAYS include when using Sleep function;
#include <ctime>
usingnamespace std;
int first = 5;
int second = 2;
int switchit;
int advancedfirst, advancedsecond;
int guess;
srand ( time(NULL) );
void firstnumber() // randomize first number
{
first = rand() % 200 + 50;
}
void secondnumber() // randomize second number
{
second = rand() % 200 + 50;
}
int advancedfirstnumber()
{
advancedfirst = rand() % 500 + 50;
return advancedfirst;
}
int advancedsecondnumber()
{
advancedsecond = rand() % 500 + 50;
return advancedsecond;
}
void advanced()
{
system("CLS");
//ask player to enter number guess; get number; create loop for 50-500
int advancedguess;
cout<<"********************************************\n\n\t\t\tADVANCED - 00000065432\n\n********************************************\n\n";
cout<<"Welcome to Advanced difficulty. The number range has moved up from 50-200 to 50-500.";
cout<<"I trust you have the patience for this one; it'll surely take a while.\n";
cout<<"\n\t\t\t\tShall we begin?:";
cout<<"\n\t\t\t";
string begin;
getline(cin, begin);
if (begin == "yes" || begin == "sure" || begin == "ok" || begin == "yeah" || begin == "yea" ||
begin == "hell yeah")
{
cout<<"\n\n\t\t\t\tVery well.\n";
}
else {
cout<<"\n\n\t\t\t\tWell hurry up, I'll give you 20 seconds to get ready.\n";
Sleep(5000);
}
advancedrestart:
cout<<"\nEnter a number between 50 and 500: ";
cin>>advancedguess;
cout<<"\n\n";
do {
// display number, two spaces, display second, two spaces
advancedfirstnumber();
cout<< advancedfirstnumber();
cout<<" ";
advancedsecondnumber();
cout<< advancedsecondnumber();
cout<<" ";
} while (advancedfirst != advancedsecond);
// check if the guess is equal to where the two numbers matched
if (advancedguess == advancedfirst)
{
cout<<"\n\n\t\t\t\tVery nice.\n\n";
cout<<"Well that's it for now, I'll be adding in another level soon.\n";
Sleep(2000);
exit (1);
}
else {
cout<<"\n\n\t\t\t\tThat is incorrect.";
Sleep(5000);
cout<<"\n\n\t\t\t\t\t ...";
goto advancedrestart;
}
}
int main()
{
cout<<"In this game, random numbers will be generated until those two numbers match.\n\n";
Sleep(4000);
cout<<"You will guess which number the two will match at.\n\n";
Sleep(4000);
cout<<"If you get it wrong, it will restart.\n";
cout<<"Get ready.\n\n";
Sleep(5000);
cout<<"3.. "; Sleep(400); cout<<"2.. "; Sleep(400); cout<<"1.. "; Sleep(400); cout<<"go!\n\n\n"; // 3.. 2.. 1.. go!
restart:
cout<<"Guess a number between 50 and 200: ";
cin >> guess;
cout<<"Press enter.\n";
cin.get();
while (first != second)
{
first = rand() % 200 + 50;
cout<< first;
cout<<" ";
second = rand() % 200 + 50;
cout<<" ";
cout<< second;
}
if (guess == first)
{
if (guess == second)
options:
cout<<"Nice.\n\n\n\n\n\n\t\t\t\t";
cout<<"1/ Restart\n\t\t\t\t";
cout<<"2/ Move on to Advanced difficulty\n\t\t\t\t";
cout<<"3/ Exit.\n";
cin>>switchit;
switch (switchit)
{
case 1:
goto restart;
break;
case 2:
advanced();
break;
case 3:
exit (1);
break;
default:
cout<<"You had to enter 1-3, please do so.\n";
goto options;
break;
}
}
elseif (!guess == first)
{
goto restart;
}
}