Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

scem0

macrumors 604
Original poster
Jul 16, 2002
7,028
1
back in NYC!
Can someone explain these two functions to me? I don't
understand how to use them correctly.

Here is a very simple program, and all I want it to do is return
a the value for what they enter:

Code:
#include <iostream.h>
#include <process.h>
#include <conio.h>

int menu();

void main()
{
int ans;

ans = menu();

// menu function after main section

if (ans == 1)
cout << "1";

else if (ans == 2)
cout << "2";

else if (ans == 3)
cout << "3";

else if (ans == 4)
cout << "4";





}

int menu()
{
	cout << "+----------------------------+ \n";
	cout <<	"|           DODGER           | \n";
	cout <<	"+----------------------------+ \n";
	cout << "|  WHERE WOULD YOU LIKE TO   | \n";
	cout << "|         GO TODAY?          | \n";
	cout << "+----------------------------+ \n";
	cout << "|1:     Play the game        | \n";
	cout << "+----------------------------+ \n";
	cout << "|2:     Instructions         | \n";
	cout << "+----------------------------+ \n";
	cout << "|3:         Quit             | \n";
	cout << "+----------------------------+ \n";
	cout << "|  Press the number that     | \n";
	cout << "| corresponds to your choice | \n";
	cout << "+----------------------------+ \n";
if(getch()==1)
{
	return 1;
}
else if (getch() == 2)
{
	return 2;
}
else if (getch()==3)
{
	return 3;
}
else return 4;	
}
I know I used getch wrong, because I don't know how to use it.
Can someone help?
 
Within menu()

use a variable: char choice

use the cin input stream instead of getch--it's not tied to x86 machines.

cin >> choice;

then

return choice; (you'll need to change menu to return a char)

in main()

You can simply

cout << menu();

to get and display the menu choice

(and yes, you do have to figure out a few things--I don't do other people's work for them. :))
 
I wanted it to where they didn't have to press their choice and
then press enter. I wanted them to just press the number
of their choice and they are returned back to the main function.
Hence, I wouldn't have an extra variable (choice).

Plus, that is too ez. :D
 
Okay, I'm back. I got getch to work, but I am
still lost on kbhit. But I will explain how getch
works so someone might not have to go through
what I went through to find it.

You have to initialize a variable, I'll call it scemo :D
and then set it equal to getch(), and the
character they input will be set to 'scem0'.
So it would look kind of like this:
Code:
#include <blah>
...

void main (void)
{
    char scemo;

cout << "Please enter a character" << endl;
scemo = getch();

cout << "The letter you typed in was " <<
     scemo << endl;
}

So, it is a lot easier than I thought.

I still don't understand kbhit. Can anyone
explain it? Or could someone direct me to
a website that explains it?

Help would be appreciated, because this is for
a computer science project. ;)
 
Okay, I figured out getch and kbhit, but now I have one more '?':

How can I fill in the values for a 2 dimensional array when I
initialize it?

Will this work?
Code:
char game[27][10] =
    {
    {+,-,-,-,-,-,-,-,-,-,-,-,+,-,+,-,-,-,-,-,-,-,-,-,-,-,+}
    {|,>,|,|,|,|,|,|,|,|,|,|,|,' ',|,|,|,|,|,|,|,|,|,|,|,<,|}
	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
    {|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
	{|,>,|,|,|,|,|,|,|,|,|,|,|,' ' ,|,|,|,|,|,|,|,|,|,|,|,<,|}
	{+,-,-,-,-,-,-,-,-,-,-,-,+,-,+,-,-,-,-,-,-,-,-,-,-,-,+}
    };
 
Originally posted by GeeYouEye
I think you have the right idea, but since they're of type char, I think you need single quotes around each one. Could be wrong though.

You're right...he needs apostrophes.
 
y'all were correct...... It took forever, but thank god for 'find and
replace' ;). I have everything working now, except it is sooooo
slow on old computers. It works great on my p4 2.4 GHz, but
it sucks on our schools pentium 2's. :( :rolleyes: :eek:.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.