Ask coding questions

← Back to all posts
my user inputs keep going to the wrong functions
MatthewMartin14 (0)

im making a c++ game to self learn c++ but i keep getting this strange bug when i ask the user for and input it will sometimes go to a completely different function mainly the when press 4 to goto train(); it takes you to fight(); instead i really don't understand this and i need help

Answered by m4rus3ru (15) [earned 5 cycles]
View Answer
MatthewMartin14 (0)

wait the problem is still there

m4rus3ru (15)

I did take a look at your code and your switch statement is correct now.
Please describe your problem a little further, on which action on which step does what outcome happen, that you didn't want to happen.

MatthewMartin14 (0)

@m4rus3ru ok so when you enter 4 it should send you to train(); but instead you go to fight(); and i really dont know why this happens

m4rus3ru (15)

@MatthewMartin14 I did just run your game end after the first fight it took me to the hub an I selected 4. That took me to the training.

Welcome to the hub!
Please select 1 of the 4 options:4

welcome to training type 1 to train and 2 to leave
MatthewMartin14 (0)

@m4rus3ru the options below welcome to training type 1 to train and 2 to leave are not the train(); options it put take a look at line 121 for a better understanding

m4rus3ru (15)

@MatthewMartin14 Nevermind, I now see what you mean.

Let's take a look at what happens:

Firt start() gets run:
as t == 0 you run hubt() next.
hubt(), after some output sends you to prep().
In prep() you start fightp() after some output.
Now in fightp() you output some things set md=25 and then call fight().
And this is where it gets interesting.

So let's take a look at fight():

1    void fight() {
2      if (hp <= 0) {
3        lose();
4      }
5      if (mh <= 0) {
6        win();
7      }
8      std::cout << "\n1.fight\\n3.defend.\n";
9      int x = 0;
11      std::cin >> x;
12      if (x == 1) {
13        attack();
14      } else if (x == 2) {
15        run();
16      } else if (x == 3) {
17        block();
18      } else {
19        std::cout << "\n";
20        std::cout << "thats not an option choose again";
21        fight();
22      }
23    }

I added line numbers so we can easily discuss further.
This function loops over and over again until it either line 2 or line 5 is true.
Here you then call lose() or win(). So far so good, but with just one small problem:
The function does not stop there. As soon as lose() or win() is done with what it does, fight() will continue on line 8 and eventually call fight() again. The solution to this is using return; after line 3 and line 6.

MatthewMartin14 (0)

@m4rus3ru well now im confused what does the return statement do to fix the problem?
i ask this because i never really understood what the return statement does in general plus i need to know so that i can fix this problem if it happens again in the future

MatthewMartin14 (0)

@m4rus3ru also when you said md = 25 that scared me a bit because md stands for monster damage mh is monster health but i saw that was just a typo lol

m4rus3ru (15)

@MatthewMartin14 The return statement usually does return a value as output of the function and it then ends right there.

As your function is a void you can just use return; which will then result in the function ending right there and nothing being returned by it. A function can however also have other datatypes, like int, an example for that would be:

int someFunction(){
    std::cout << "I was called!" << endl;
    return 5;

This function always return the value 5, you could call it from somewhere like this:

int myint = someFunction();
std::cout << "The value of myint is " << myint << endl;

The output of running this would be:

I was called!
The value of myint is 5
MatthewMartin14 (0)

@m4rus3ru so how did it stop a the loop problem when all it does is basically act as an print statement?

m4rus3ru (15)

@MatthewMartin14 It does not act like a print statement. It just returns the value as the output of the function. If you do not use that value somewhere outside of the function it will never be used.

If you run this:

int main(){
   std::cout << "Hello!" << endl;
   return 0;
   std::cout << "Good bye!" << endl;

You will see that it only prints out "Hello!" as it stops right there at the return statement.
Wherever return gets called in your function it stops the functions code right there.

MatthewMartin14 (0)

@m4rus3ru oh i see would be a good idea to put this after every other time a function gets called?

m4rus3ru (15)

@MatthewMartin14 It is a good idea to put it everywhere, where you want to stop the execution of the current function you are in. If your call to the other function is the last statement in the current function, then no return will be required.
Ther are also use cases where you do not want the first function to stop, but just pause while the other function gets called, there you of course do not use return too.

MatthewMartin14 (0)

@m4rus3ru alright thank you so much this really helped and i figured out on some of the inputs i forgot to even ask for one so thanks for helping me realize that

MatthewMartin14 (0)

@m4rus3ru also if you can could you tell me how to loop back to the switch what i mean by this is: you trained for a bit
welcome to training type 1 to train and 2 to leave11
when i entered the wrong amount of numbers the program stopped how do i add an else statement but with switch instead of if

m4rus3ru (15)


int x;
cout << "1 or 2?" << endl;
    cin >> x;
        case 1:
            cout << "train..." << endl;
        case 2:
            cout << "leave..." << endl;
            cout << "what was that? I need 1 or 2!" << endl;

So we do loop something using while([condition]) as long as the condition equals true this will run forever unless interrupted.

We do loop until we eventually return and therefor terminate any further execution of code of that function after a valid value was entered on this way.
default: gets triggered whenever the input does not match any of the cases.

MatthewMartin14 (0)

@m4rus3ru instead of doing a while loo could i just do defualt:
cout << "what was that? I need 1 or 2!" << endl;

m4rus3ru (15)

@MatthewMartin14 Yes, that would be possible too, but it would be a smart idea to learn about doing loops and how to use them too ;)

m4rus3ru (15)

In your switch you didn't use break; to end the case.

In case 1 each of these methods will be called (in that order):
What you want to do instead is:

switch(x) {
    case 1:
    case 2: ...
m4rus3ru (15)

I would also recommend using namespace std; and using multiple << operators in one line instead of breaking it up into multiple lines without any need.
It is also recommended to use endl instead of \n as endl flushes the output buffer and \n does not.

EuanTH (4)

When using switch case don't forget to use breaks, otherwise it will move on to the next case. I.e.

int x = 2;
std::cout<< "Case 1" << std::endl;
std::cout<< "Case 2" << std::endl;
EuanTH (4)

Furthermore, you can reduce the amount of lines your program has by doing

std::cout << "\n" << "Hello" << "\n" << " World!;

rather than

std::cout << "Hello";
std::cout << "\n";
std::cout << " World!";

I recommend to look into using classes to house all your player variables; videos by The Cherno or thenewboston helped me out! Happy coding!

MatthewMartin14 (0)

@EuanTH i would normally do that i just copied that line and pasted it when needed to save time so i dont have to keep retyping the line