C++ fighting game input help.

I'm making a fighting game...

Page 1 of 1

7 Replies - 2451 Views - Last Post: 26 August 2010 - 05:40 PM Rate Topic: -----

#1 Gus.bat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-September 09

C++ fighting game input help.

Posted 26 August 2010 - 12:37 PM

I'm making a fighting game in C++... I got pretty far but when it goes into a "while" loop it dosn't stop for input "cin" Here is my code so far...
#include <iostream>

using namespace std;

int plname;
int health;
int maxhealth;
int enemy;
int boss;
int atk;
int def;
int exp;
int maxexp;
int lvl;
int maplvl;
int lvl5beat;
int win;
int enatk;
int endef;
int enexp;
int train;
int ans;
int atb;
int att;
int fightloop;
int hrs;

void Fight(int a, int b, int c, int d);
void Lvlup();

int main()
{
    exp = 0;
    maxexp = 20;
    maxhealth = 6;
    health = 6;
    while (win == 0){
    cout << "Voice: What is your name? \n" << "You: My name is ";
    cin >> plname;
    cout << "Voice: Welcome " << plname << ", You have 3 hours to train...";
    train = 3;
    while (train > 0){
    cout << "Voice: You have " << train << " to train." << endl;
    cout << "Voice: What would you like to train...?\n" << "1: Attack\n2: Defence\nYou: ";
    cin >> ans;
    if (ans == 1){
        atk = atk+1;
        train = train-1;
    }
    if (ans == 2){
        def = def+1;
        train = train-1;
    }
    }
    while (win == 0){
    system("cls");
    cout << "Voice: Choose your level... \n" << "1\n2\n3\n4\n5\n";
    if (lvl5beat == 1){
        cout << "6\n";
    }
    cout << "You: ";
    cin >> maplvl;
    if (maplvl < 1){
        maplvl = 1;
    }
    if (maplvl < 5){
        if (lvl5beat == 0){
            maplvl = 1;
        }
        if(lvl5beat == 1){
            maplvl = 6;
        }
    }
    if (maplvl == 1){
        Fight(1, 2, 5, 8);
    }
    if (maplvl == 2){
        Fight(5, 4, 25, 25);
    }
    if (maplvl == 3){
        Fight(10, 15, 50, 100);
    }
    }
    }
}

void Fight(int a, int b, int c, int d){
    atb = 1;
    enatk =  a;
    endef = b;
    enemy = c;
    enexp = d;
    fightloop = 0;
    while (fightloop == 0){
    system("cls");
    cout << "1: Attack\n2: Meditate\n3: Run\nChoice: ";
    cin >> ans;
    if (ans == 1){
        att = atk*atb;
        enemy = endef-att;
        if(enemy < 1){
            exp = exp+enexp;
            if(exp < (maxexp-1)){
                Lvlup();
            }
            health = maxhealth;
            fightloop = 1;
        }
    }
    if (ans == 2){
        atb = atk+1;
    }
    if (ans == 3){
        fightloop  = 1;
        health = maxhealth;
    }
    health = health-enatk;
    }
}

