Ok, so I wrote this generic/simple program for a class but am having some trouble with the pauses in the program.

I want to use cin.get(); or cin.ignore(); to pause my program so I can avoid system("PAUSE")(word on the street is:shit sucks) and make it look cleaner. The thing is, the first time the program loops (after the HP is initialized), the program will skip the cin.get() (replaces the system("PAUSE") on line 55), and clear the screen automatically, making it look like there is a mathematical error when the new "health" is displayed.

My question is, is there any way to remedy this problem without using a system("PAUSE")?

using namespace std;

    int monhp, herohp;
    int herohit, monhit;
    string hero, monster;
    const int MAX_HP = 150, MIN_HP = 125, MAX_HIT = 30;
    bool game = true;
    char yn = 'y';
        cout<<"Hero Name: ";
        cout<<"Monster Name: ";
        herohp = rand()%(MAX_HP-MIN_HP) + MIN_HP;
        monhp = rand()%(MAX_HP-MIN_HP) + MIN_HP;
        cout<<endl<<endl<<hero<<" has "<<herohp<<" HP"<<endl;
        cout<<monster<<" has "<<monhp<<" HP\n\n";
        bool heroAlive = true;
        bool monAlive = true;
        while(heroAlive && monAlive)
            herohit = rand()%MAX_HIT;
            monhit = rand()%MAX_HIT;
            if(monhit >= MAX_HIT * 0.8)
                cout<<"Critical hit! ";
            else if(monhit <= MAX_HIT * 0.2)
                cout<<"Weak! ";
            else if(monhit == 0)
            cout<<hero<<" gets hit for "<<monhit<<" points!!!\n";
            if(herohit >= MAX_HIT * 0.8)
                cout<<"Critical hit! ";
            else if(herohit <= MAX_HIT * 0.2)
                cout<<"Weak! ";
            else if(herohit == 0)
            cout<<monster<<" takes "<<herohit<<" damage from "<<hero<<"!!!\n\n";

            herohp -= monhit;
            monhp -= herohit;

            cout<<hero<<" now has "<<herohp<<" hitpoints."<<endl;
            cout<<monster<<" now has "<<monhp<<" hitpoints."<<endl;

            if(herohp<1 || monhp<1)
                    cout<<monster<<" has killed "<<hero<<"! "<<monster<<" wins!!!";
                    heroAlive = false;
                else if(monhp<herohp)
                    cout<<hero<<" has killed "<<monster<<"! "<<hero<<" wins!!!";
                    monAlive = false;
                else cout<<"EVERYONE DIES.";
            cout<<endl<<"Again?(y/n): ";
    return 0;

P.S. I dont want to use a cin.ignore() and cin.get() after one another as the double enter makes things sluggish and clunky feeling.

P.S.S Im OCD about these things.


Edited 6 Years Ago by WhyteRyce: n/a

yes you need to clear the stream. Use something like this :

#include <limits>
#include <string>
void waitForInput(){
 string dummy;

P.S. I dont want to use a cin.ignore() and cin.get() after one another as the double enter makes things sluggish and clunky feeling.

Yes you do. cin.ignore is not an input, it clears the buffer. Then to make it wait, you use the cin.get .

But, as firstPerson mentioned, getline would be better. cin.get only gets the 1st character, and if the user types anything more than ENTER, you've still got a problem. getline solves that problem.

Ok thanks for the help! I guess I could have just read that stick couldn't I... sorry about that.

This article has been dead for over six months. Start a new discussion instead.