#include <cstdlib>
#include <iostream>
#include <ctime>
usingnamespace std;
int mainagain();
int main()
{
//unsigned seed = time (0); //commented the random generator to troubleshoot code
//srand (seed);
int x = rand(), guess;
char y, again;
do {
cout << "Im thinking of a number, take a guess.\n";
cin >> guess;
if ( guess < x )
cout << "guess higher\n";
elseif (guess > x )
cout << "guess lower\n";
elseif (guess == x)
cout << "jackpot\n";
else cout << "invalid\n";
}while (guess != x);
if (guess == x)
{
cout << "\a";
for(x = 1; x < 5; x++)
cout << "\aJackpot" << endl;
}
cout << "Do you want to play again? y or n\n";
cin >> again;
if(again == y)
{
mainagain(); // trying to call the main function again to repeat the program
}else
cout << "Thanks for playing.\n";
system ("PAUSE");
return 0;
system("PAUSE");
return 0;
}
// not sure if i needed a seperate function to call or if i could call the main function within its self.
int mainagain()
{
//unsigned seed = time (0);
//srand (seed);
int x = rand(), guess;
char y, again;
do {
cout << "Im thinking of a number, take a guess.\n";
cin >> guess;
if ( guess < x )
cout << "guess higher\n";
elseif (guess > x )
cout << "guess lower\n";
elseif (guess == x)
{
for(x = 1; x < 5; x++)
cout << "\aJackpot" << endl;
}
else cout << "invalid\n";
}while (guess != x);
cout << "Do you want to play again? y or n\n";
cin >> again;
if(again == y)
{
cout << "lets play again.\n";
main();
}else
cout << "Thanks for playing.\n";
system ("PAUSE");
return 0;
}
Don't ever call main. This is not BASIC; calling main does not take you back to the start - it creates a whole new main function on the stack, eating up memory, and then does it again when you call main again, and then again, and again, and again, each time eating up more and more of the stack, until you run out of memory.
I see you already know how to use the do-while loop. Perhaps sticking a
Just don't call main ever, and be very careful calling a function from inside itself (this is known as recursion and if you get it wrong, you overflow the stack and crash).
im somewhat familiar with the loops, but i wanted to call functions to make the loop happen. and if again == y i wanted the same function to be called again.
I found my error:
I had if(again ==y)
should have been if(again =='y')
#include <cstdlib>
#include <iostream>
#include <ctime>
usingnamespace std;
int mainagain();
int main()
{
unsigned seed = time (0);
srand (seed);
int x = rand(), guess;
char y, again;
do
{
do {
cout << "Im thinking of a number, take a guess.\n";
cin >> guess;
if ( guess < x )
cout << "guess higher\n";
elseif (guess > x )
cout << "guess lower\n";
elseif (guess == x)
cout << "jackpot\n";
else cout << "invalid\n";
}while (guess != x);
if (guess == x)
{
for(x = 1; x < 5; x++)
cout << "\aJackpot" << endl;
}
cout << "Do you want to play again? y or n\n";
cin >> again;
if(again == 'y')
{
main(); // trying to call the main function again to repeat the program
}else
cout << "Thanks for playing.\n";
system ("PAUSE");
return 0;
}while (again == 'y');
system("PAUSE");
return 0;
}
Why do you still have the call to main in there? That's nonsensical; why put in a do-while loop to loop around all of main if you're not going to use it?