Beginner Needs Help

Started by
5 comments, last by wasp911 20 years, 8 months ago
I have been working my way through a C++ books and some tutorials. I have started a simple text game to practice my skills on. One problem I have run into is with my functions. I have a function that "moves" the character and one that displays the main menu. If you hit 1 from the main menu, it starts the game. N,S,E and W move and L displays your current location. Q is supposed to quit the function and return to the main menu. This works ok, but you have to press 5 (the char that kills the program itself) twice in order for the program to quit. Can someone look over my code and point out the errors? I would really appreciate it. Code is pasted below. #include <iostream> #include <stdlib.h> #include <iomanip> #include <conio.h> #include <stdlib.h> #include <windows.h> using namespace std; // function declarator(prototype) void startup_menu(); int main_menu(); void move(); void location(int, int); int main() { //main loop //call game title screen startup_menu(); return 0; }//end main void location(int xcoord, int ycoord) { cout << setw(73) << "Your location is " << xcoord << ", " << ycoord << endl; } // move functions moves your character around the game world // and displays its position in x,y coordinates. void move() { char dir = ''a''; int x = 10, y = 10; while(dir != ''q'') //while dir variable not equal to enter { //begin while dir = getch(); //get character //system("cls"); switch(dir)//switch on character { //begin switch case ''n'': y--; system("cls"); cout << "\nYou move North." << endl; break; //go north case ''s'': y++; cout << "\nYou move South." << endl; break; //go south case ''e'': x++; cout << "\nYou move East." << endl; break; //go east case ''w'': x--; cout << "\nYou move West." << endl; break; //go west case ''l'': location(x,y); break; case ''q'': system("cls"); main_menu(); break; //quit default: cout << "Try again"; //unknown character } //end switch if(x==7 && y==11) //if x is 7 and y is 11 { //begin if result code cout << "\nYou found the treasure!"; } //end if result code else if(x==12 && y==12) { cout << "\nYou have entered the town of Havenvale." << endl; } }//end while }//end function void startup_menu() { char startgame; startgame = ''@''; // title screen. press enter to go to game. while(startgame != ''\r'') { cout << endl << endl << endl << endl; cout << setw(45) << "DragonQuest" << endl << endl << setw(46) << "Press Enter!"; startgame = getch(); system("cls"); } main_menu(); } int main_menu() { //game menu cout << "*************" << endl << "Main Menu" << endl << "*************" << endl << "1) New Game" << endl << "2) Load Game" << endl << "3) Manual" << endl << "4) Credits" << endl << "5) Exit" << endl << "*************" << endl; while(1) { char choice; //take user''s choice and launch it choice = getch(); if(choice==''1'') { system("cls"); // calling move function. move(); } else if(choice==''5'') { //cout << "Exiting..." << endl; return 0; } } }
Advertisement
There are a number of things wrong here, but I'll stick to ones that are causing your current problem.

The first problem is this line in move():
    case 'q': system("cls"); main_menu(); break; //quit  


You shouldn't be calling main_menu() from here, since main menu is the function that called move() in the first place. What you want to do is return to the calling function, not call a new instance of it. So the line should be something like this:
    case 'q': system("cls"); return; //quit  


This alone won't fix the problem though, since the way main_menu() is written, it won't actually re-display the menu after move() has returned. You'll have to redesign the way main_menu is arranged to get it to work properly. I'll leave it to you to figure out how to do that.

By the way, this line:
default: cout << "Try again"; //unknown character
should be:
default: cout << "Try again" << endl; //unknown character



[edited by - Plasmadog on August 7, 2003 6:48:34 PM]
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
quote:
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>


Why do you have window.h? Its not a window program is it? You included two stdlib''s also.



WiseElben.com - Game programming tutorials, articles, and community.

E-mail:wiseelben@wiseelben.com
AIM: WiseElben
ICQ: 299127026

[edited by - GameDev Staff on September 27, 1989 9X:58:97 XMS] For violating Article 43 Page 456 Paragraph 251 Line .042]
Thanks for the help. I made some changes and it is working properly. I will post the updated code below. I know it is really crude, but I am stil trying to learn. If you spot anything else that is wrong or is bad practice and feel like letting me know, please do. If not, it''s cool. Thanks again.

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>

using namespace std;

// function declarator(prototype)
void startup_menu();
int main_menu();
void move();
void location(int, int);

int main()
{ //main loop
//call game title screen
startup_menu();

return 0;
}//end main

void location(int xcoord, int ycoord)
{
cout << setw(73) << "Your location is " << xcoord << ", " << ycoord << endl;
}

