Help on a game.

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

Rhys
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Mon Mar 09, 2009 12:08 am
Current Project: Learning from my C++ book.
Favorite Gaming Platforms: PC. Lurv it.
Programming Language of Choice: C++.
Location: Melbourne, Australia.

Help on a game.

Post by Rhys »

Hi,

I haven't been programming for very long, and this is my first project that has a goal, and isn't copied from a tutorial. The game I'm trying to make is simple, you couldn't get it wrong if you tried, but it's just me teaching myself. What I want to happen is for the user to input a value for x, then output a question, like "is x > 5?", then the user inputs Y or N for confirmation.

I know how to do "if x > 5" then output this or that, but I don't know how to get input.
Here's the source;

Code: Select all

#include <string> 
#include <iostream>
using namespace std;

int main()

{
	// Variables.
	string input;
	float x = 0.0;
	float y = 0.0;
	float z = 0.0;

	// Main Function.
	system("TITLE The Number Game!");
	
	cout << "The Number Game!" << endl;
	cout << "To play the number game, enter a number between 1 and 10." << endl << endl;
	cin >> x;
	cout << endl << endl;
	cout << "Number entered: " << x << endl;
	cout << "Let's begin the game. You need to answer a series or questions using Y and N. These resemble Yes and NO."
		" You must use capitals. Here comes the first question." << endl << endl;

	cout << "Is " << x << "larger than five(5)?" << endl;

	if (x > 5){
		cout << "Input your answer." << endl;
	} else {
		cout << "Input your answer." << endl;
	}
		cin >> input;

		if (input == "Y"){
			cout << "Correct!";
		} else {
			cout << "Wrong.";
		}


	system("pause");
	return 0;

}
Sorry if this is in the wrong place or not allowed, I'm no regular, but if it's wrong just say, if not then your help is appreciated.

Edit: Was also wondering how to remove that "Press any key to continue" at the end of console applications.
gordon
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 60
Joined: Mon May 04, 2009 2:38 pm

Re: Help on a game.

Post by gordon »

I would help you with the program but I'm a little busy so I'll just answer one of your questions.
Edit: Was also wondering how to remove that "Press any key to continue" at the end of console applications.
The message comes with the:

Code: Select all

system("PAUSE");
Remove that and your program will end without requiring the user to press a key and without a message.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Help on a game.

Post by Falco Girgis »

You "don't know how" to get input? It looks like you do. You're using cin...
gordon
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 60
Joined: Mon May 04, 2009 2:38 pm

Re: Help on a game.

Post by gordon »

Code: Select all

       cout << "Is " << x << "larger than five(5)?" << endl;

       if (x > 5){
          cout << "Input your answer." << endl;
       } else {
          cout << "Input your answer." << endl;
       }
          cin >> input;

          if (input == "Y"){
             cout << "Correct!";
          } else {
             cout << "Wrong.";
          }

    }
Since gyrovorbis has concluded that you do know how to use input I'll suggest how you could achieve your goal. I have taken the section above as I think it is what needs changed, checking the input for Y or N should be done within an if statement that checked whether the number was above or below 5. Right now it outputs yes or no , regardless of the number entered.

Something like this should be done to that section:

Code: Select all

       cout << "Is " << x << "larger than five(5)?" << endl;

          cout << "Input your answer." << endl;
          cin >> input;

                if (x > 5){
          
                   if (input == "Y"){
                      cout << "Correct!";
                   } else {
                     cout << "Wrong.";
                   }


       } else {

          if (input == "N"){
          cout << "Correct!";
          } else {
          cout << "Wrong.";
           }
       }


      

    }
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Help on a game.

Post by MadPumpkin »

looks they're helping you just fine :D
by the way...
you could have named your thread anything you want
and a lot of people would assume it was for a game haha
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
Kros
Chaos Rift Regular
Chaos Rift Regular
Posts: 136
Joined: Tue Feb 24, 2009 4:01 pm
Current Project: N/A
Favorite Gaming Platforms: PC, Playstation/2/3
Programming Language of Choice: C++
Location: Oregon,USA
Contact:

Re: Help on a game.

Post by Kros »

gordon wrote:I would help you with the program but I'm a little busy so I'll just answer one of your questions.
Edit: Was also wondering how to remove that "Press any key to continue" at the end of console applications.
The message comes with the:

Code: Select all

system("PAUSE");
Remove that and your program will end without requiring the user to press a key and without a message.
To further elaborate, removing the pause command isn't going to immediately fix things. Without that there, it'll likely exit as soon as your final cout occurs. If this is what you want, then by all means, just remove the system("pause");. Otherwise, you need to replace it with something like:

Code: Select all

	cin.ignore();
	cin.get();

	return 0;
This will stop the program from exiting until a key is pressed but, no message will show.
gordon wrote: Something like this should be done to that section:

Code: Select all

       cout << "Is " << x << "larger than five(5)?" << endl;

          cout << "Input your answer." << endl;
          cin >> input;

                if (x > 5){
          
                   if (input == "Y"){
                      cout << "Correct!";
                   } else {
                     cout << "Wrong.";
                   }


       } else {

          if (input == "N"){
          cout << "Correct!";
          } else {
          cout << "Wrong.";
           }
       }


      

    }
That is some crazy weird indentation. ;)
Isaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
YouTube Channel
gordon
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 60
Joined: Mon May 04, 2009 2:38 pm

Re: Help on a game.

Post by gordon »

That is some crazy weird indentation. ;)
Yeah I apologise for that. TAB , SPACE etc can do unexpected things within a webbrowser.....
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Help on a game.

