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")?

#include<iostream>
#include<ctime>
#include<stdio.h>
using namespace std;

main()
{
    int monhp, herohp;
    int herohit, monhit;
    string hero, monster;
    const int MAX_HP = 150, MIN_HP = 125, MAX_HIT = 30;
    srand(time(unsigned(NULL)));
    bool game = true;
    char yn = 'y';
    while(yn=='y')
    {
        cout<<"Hero Name: ";
        cin>>hero;
        cout<<"Monster Name: ";
        cin>>monster;
    
        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";
        system("PAUSE");
        system("CLS");
        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<<"Miss!";
            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<<"Miss!";
            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;
            system("PAUSE");
            system("CLS");

            if(herohp<1 || monhp<1)
            {
                
                if(herohp<monhp)
                {
                    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.";
            }  
        }
        do
        {
            cout<<endl<<"Again?(y/n): ";
            cin>>yn;
        }
        while(yn!='y'&&yn!='n');
        system("CLS");  
    }
    cin.get();            
    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.

Thanks

Recommended Answers

All 3 Replies

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

#include <limits>
#include <string>
//...
void waitForInput(){
 std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
 string dummy;
 getline(dummy,'\n');
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.