Jump to content
  • Advertisement
Sign in to follow this  
Homicidal

Issues with leveling up in a C++ text adventure/rpg

This topic is 3900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Heres what I have, when you type advance it says what do you want to advance, ~!~ to stop, and if you type ~!~ it will stop. However when you try to advance health or anything else, it doesn't do anything and you can no longer type or do anything. void advance() { cout << "What do you want to advance?" << endl; cout << "~!~ to stop" << endl << endl; getline (cin, command); lower(); while (command != "~!~"); { if (command == "axe skill") { if (exp >= 8) { axe_skill = axe_skill + 1; exp = exp - 8; } else cout << "Not enough experience!" << endl << endl; } else if (command == "sword skill") { if (exp >= 8) { sword_skill = sword_skill + 1; exp = exp - 8; } else cout << "Not enough experience!" << endl << endl; } else if (command == "club skill") { if (exp >= 8) { blunt_skill = blunt_skill + 1; exp = exp - 8; } else cout << "Not enough experience!" << endl << endl; } else if (command == "health") { if (exp >= 3) { player_base_stats[0] = + 1; exp = exp -3; } else cout << "Not enough experience!" << endl << endl; } else if (command == "strength") { if (exp >= 8) { player_base_stats[1] = + 1; exp = exp - 8; } else cout << "Not enough experience!" << endl << endl; } } }

Share this post


Link to post
Share on other sites
Advertisement
What is the data type of 'command'? If it's a char *, then you are comparing the actual pointer to the string, which of course will never return true. If that's the case, you should try using std:string instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by Homicidal
Quote:
Original post by AdamGL
You can't type anything else because the getline() is outside the loop.

Nope... I just tried it and it didn't do anything...


Then you did something wrong. You don't have any code inside the loop that reads input. The input is only read once, outside the loop,

You should step through the program with a debugger to better understand the order of operations and to verify the contents of 'command'.

Share this post


Link to post
Share on other sites
You could check all of the variable scopes.
Try some debugging to see what happens after you type in a command.
You could just overhaul and recode it.
I usually add random cout's to places where i think a problem lies like after a function call to see if the function is the problem.

Share this post


Link to post
Share on other sites
Whats that lower () function you got ?
may be a problem in that ?

Anything to do with tolower (int) ?

You have this line....

player_base_stats[0] = + 1;

Use += 1 ; or = 1 ;

Make yourself a debug function and #define MYDBUG

Place that function in critical areas.
When you want to use the debug function define MYDBUG.

a debug function could be.....

(I cant find the code tags btw)

#include <iostream>
#include <fstream>

void my_debug ( char* file, char* sheet, int line )
{
std::ofstream outfile ;
outfile.open (file, std::ios::trunc ) ;

if (outfile.is_open()){
outfile << sheet << " : " << line ;
outfile.close () ;
}

else { std::cout << "\nerror on file open\n" ; }
}

source file code.....lalala....

#ifdef MYDBUG
my_debug ( "C:\\db.txt", "main.cpp", 27) ;
#endif

It should be fun when debugging.

If this posted message gets scrambled I'm sorry.

You seriously need to sort out your loop implementation.

[Edited by - codical on January 16, 2008 6:47:04 AM]

Share this post


Link to post
Share on other sites
Instead of 394892038408 if statements dont you think it would be easier to read/debug if you replaced them with switch statements?

Share this post


Link to post
Share on other sites
Quote:
Original post by AdamGL
You can't type anything else because the getline() is outside the loop.


I agree. The user never has a chance to enter anything after the loop begins, the command variable never changes.


void advance()
{
cout << "What do you want to advance?" << endl;
cout << "~!~ to stop" << endl << endl;
getline (cin, command); //user types "health" for example
lower();
while (command != "~!~");
{
//command == "health" Since the user never has a chance to alter his/her input, command will always == "health"
//The condition for breaking out of the loop will never be met and the program will get stuck here. Incidently,
//I'll bet if you toss in a breakpoint or cout and look at the player's health stat you'll find it increases with each loop
//the rest of the loop goes here



The solution is probably to move the getline command in the loop, before any of the checks:


void advance()
{
command = ""; //I assume command is declared elsewhere, so we want to make sure it's empty before we do any checking on it.
cout << "What do you want to advance?" << endl;
cout << "~!~ to stop" << endl << endl;
//getline (cin, command); We're moving this guy.
//lower(); this guy too, since I'm assuming he converts command to lower case?
while (command != "~!~");
{
getline(cin, command);
//Now the user has a chance to change his/her input at the beginning of each loop. They may type "health" this time
//and when we come back up here they'll get a chance to issue another command "blunt skill" or "~!~" for example.
lower(); //Convert to lower case (I assume)
...



Hope that helps!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!