Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ program ending too early

Tags:

c++

I am very new to c++, and i have written up this piece of code. Which is designed to go in this order.. 1. Asks for name then welcomes the person 2. asks for their weapon of choice 3. picks a random number and damages a panda

I have had all 3 of these steps working. Then i decided maybe i could change the range of my random number, by using variables in my rand() functions parenthesis. That did not work as planned, so i tried reverting back. Thanks in advance to any help received. I wouldnt have any idea how i could search for this through the internet, so i came here.. Hoping someone could spot my issue. I am using netbeans IDE.

My issue: it firstly asks for my name, then i input my name and it welcomes me. But then it finishes the code. Before even attempting the rest of the code. My thoughts are that i obviously missed something i should have changed back.

Welcome to panda hunter! Please enter your name: Darryl
Welcome!, Darryl!

RUN SUCCESSFUL (total time: 3s)

But i have looked over it many many times and cannot spot anything wrong. Also my thoughts are that there is something wrong with this line, because this is where it fails to do and get further:

    cout << "Pick your weapon of choice! Then press enter to attack: ";

. Here is the whole files contents:

#include <iostream>
#include <cstdlib>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>

using namespace std;

string getName(){
    string name;
    cin >> name;
    return name;
}
string weaponChoice(){
    string weapon;
    cin >> weapon;
    return weapon;
}
int rand(){
    int damagePanda = rand() % 20 + 1;
    return damagePanda;
}
int main() {

    srand(time(0));
    int pandaHealth = 100;
    int userHealth = 100;   


    cout << ("Welcome to panda hunter! Please enter your name: ");
    cout << "Welcome!, " << getName() << "!" << endl;
    cout << "Pick your weapon of choice! Then press enter to attack: ";
    cout << "You surprise the panda with your " << weaponChoice() << ", dealing " <<   rand() << " damage!";
    pandaHealth = pandaHealth - rand();
    cout << "Panda has " << pandaHealth << " health remaining";

    char f;
    cin >> f;
    return 0; 
}
like image 763
Darryl Chapman Avatar asked Dec 26 '22 03:12

Darryl Chapman


1 Answers

int rand(){
    int damagePanda = rand() % 20 + 1;
    return damagePanda;
}

Recursive call. You are probably blowing your stack here.

The compiler should have warned you here! Not sure why it didn't.

Change to

int myrand(){
    int damagePanda = rand() % 20 + 1;
    return damagePanda;
}

Also change

cout << "You surprise the panda with your " 
<< weaponChoice() << ", dealing " <<   rand() << " damage!";

to

cout << "You surprise the panda with your "  
<< weaponChoice() << ", dealing " <<   myrand() << " damage!";

This also probably needs to change

pandaHealth = pandaHealth - rand();

This last change probably depends on your application logic - I haven't tried to understand it.

like image 149
user93353 Avatar answered Jan 11 '23 09:01

user93353