void Lvlup(){
    lvl = lvl+1;
    if (lvl == 2){
        hrs = 5;
    }
    if (lvl == 3){
        hrs = 16;
    }
    cout << "Voice: Congratz " << plname << ", You have 3 hours to train...";
    train = hrs;
    while (train > 0){
        system("cls");
    cout << "Voice: You have " << train << " to train." << endl;
    cout << "Voice: What would you like to train...?\n" << "1: Attack\n2: Defence\nYou: ";
    cin >> ans;
    if (ans == 1){
        atk = atk+1;
        train = train-1;
    }
    if (ans == 2){
        def = def+1;
        train = train-1;
    }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ fighting game input help.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,524
  • Joined: 23-August 08

Re: C++ fighting game input help.

Posted 26 August 2010 - 12:43 PM

http://www.parashift...t.html#faq-15.6
Was This Post Helpful? 0
  • +
  • -

#3 Gus.bat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-September 09

Re: C++ fighting game input help.

Posted 26 August 2010 - 12:46 PM

It's line 45, that user input is just skipped over. Am I missing something, because the input on line 39 works fine.
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: C++ fighting game input help.

Posted 26 August 2010 - 01:06 PM

"plname" is an integer. Why would you use an integer variable to store the name of a player? Use std::string instead. By the way, if I enter an integer as my name, everything seems to run OK.
Was This Post Helpful? 0
  • +
  • -

#5 Gus.bat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-September 09

Re: C++ fighting game input help.

Posted 26 August 2010 - 01:11 PM

"plname" is the one that works fine, its the next input for "ans" that dosn't work. Can you try compiling it and seeing if it works for you? I'm using Code::Blocks....
Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: C++ fighting game input help.

Posted 26 August 2010 - 01:15 PM

It does work for me, on Code::Blocks too. Though, what do you mean by not working? Does it jump over the execution of that "cin >> ans"?
Was This Post Helpful? 0
  • +
  • -

#7 Gus.bat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-September 09

Re: C++ fighting game input help.

Posted 26 August 2010 - 01:40 PM

Yes, exactly I tried even just taking that out and using the Lvlup() function but it still dosn't work...

Here's the new code. Could it be because its in a while loop? And it skips over it causing it to just keep clearing the screen...
 #include <iostream>

using namespace std;

int plname;
int health;
int maxhealth;
int enemy;
int boss;
int atk;
int def;
int exp;
int maxexp;
int lvl;
int maplvl;
int lvl5beat;
int win;
int enatk;
int endef;
int enexp;
int train;
int ans;
int atb;
int att;
int fightloop;
int hrs;

void Fight(int a, int b, int c, int d);
void Lvlup();

int main()
{
    exp = 0;
    maxexp = 20;
    maxhealth = 6;
    health = 6;
    while (win == 0){
    cout << "Voice: What is your name? \n" << "You: My name is ";
    cin >> plname;
    Lvlup();
    while (win == 0){
    system("cls");
    cout << "Voice: Choose your level... \n" << "1\n2\n3\n4\n5\n";
    if (lvl5beat == 1){
        cout << "6\n";
    }
    cout << "You: ";
    cin >> maplvl;
    if (maplvl < 1){
        maplvl = 1;
    }
    if (maplvl < 5){
        if (lvl5beat == 0){
            maplvl = 1;
        }
        if(lvl5beat == 1){
            maplvl = 6;
        }
    }
    if (maplvl == 1){
        Fight(1, 2, 5, 8);
    }
    if (maplvl == 2){
        Fight(5, 4, 25, 25);
    }
    if (maplvl == 3){
        Fight(10, 15, 50, 100);
    }
    }
    }
}

void Fight(int a, int b, int c, int d){
    atb = 1;
    enatk =  a;
    endef = b;
    enemy = c;
    enexp = d;
    fightloop = 0;
    while (fightloop == 0){
    system("cls");
    if (lvl == 1){
        if (health > 4){ //5,6
            cout << "You feel strong\n\n";
        }
        if (health < 5|health > 2){ //3,4
            cout << "You feel weak\n\n";
        }
        if (health < 3){ //1,2
            cout << "You feel feeble\n\n";
        }
    }
    cout << "1: Attack\n2: Meditate\n3: Run\nChoice: ";
    cin >> ans;
    if (ans == 1){
        att = atk*atb;
        enemy = endef-att;
        if(enemy < 1){
            exp = exp+enexp;
            if(exp < (maxexp-1)){
                Lvlup();
            }
            health = maxhealth;
            fightloop = 1;
        }
    }
    if (ans == 2){
        atb = atk+1;
    }
    if (ans == 3){
        fightloop  = 1;
        health = maxhealth;
    }
    health = health-enatk;
    }
}

void Lvlup(){
    lvl = lvl+1;
    if (lvl == 1){
        hrs = 3;
    }
    if (lvl == 2){
        hrs = 5;
    }
    if (lvl == 3){
        hrs = 16;
    }
    train = hrs;
    while (train > 0){
        system("cls");
    cout << "Voice: You have " << train << " hours to train." << endl;
    cout << "Voice: What would you like to train...?\n" << "1: Attack\n2: Defence\nYou: ";
    cin >> ans;
    system("pause");
    if (ans == 1){
        atk = atk+1;
        train = train-1;
    }
    if (ans == 2){
        def = def+1;
        train = train-1;
    }
    }
}



*Line 108... atb = atb+1
Was This Post Helpful? 0
  • +
  • -

#8 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: C++ fighting game input help.

Posted 26 August 2010 - 05:40 PM

cin is in an error state if you type a string where it expects an integer.

I would suggest:
std::string plName;
std::getline( std::cin, plName );



so just include the header file <string> and change player name from an integer to a string. If you don't know that storing a string into an integer is bad you might want to review some C++ datatype and C++ array stuff.

also you might google "C string"

This post has been edited by taylorc8: 26 August 2010 - 05:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1