Not sure why this isnt working..

Let me start off by saying that I just started teaching myself c++ a few days ago. I also just started a college course today actually. With that said...

I created a simple guessing game. It worked great so i wanted to add a little complexity to it. So i have the game start by introducing himself and asking what your name is. Once you enter your name it says lets play a game.

Now as i have it right now. The game runs through once great and ends properly. But once it gets to the "would you like to play again prompt...i hit Y - and nothing happens. Now i know you think this is a simple answer but..

I didnt want it to start back at the very begining. I didnt want it to say hi my name is HAL. I just wanted it to go straight back to the guessing game. So what i did was created another .cpp file and linked it with a header file. But it is not working....

Here is rthe header:

#ifndef submain_h
#define submain_h

int submain();

#endif

Here is my main:

#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <string>
#include <time.h>
#include "submain.h"

int main()
{
using namespace std;
int guess = 0;
int comp = 0;
int tries = 5;
char answer = 1;
char name[10];
int guess1 = 0;
int comp1 = 0;
int tries1 = 5;
char answer1 = 1;

comp = rand()% + time (0) %100;

cout << "Hello my name is HAL. What is your name?" << endl << endl;
cin >> name;
cout << endl;
cout << "Nice to meet you " << name << " . Let's play a game? " << endl << endl;
cout << "I have chosen a number between 0-100. Can you guess it?" << endl;
cout << "You have " << tries << " attempts to guess it! " << endl;

do{
cin >> guess;
if (guess < comp)
{
tries--;
cout << "I'm sorry. That number is too low. Please try again.." << endl;
cout << "You have " << tries << " more attempts to guess it! " << endl;
}
if (guess > comp)
{
tries--;
cout << "I'm sorry. That number is too high. Please try again.." << endl;
cout << "You have " << tries << " more attempts to guess it! " << endl;
}


} while (guess != comp && tries >=1);

if (guess == comp)
{
cout << endl;
cout << guess << " is the correct number! You have won!" << endl;
cout << endl;
}

else if (tries <= 1)
{
cout << endl;
cout << " I'm sorry. You are out of turns. The correct number was " << comp << " !" << endl << endl;
cout << " GAME OVER!" << endl << endl;
}

cout << "Press the Y key to play agian" << endl << endl;
cin >> answer;

if (answer == 'Y')
{
system("CLS");
submain();
}
else if (answer != 'Y')
system ("pause");
return 0;

}


Here is my second .cpp file called submain:


#include <iostream>

int submain()
{
using namespace std;


int guess1 = 0;
int comp1 = 0;
int tries1 = 5;
char answer1 = 1;


cout << "I have chosen a new number between 0-100. Can you guess it?" << endl;
cout << "You have " << tries1 << " attempts to guess it! " << endl;

do{
cin >> guess1;
if (guess1 < comp1)
{
tries1--;
cout << "I'm sorry. That number is too low. Please try again.." << endl;
cout << "You have " << tries1 << " more attempts to guess it! " << endl;

}
if (guess1 > comp1)
{
tries1--;
cout << "I'm sorry. That number is too high. Please try again.." << endl;
cout << "You have " << tries1 << " more attempts to guess it! " << endl;

}


} while (guess1 != comp1 && tries1 >=1);

if (guess1 == comp1)
{
cout << endl;
cout << guess1 << " is the correct number! You have won!" << endl;
cout << endl;

}

else if (tries1 <= 1)
{
cout << endl;
cout << " I'm sorry. You are out of turns. The correct number was " << comp1 << " !" << endl << endl;
cout << " GAME OVER!" << endl << endl;

}
do{
cout << "Press the Y key to play agian" << endl << endl;
cin >> answer1;

if (answer1 == 'Y')
{
system("CLS");
submain();
}
else if (answer1 != 'Y')
system ("pause");
return 0;

}while(answer1 != 'Y');

}

Any ideas what im doing wrong? Should i not be doing it this way?



But it is not working....


What does that mean?

Do you get a compiler error? If yes, what is the error?

Does the program run and crash? If yes, tell us where it crashes.

Does the program work but just not do what you want it to do? If yes, tell us what you want it to do and what it actually does.

It works through one cycle. Once it gets to the "would you like to play again" prompt - i press "y" and nothing happens. It just sits there.

What i want it to do is start back at :

cout << "I have chosen a new number between 0-100. Can you guess it?" << endl;
cout << "You have " << tries1 << " attempts to guess it! " << endl;


I'm trying to get it so it wont start back at the very begining...
i press "y" and nothing happens. It just sits there.

Your code is checking against 'Y'. Not 'y'.

Note that by calling submain within submain, and then calling submain, and then calling submain and again and again and again, you will be going deeper and deeper and deeper, with every level carefully stored on the stack, until you finally run out of memory on the stack and it crashes. This is not BASIC; a function call is not just moving the point of execution to somewhere else and carrying on from there.
Last edited on
uugh.. i cant beleive i didnt see that! Thanks..

I understand what your saying about eventually running out of memory. But i honestly have no idea what i should do instead. I couldnt figure out how to get it to restart and not go all the way back to the begining without doing what i did..??
When you want to repeat some code, use a loop.

1
2
3
4
while( some_condition )
{
   // code that you want to repeat
}


The code in that block will keep repeating as long as some_condition is true
Topic archived. No new replies allowed.