// move functions moves your character around the game world
// and displays its position in x,y coordinates.
void move()
{
char dir;
int x = 10, y = 10;

while(dir != ''q'') //while dir variable not equal to enter
{ //begin while
dir = getch(); //get character
//system("cls");
switch(dir)//switch on character
{ //begin switch
case ''n'': y--; system("cls"); cout << "\nYou move North." << endl; break; //go north
case ''s'': y++; cout << "\nYou move South." << endl; break; //go south
case ''e'': x++; cout << "\nYou move East." << endl; break; //go east
case ''w'': x--; cout << "\nYou move West." << endl; break; //go west
case ''l'': location(x,y); break;
case ''q'': system("cls"); return; //quit
default: cout << "Try again" << endl; //unknown character
} //end switch

if(x==7 && y==11) //if x is 7 and y is 11
{ //begin if result code
cout << "\nYou found the treasure!";
} //end if result code
else
if(x==12 && y==12)
{
cout << "\nYou have entered the town of Havenvale." << endl;
}
}//end while
}//end function

void startup_menu()
{
char startgame;

// title screen. press enter to go to game.
while(startgame != ''\r'')
{
cout << endl << endl << endl << endl;
cout << setw(45) << "DragonQuest" << endl
<< endl << setw(46) << "Press Enter!";
startgame = getch();
system("cls");
}
main_menu();
}

int main_menu()
{
while(1)
{
//game menu
cout << "*************" << endl
<< "Main Menu" << endl
<< "*************" << endl
<< "1) New Game" << endl
<< "2) Load Game" << endl
<< "3) Manual" << endl
<< "4) Credits" << endl
<< "5) Exit" << endl
<< "*************" << endl;

char choice;
//take user''s choice and launch it
choice = getch();
if(choice==''1'')
{
system("cls");
// calling move function.
move();
}
else
if(choice==''5'')
{
//cout << "Exiting..." << endl;
return 0;
}

}
}
I put headers I work with in the book I''m using as I go along. I like to try the various functions built in. The unnecessary ones need to be taken out, but I haven''t done so yet.

My code is probably hard on the eyes to people who know what they''re doing lol.
It''s not really a bug, but this line in move()...
while(dir != ''q'')
...is a bit pointless. If the user ever hits q, the function returns immediately, so dir will never be equal to q at this point of the loop. It may as well be while(1).

Also, the startup_menu function is doing more work than it has to. The menu text is printed inside the loop, so it is being printed over and over again. There is no need for this. It should be printed just once before the loop starts, and the cls() should take place after the loop ends.
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
Thanks again. Changed these functions. Hopefully they''re right.

void startup_menu()
{
char startgame;

// title screen. press enter to go to game.
cout << endl << endl << endl << endl;
cout << setw(45) << "DragonQuest" << endl
<< endl << setw(46) << "Press Enter!";

while(startgame != ''\r'')
{
startgame = getch();
}

system("cls");
main_menu();
}

void move()
{
char dir;
int x = 10, y = 10;

while(1)
{ //begin while
dir = getch(); //get character
//system("cls");
switch(dir)//switch on character
{ //begin switch
case ''n'': y--; system("cls"); cout << "\nYou move North." << endl; break; //go north
case ''s'': y++; system("cls"); cout << "\nYou move South." << endl; break; //go south
case ''e'': x++; system("cls"); cout << "\nYou move East." << endl; break; //go east
case ''w'': x--; system("cls"); cout << "\nYou move West." << endl; break; //go west
case ''l'': location(x,y); break;
case ''q'': system("cls"); return; //quit
default: system("cls"); cout << "Try again" << endl; //unknown character
}//end switch
}//end while
}//end function

int main_menu()
{
char choice;

while(1)
{
//game menu
cout << "*************" << endl
<< "Main Menu" << endl
<< "*************" << endl
<< "1) New Game" << endl
<< "2) Load Game" << endl
<< "3) Manual" << endl
<< "4) Credits" << endl
<< "5) Exit" << endl
<< "*************" << endl;

//take user''s choice and launch it
choice = getch();
if(choice==''1'')
{
system("cls");
// calling move function.
move();
}
else
if(choice==''5'')
{
//cout << "Exiting..." << endl;
return 0;
}
else
{
system("cls");
cout << "Invalid!" << endl;
}
}
return 1;
}
In a game like that above... he has the func load()... how would some one program a game wich you can save ...would you have to create your own program type... like a .sav or .watever... i dont know how it would actually load ... can ne 1 tell me how to have a game in wich you can load a saved "quest"...

This topic is closed to new replies.

Advertisement