Q won't make C++ console app exit

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 1631 Views - Last Post: 29 September 2010 - 04:39 PM Rate Topic: -----

#1 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Q won't make C++ console app exit

Posted 20 September 2010 - 07:52 PM

In a game i made in a C++ console app i have a quit option on the main menu and i made a do while loop to see if q was typed and if it was to exit the program but i can't get it to work it compiles but q won't make the program exit.

Example

void main_menu()
{

//EDIT char option

 do {


// other stuff

}while (option !='q' || option !='Q'); 


That's just an example but that's how i have it setup.

Thanks

This post has been edited by Alex6788: 20 September 2010 - 08:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Q won't make C++ console app exit

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Q won't make C++ console app exit

Posted 20 September 2010 - 07:57 PM

How are you gathering the value to place into the variable 'option'?
Was This Post Helpful? 0
  • +
  • -

#3 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Q won't make C++ console app exit

Posted 20 September 2010 - 08:44 PM

View Postno2pencil, on 20 September 2010 - 08:57 PM, said:

How are you gathering the value to place into the variable 'option'?

Oh that was just an example i for got to put the char it's there though that's not the problem.

This post has been edited by Alex6788: 20 September 2010 - 08:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Q won't make C++ console app exit

Posted 20 September 2010 - 08:48 PM

We have to see exactly how you did it in order to tell you what's going amiss.
Was This Post Helpful? 0
  • +
  • -

#5 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Q won't make C++ console app exit

Posted 20 September 2010 - 09:04 PM

View PostKYA, on 20 September 2010 - 09:48 PM, said:

We have to see exactly how you did it in order to tell you what's going amiss.

Ok i'll post it but that is exactly how i did it i just took the stuff in the middle out.

CODE:

Main menu function
void main_menu(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno)
{

char input;

{do
	
system("cls");
cout << "Main Menu" << endl;
cout << "=====================" << endl;
cout << "P: Play Game" << endl;
cout << endl;
cout << "S: Stateics" << endl;
cout << endl;
cout << "C: Credits" << endl;
cout << endl;
cout << "Q: Quit" << endl;
cout << "=====================" << endl;
cout << "> ";
cin >> input;

switch ( input )
{
case 'p':
case 'P':
	{
		
diffselect(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
	}
	break;
case 's':
case 'S':
	{
		
stats(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
	}
	break;
case 'c':
case 'C':
	{
		system("cls");
		cout << "Credits:" << endl;
		cout << "==========================================" << endl;
		cout << "A Frowny Face Game :-(" << endl;
		cout << endl;
		cout << "Programming By: Alex Boyd" << endl;
		cout << "==========================================" << endl;
        cin.ignore();
		cin.get();
	}
break;
case 'q':
case 'Q':
	{
        cout << endl;
		cout << "Unrecognizable Figure: Good Bye!" << endl;
		Sleep(2000);
		
	}
	break;
default:
	{
		system("cls");
    cout << "Unrecognizable Figure: " << input << " is not a option.  Your options are as follows." << endl;
	cout << endl;
	cout << "VAILD OPTIONS ARE P, S, C, and Q!" << endl;
	cin.ignore();
	cin.get();
	 
    }
break;

}

}while (input !='q' || input !='Q');

}


This post has been edited by Alex6788: 20 September 2010 - 09:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Q won't make C++ console app exit

Posted 20 September 2010 - 09:04 PM

The stuff in the middle is important. Is the stream eating a newline or input? How exactly did you gather said input? so on and so forth...
Was This Post Helpful? 0
  • +
  • -

#7 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Q won't make C++ console app exit

Posted 20 September 2010 - 09:06 PM

View PostKYA, on 20 September 2010 - 10:04 PM, said:

The stuff in the middle is important. Is the stream eating a newline or input? How exactly did you gather said input? so on and so forth...

I posted the code.
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Q won't make C++ console app exit

Posted 20 September 2010 - 09:07 PM

Answer: Your conditional is this OR this.


Example: input is 'q', it meets one of the conditionals and the program continues.

q || Q
T T = T
T F = T
F T = T
F F = F


Make it AND.
Was This Post Helpful? 1
  • +
  • -

#9 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Q won't make C++ console app exit

Posted 20 September 2010 - 09:10 PM

I always suggest using toupper (declared in ctype.h or cctype header) rather than checking for two values.
Was This Post Helpful? 1
  • +
  • -

#10 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Q won't make C++ console app exit

Posted 20 September 2010 - 09:31 PM

View Postno2pencil, on 20 September 2010 - 10:10 PM, said:

I always suggest using toupper (declared in ctype.h or cctype header) rather than checking for two values.

Yeah i know i've been meaning to research on it.

This post has been edited by Alex6788: 20 September 2010 - 09:32 PM

Was This Post Helpful? 0
  • +
  • -

#11 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Q won't make C++ console app exit

Posted 21 September 2010 - 02:20 PM

View PostKYA, on 20 September 2010 - 10:07 PM, said:

Answer: Your conditional is this OR this.


Example: input is 'q', it meets one of the conditionals and the program continues.

q || Q
T T = T
T F = T
F T = T
F F = F


Make it AND.

I don't understand it sounds like you stopped in mid sentence

Quote

Make it AND.

Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Q won't make C++ console app exit

Posted 21 September 2010 - 02:33 PM

Make it AND not OR. AND is a noun in his sentence, not the word 'and'.
Was This Post Helpful? 1
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Q won't make C++ console app exit

Posted 21 September 2010 - 02:45 PM

&&
Was This Post Helpful? 1
  • +
  • -

#14 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Q won't make C++ console app exit

Posted 21 September 2010 - 08:14 PM

Oh ok i get it thanks. I'm getting a problem when the user enters q it quits the program but if you've played a game and then go back to the main menu and hit q it won't quit it just keeps saying play again.

Heres the code i have it in spoiler tags because it's 700 some lines.


//Header Files
//====================
#include "stdafx.h"
#include <iostream>
#include "cstdlib"
#include <fstream>        
#include "windows.h"
#include <cstdio>
#include <ctime>
#include <iomanip>
#include <sstream>
//=====================

using namespace std;

// function prototypes
//=====================================================================================================
void diffselect(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno);
void gamee(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno);
void gamem(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno);
void gameh(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno);
void main_menu(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno);
void stats(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno);
//=====================================================================================================

int main()
{


	system("title It's the guess a number game! v3.0"); // Set title of the window to "It's the guess a number game! v3.0

	system("color 0a"); // Set background to black and foreground aka text to light green.

    string name;
	char yesno;
	int gamesplayed = 0;
    int numinfile = 0;
    int totalnum = 0;
    int faildguesses = 0;

cout << "A Frowny Face Mini-Game :-(" << endl; //company name
cout << "*******************************" << endl;
cout << "Guess a number game 3.0!" << endl;
cout << endl;
cout << "Can you guess the number?" << endl;  //Silly slogan
cout << endl;
cout << "By: Alex" << endl;     // Who made it
cout << endl;
cout << "Frowny Face Games (c) 2010" << endl;    // Copyright
cout << "*******************************" << endl;  
cout << endl;
cout << "Press 'Enter' Button to Start" << endl;
cin.get();

  main_menu(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);

  srand( time( 0 ) );

  return 0;
}

void main_menu(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno)
{

char input;

do{

	
system("cls");
cout << "Main Menu" << endl;
cout << "=====================" << endl;
cout << "P: Play Game" << endl;
cout << endl;
cout << "S: Stateics" << endl;
cout << endl;
cout << "C: Credits" << endl;
cout << endl;
cout << "Q: Quit" << endl;
cout << "=====================" << endl;
cout << "> ";
cin >> input;

switch ( input )
{
case 'p':
case 'P':
	{
		
diffselect(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
	}
	break;
case 's':
case 'S':
	{
		
stats(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
	}
	break;
case 'c':
case 'C':
	{
		system("cls");
		cout << "Credits:" << endl;
		cout << "==========================================" << endl;
		cout << "A Frowny Face Game :-(" << endl;
		cout << endl;
		cout << "Programming By: Alex" << endl;
		cout << "==========================================" << endl;
        cin.ignore();
		cin.get();
	}
break;
case 'q':
case 'Q':
	{
        cout << endl;
		cout << "Unrecognizable Figure: Good Bye!" << endl;
		Sleep(2000);
		
	}
	break;
default:
	{
		system("cls");
    cout << "Unrecognizable Figure: " << input << " is not a option.  Your options are as follows." << endl;
	cout << endl;
	cout << "VAILD OPTIONS ARE P, S, C, and Q!" << endl;
	cin.ignore();
	cin.get();
	 
    }
break;

}

}while (input !='q' && input !='Q');

}


void stats(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno)
{


//Used for date and time
char sdate[9];

char stime[9];

_strdate_s( sdate );

_strtime_s( stime );
//Used for date and time

system("cls");
if( stats )
{

cin.ignore();
system("cls");
cout << "Stateics:" << endl;
cout << "===============================================" << endl;

ifstream viewdata("stats.txt", ifstream::in);

while (viewdata.good())
cout << (char) viewdata.get();

viewdata.close();
cin.get();
main_menu(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);

}
else
{   // If there was an error this saves it to the error log for later viewing.
	cerr << "ERROR: Unable to open file 'stats.txt' for reading." << endl; //If there is an error reading data to file this will be displayed and the error will be saved to the error log
	ofstream errorlog;
	errorlog.open("Error_log.txt", ios::app);
	errorlog << "Time: " << stime << "Date: " << sdate << "ERROR READING: There was an error reading the file 'stats.txt'" <<"\n";
	errorlog << "==============================================================================================================" <<"\n"; // Inserts that in between each entry so theres seperation
	errorlog.close();
}

}

void diffselect(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno)
{

//Used for date and time
char sdate[9];

char stime[9];

_strdate_s( sdate );

_strtime_s( stime );
//Used for date and time	

   
  char option;

   system("cls");
   cout << "Unrecognizable Figure: You there what's your name?" << endl;
   cout << endl;
   cout << "> ";
   cin.get();
   getline ( cin,name );
   
   system("cls");
   cout << "Unrecognizable Figure: What difficulty level would you like " << name << "?" << endl;
   cout << endl;
   cout << "Difficulty Level:" << endl;
   cout << "**********************************************" << endl;
   cout << "E: Easy ( Guess a number between 1 and 10 )" << endl;
   cout << endl;
   cout << "M: Medium ( Guess a number between 1 and 30 )" << endl;
   cout << endl;
   cout << "H: Hard ( Guess a number between 1 and 100 )" << endl;
   cout << "**********************************************" << endl;
   cout << endl;
   cout << "> ";
   cin >> option;
   cin.get();
 

   switch ( option )
   {

   case 'e':
   case 'E':
	   {
		   gamee(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
	   }
	   break;
   case 'm':
   case 'M':
	   {
		   gamem(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
		  
	   }
	   break;
   case 'h':
   case 'H':
	   {
       gameh(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);

	   }
	   break;
   default:
	   {
           system("cls");
		   cout << "Unrecognizable Figure: " << option << " is not an option!" << endl;
		   cout << endl;
		   cout << "Unrecognizable Figure: E for 'E'asy, M for 'M'edium, and H for 'H'ard Got it!" << endl;
		   cin.ignore();
		   cin.get();
	   }
	   break;
   }


	   }


void gamee(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno)
{

//Used for date and time
char sdate[9];

char stime[9];

_strdate_s( sdate );

_strtime_s( stime );
//Used for date and time	
	
	
   int guess, comguess =(rand()%10)+1;
   

   system("cls");
   cout << "Unrecognizable Figure: " << name << " i am thinking of a number between 1 and 10." << endl;
   cout << endl;
   cout << endl;
   cout << "Press 'Enter' Button to Start" << endl;
   cin.get();

for ( ;; )
{
   system("cls");
   cout << "What is your guess? ";
   cin >> guess;

if( !cin.good() || guess <1 || guess > 10 )
{ 
	cin.clear();
	cin.sync();
	system("cls");
	cout << "Unrecognizable Figure: " << name  << " " << guess << " is not in the rang between 1 and 10." << endl;
    cin.get();
	continue;
}
if ( guess < comguess) //if players guess is less than computers guess 
{
system("cls");
cout << "WRONG " << name << " TOO LOW!" << endl;
++faildguesses;
cin.ignore();
cin.get();
continue;//from top of for loop

	
}

else if ( guess > comguess ) //if players guess is more than computers guess
{
system("cls");
cout << "WRONG " << name << " TOO HIGH!" << endl;
++faildguesses;
cin.ignore();
cin.get();
continue;//from top of for loop


}
else if ( guess == comguess ) //if players guess is equal to the computers guess

  system("cls");
  cout << "***********************************************************" << endl;
  cout << "CORRECT! " << name << " The number is " << comguess << endl;
  cout << "***********************************************************" << endl;
  cout << endl;
  cout << "***********************************************************" << endl;
  cout << name << " you made " << faildguesses << " incorrect guesses" << endl;
  cout << "***********************************************************" << endl;
  cin.get();
  ++gamesplayed;

ifstream Getnum("numbers.txt", ifstream::in);
Getnum>>numinfile;
Getnum.close();
totalnum = numinfile + gamesplayed;

ofstream savenum;
savenum.open("numbers.txt");
savenum << totalnum <<"\n";
savenum.close();

if ( gamee )
{
ofstream savegame;
savegame.open("stats.txt", ios::app);
savegame << "Games played: " << gamesplayed <<"\n" <<"Name: " << name <<"\n";
savegame << "Time: " << stime << " Date: " << sdate <<"\n" << "Difficulty Level: Easy" <<"\n" << "Number to guess: " << guess <<"\n" << "Guesses Took: " << faildguesses <<"\n";
savegame << "===============================================" <<"\n"; // Inserts that in between each entry so theres seperation
savegame.close();
}
else
{
// If there was an error this saves the error to the error log for later viewing.
cerr << "ERROR: Unable to open file 'stats.txt' for writing." << endl; //If there is an error writing data to file this will be displayed and the error will be saved to the error log
ofstream errorlog;
errorlog.open("Error_log.txt", ios::app);
errorlog << "Time: " << stime << "Date: " << sdate << "ERROR WRITING: There was an error writing to the file 'stats.txt'" <<"\n";
errorlog << "==============================================================================================================" <<"\n"; // Inserts that in between each entry so theres seperation
errorlog.close();

}

   for ( ;; )
   {

    cout << endl;
	cout << "Unrecognizable Figure: Play again " << name << " Y/N?" << endl;
	cout << endl;	
	cout << "> ";
		cin >> yesno;
		cin.get();

    switch ( yesno )
    {
      case 'n': 
	  case 'N':

		  {
         
			main_menu(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
		  }
		  break;
      case 'y': 
	  case 'Y': 
		  {
		gamee(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
		  }
                break;
      default : 
		  {
			  system("cls");
		 cout << "Unrecognizable Figure: " << yesno << " isn't an option " << name << " Options are Y and N!" << endl;
		  continue;
		  cin.get();
		  }           
		  break;

	}

   }
}



}

   
void gamem(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno)
{

   // initial to zero before each game begins
   int guess, comguess =(rand()%30)+1;

//Used for date and time
char sdate[9];

char stime[9];

_strdate_s( sdate );

_strtime_s( stime );
//Used for date and time	


   system("cls");
   cout << "Unrecognizable Figure: " << name << " i am thinking of a number between 1 and 30." << endl;
   cout << endl;
   cout << endl;
   cout << "Press 'Enter' Button to Start" << endl;
   cin.get();

for ( ;; )
{
   system("cls");
   cout << "What is your guess? ";
   cin >> guess;

if( !cin.good() || guess <1 || guess > 30 )
{ 
	cin.clear();
	cin.sync();
	system("cls");
	cout << "Unrecognizable Figure: " << name  << " " << guess << " is not in the rang between 1 and 30." << endl;
    cin.get();
	continue;
}
if ( guess < comguess) //if players guess is less than computers guess 
{
system("cls");
cout << "WRONG " << name << " TOO LOW!" << endl;
++faildguesses;
cin.ignore();
cin.get();
continue;//from top of for loop

	
}

else if ( guess > comguess ) //if players guess is more than computers guess
{
system("cls");
cout << "WRONG " << name << " TOO HIGH!" << endl;
++faildguesses;
cin.ignore();
cin.get();
continue;//from top of for loop


}
else if ( guess == comguess ) //if players guess is equal to the computers guess

  system("cls");
  cout << "***********************************************************" << endl;
  cout << "CORRECT! " << name << " The number is " << comguess << endl;
  cout << "***********************************************************" << endl;
  cout << endl;
  cout << "***********************************************************" << endl;
  cout << name << " you made " << faildguesses << " incorrect guesses" << endl;
  cout << "***********************************************************" << endl;
  cin.get();
  ++gamesplayed;

ifstream Getnum("numbers.txt", ifstream::in);
Getnum>>numinfile;
Getnum.close();
totalnum = numinfile + gamesplayed;

ofstream savenum;
savenum.open("numbers.txt");
savenum << totalnum <<"\n";
savenum.close();

if ( gamem )
{
ofstream savegame;
savegame.open("stats.txt", ios::app);
savegame << "Games played: " << gamesplayed <<"\n" << "Name: " << name <<"\n";
savegame << "Time: " << stime << " Date: " << sdate <<"\n" << "Difficulty Level: Medium" <<"\n" << "Number to guess: " << guess <<"\n" << "Guesses Took: " << faildguesses <<"\n";
savegame << "===============================================" <<"\n"; // Inserts that in between each entry so theres seperation
savegame.close();
}
else
{
// If there was an error this saves the error to the error log for later viewing.
cerr << "ERROR: Unable to open file 'stats.txt' for writing." << endl; //If there is an error writing data to file this will be displayed and the error will be saved to the error log
ofstream errorlog;
errorlog.open("Error_log.txt", ios::app);
errorlog << "Time: " << stime << "Date: " << sdate << "ERROR WRITING: There was an error writing to the file 'stats.txt'" <<"\n";
errorlog << "==============================================================================================================" <<"\n"; // Inserts that in between each entry so theres seperation
errorlog.close();

}

  for ( ;; )
  {

    cout << endl;
	cout << "Unrecognizable Figure: Play again " << name << " Y/N?" << endl;
	cout << endl;	
	cout << "> ";
		cin >> yesno;
		cin.get();

    switch ( yesno )
    {
      case 'n': 
	  case 'N':

		  {
         
			main_menu(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
		  }
		  break;
      case 'y': 
	  case 'Y': 
		  {
		 gamem(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
		  }
                break;
      default : 
		  {
		  system("cls");
		  cout << "Unrecognizable Figure: " << yesno << " isn't an option " << name << " Options are Y and N!" << endl;
		  continue;
		  cin.get();
		  }           
		  break;

	}
  }
}



}


void gameh(int totalnum,int numinfile,int gamesplayed,int faildguesses,string& name,char& yesno)
{

   // initial to zero before each game begins
   int guess, comguess =(rand()%100)+1;


//Used for date and time
char sdate[9];

char stime[9];

_strdate_s( sdate );

_strtime_s( stime );
//Used for date and time	


   system("cls");
   cout << "Unrecognizable Figure: " << name << " i am thinking of a number between 1 and 100." << endl;
   cout << endl;
   cout << endl;
   cout << "Press 'Enter' Button to Start" << endl;
   cin.get();

for ( ;; )
{
   system("cls");
   cout << "What is your guess? ";
   cin >> guess;

if( !cin.good() || guess <1 || guess > 100 )
{ 
	cin.clear();
	cin.sync();
	system("cls");
	cout << "Unrecognizable Figure: " << name  << " " << guess << " is not in the rang between 1 and 100." << endl;
    cin.get();
	continue;
}
if ( guess < comguess) //if players guess is less than computers guess 
{
system("cls");
cout << "WRONG " << name << " TOO LOW!" << endl;
++faildguesses;
cin.ignore();
cin.get();
continue;//from top of for loop

	
}

else if ( guess > comguess ) //if players guess is more than computers guess
{
system("cls");
cout << "WRONG " << name << " TOO HIGH!" << endl;
++faildguesses;
cin.ignore();
cin.get();
continue;//from top of for loop


}
else if ( guess == comguess ) //if players guess is equal to the computers guess

  system("cls");
  cout << "***********************************************************" << endl;
  cout << "CORRECT! " << name << " The number is " << comguess << endl;
  cout << "***********************************************************" << endl;
  cout << endl;
  cout << "***********************************************************" << endl;
  cout << name << " you made " << faildguesses << " incorrect guesses" << endl;
  cout << "***********************************************************" << endl;
  cin.get();
  ++gamesplayed;

ifstream Getnum("numbers.txt", ifstream::in);
Getnum>>numinfile;
Getnum.close();
totalnum = numinfile + gamesplayed;

ofstream savenum;
savenum.open("numbers.txt");
savenum << totalnum <<"\n";
savenum.close();

if ( gamem )
{
ofstream savegame;
savegame.open("stats.txt", ios::app);
savegame << "Games played: " << gamesplayed <<"\n" << "Name: " << name <<"\n";
savegame << "Time: " << stime << " Date: " << sdate <<"\n" << "Difficulty Level: Hard" <<"\n" << "Number to guess: " << guess <<"\n" << "Guesses Took: " << faildguesses <<"\n";
savegame << "===============================================" <<"\n"; // Inserts that in between each entry so theres seperation
savegame.close();
}
else
{
// If there was an error this saves the error to the error log for later viewing.
cerr << "ERROR: Unable to open file 'stats.txt' for writing." << endl; //If there is an error writing data to file this will be displayed and the error will be saved to the error log
ofstream errorlog;
errorlog.open("Error_log.txt", ios::app);
errorlog << "Time: " << stime << "Date: " << sdate << "ERROR WRITING: There was an error writing to the file 'stats.txt'" <<"\n";
errorlog << "==============================================================================================================" <<"\n"; // Inserts that in between each entry so theres seperation
errorlog.close();

}

for ( ;; )
{

    cout << endl;
	cout << "Unrecognizable Figure: Play again " << name << " Y/N?" << endl;
	cout << endl;
		cout << "> ";
		cin >> yesno;
		cin.get();

    switch ( yesno )
    {
      case 'n': 
	  case 'N':

		  {
         
			main_menu(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
		  }
		  break;
      case 'y': 
	  case 'Y': 
		  {
		 gameh(totalnum,numinfile,gamesplayed,faildguesses,name,yesno);
		  }
                break;
      default : 
		  {
		  system("cls");
		  cout << "Unrecognizable Figure: " << yesno << " isn't an option " << name << " Options are Y and N!" << endl;
		  continue;
		  cin.get();
		  }           
		  break;

	}

}

}



}


This post has been edited by Alex6788: 21 September 2010 - 08:25 PM

Was This Post Helpful? 0
  • +
  • -

#15 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Q won't make C++ console app exit

Posted 21 September 2010 - 08:19 PM

Your menu should function is doing more than it should. If structured correctly it should just display the menu, then prompt outside the scope of the menu, display then menu while not quit. You're trying to force too much responsibility into one function. Logically, why have only one function inside of main?

main() {
  menu();
  return 0;
}


Doesn't make sense.

** Edit **
You do realize the putting into spoiler is extremely annoying... the code tags are condensed & scrollable. Putting into spoiler just makes people not want to bother reading it.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3