Post by MarauderIIC »

This

Code: Select all

                if (x > 5){
         
                   if (input == "Y"){
                      cout << "Correct!";
                   } else {
                     cout << "Wrong.";
                   }


       } else {

          if (input == "N"){
          cout << "Correct!";
          } else {
          cout << "Wrong.";
           }
       }
could be condensed to

Code: Select all

//"Correct" if x is greater than 5 and the user answers 'yes'
//"Correct" if x is less than 5 and the user answers 'no'
                   if ( (input == "Y" && x > 5) || (input == "N" && x < 5) ){
                      cout << "Correct!";
                   } else {
                     cout << "Wrong.";
                   }
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
gordon
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 60
Joined: Mon May 04, 2009 2:38 pm

Re: Help on a game.

Post by gordon »

MarauderIIC wrote:This

Code: Select all

                if (x > 5){
         
                   if (input == "Y"){
                      cout << "Correct!";
                   } else {
                     cout << "Wrong.";
                   }


       } else {

          if (input == "N"){
          cout << "Correct!";
          } else {
          cout << "Wrong.";
           }
       }
could be condensed to

Code: Select all

//"Correct" if x is greater than 5 and the user answers 'yes'
//"Correct" if x is less than 5 and the user answers 'no'
                   if ( (input == "Y" && x > 5) || (input == "N" && x < 5) ){
                      cout << "Correct!";
                   } else {
                     cout << "Wrong.";
                   }
good call.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Help on a game.

Post by Ginto8 »

I would HIGHLY frown upon this, but for the sake of conversation I'd like to point out that the DOS command "pause" can take the parameter ">nul", which stops "press any key to continue..." from showing, so it is possible to do system("pause >nul") to solve your problem. However, getchar(), getch(), and cin.sync() with cin.get() are all MUCH better choices. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Rhys
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Mon Mar 09, 2009 12:08 am
Current Project: Learning from my C++ book.
Favorite Gaming Platforms: PC. Lurv it.
Programming Language of Choice: C++.
Location: Melbourne, Australia.

Re: Help on a game.

Post by Rhys »

MarauderIIC wrote:

Code: Select all

 //"Correct" if x is greater than 5 and the user answers 'yes'
    //"Correct" if x is less than 5 and the user answers 'no'
                       if ( (input == "Y" && x > 5) || (input == "N" && x < 5) ){
                          cout << "Correct!";
                       } else {
                         cout << "Wrong.";
                       }
When I implement this, (whole code);

Code: Select all

#include <string> 
#include <iostream>
using namespace std;

int main()

{
	// Variables.
	string input;
	float x = 0.0;
	float y = 0.0;
	float z = 0.0;

	// Main Function.
	system("TITLE The Number Game!");
	
	cout << "The Number Game!" << endl;
	cout << "To play the number game, enter a number between 1 and 10." << endl << endl;
	cin >> x;
	cout << endl << endl;
	cout << "Number entered: " << x << endl;
	cout << "Let's begin the game. You need to answer a series or questions using Y and N. These resemble Yes and NO."
		" You must use capitals. Here comes the first question." << endl << endl;

	cout << "Is " << x << "larger than five(5)?" << endl;
if ( (input == "Y" && x > 5) || (input == "N" && x < 5) ){
                      cout << "Correct!";
                   } else {
                     cout << "Wrong.";
                   }
		
	cin.ignore();
	cin.get();
	return 0;
I don't get to answer the question is x > 5. It simply states "Wrong", even when x is larger than 5.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: Help on a game.

Post by herby490 »

Here try this code I changed a few things about it.
First i created a char that is used for input of the choice. I also made it so it will see if the input is greater than or equal the the choice so if the user selects five he won't be wrong every time.

Code: Select all

#include <string>
#include <iostream>
using namespace std;

int main()

{
   // Variables.
   string input;
   double x = 0.0;
   float y = 0.0;
   float z = 0.0;
	char a;
   // Main Function.
   system("TITLE The Number Game!");
   
   cout << "The Number Game!" << endl;
   cout << "To play the number game, enter a number between 1 and 10." << endl << endl;
   cin >> x;
   cout << endl << endl;
   cout << "Number entered: " << x << endl;
   cout << "Let's begin the game. You need to answer a series or questions using Y and N. These resemble Yes and NO."
    << " You must use capitals. Here comes the first question." << endl << endl;
cin >> a;
   cout << "Is " << x << " larger than five(5)?" << endl;
if ( ( a == 'Y' && x >= 5) || (a == 'N' && x < 5) ){
                      cout << "Correct!";
                   } else {
                     cout << "Wrong.";
                   }
      
   cin.ignore();
   cin.get();
   return 0;
}
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Help on a game.

Post by dandymcgee »

Did you fix the "Press any key to continue..." message?

If it still does it without the System("pause") and you are using Code::Blocks, try unchecking "Pause when execution ends" in the "Build Targets" tab of the Project->Properties window.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Help on a game.

Post by MarauderIIC »

Rhys: probably because it's caps-sensitive.... and it looks like you never getline(cin, input), so input is never assigned. Oh yeah, and < 5 should be <= 5.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Rhys
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Mon Mar 09, 2009 12:08 am
Current Project: Learning from my C++ book.
Favorite Gaming Platforms: PC. Lurv it.
Programming Language of Choice: C++.
Location: Melbourne, Australia.

Re: Help on a game.

Post by Rhys »

Thanks for the help, the programming book I ordered just arrived, that should help. :)
Post Reply