[SOLVED] Console application question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

[SOLVED] Console application question

Post by Kyosaur »

I decided to take up c++ and make a simple console game. Im just wondering if there is a way to stop the console from closing until i call a certain function (maybe OnGameModeEnd) ? I know of system("pause"); but its not exactly what im looking for.
Last edited by Kyosaur on Sun Jul 18, 2010 2:29 am, edited 3 times in total.
Image
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: Console application question

Post by zeid »

Here is a simple solution. The program is sequential so what we do is return to the start of the program so long as the conditions to exit haven't been met.

Code: Select all

#include<conio.h>

void main()
{
	bool exit=false;
	while(!exit)
	{
		if(kbhit())
			if(getch()=='q')
				exit=true;
	}
}
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: Console application question

Post by Kyosaur »

zeid wrote:Here is a simple solution. The program is sequential so what we do is return to the start of the program so long as the conditions to exit haven't been met.

Code: Select all

#include<conio.h>

void main()
{
	bool exit=false;
	while(!exit)
	{
		if(kbhit())
			if(getch()=='q')
				exit=true;
	}
}
Ohh nice, thanks.


EDIT: Is there a way to make it accept input? Kind of hard to make a text based game if you cant input anything D:.
Image
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Console application question

Post by X Abstract X »

Kyosaur wrote:
zeid wrote:Here is a simple solution. The program is sequential so what we do is return to the start of the program so long as the conditions to exit haven't been met.

Code: Select all

#include<conio.h>

void main()
{
	bool exit=false;
	while(!exit)
	{
		if(kbhit())
			if(getch()=='q')
				exit=true;
	}
}
Ohh nice, thanks.


EDIT: Is there a way to make it accept input? Kind of hard to make a text based game if you cant input anything D:.
The example zeid posted does take in input. All you have to do is store it in a string. You might want to use std::cin though, it's standard C++.

Code: Select all

std::string input;

std::cout << "Enter your name.\n";
std::cin >> input;
std::cout << "Your name is: " << input << ".\n";
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: Console application question

Post by Kyosaur »

X Abstract X wrote:
Kyosaur wrote:
zeid wrote:Here is a simple solution. The program is sequential so what we do is return to the start of the program so long as the conditions to exit haven't been met.

Code: Select all

#include<conio.h>

void main()
{
	bool exit=false;
	while(!exit)
	{
		if(kbhit())
			if(getch()=='q')
				exit=true;
	}
}
Ohh nice, thanks.


EDIT: Is there a way to make it accept input? Kind of hard to make a text based game if you cant input anything D:.
The example zeid posted does take in input. All you have to do is store it in a string. You might want to use std::cin though, it's standard C++.

Code: Select all

std::string input;

std::cout << "Enter your name.\n";
std::cin >> input;
std::cout << "Your name is: " << input << ".\n";
Thanks, thats exactly what i was looking for :D.

Sorry for the n00by question lol, im still learning :(.
Image
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Console application question

Post by X Abstract X »

Everyone's been there, don't be afraid to ask anything.
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: Console application question

Post by Kyosaur »

X Abstract X wrote:Everyone's been there, don't be afraid to ask anything.
ok lol.

going to add solved to the tittle now, thanks again for the help guys :D.
Image
Post Reply