this is a very simple RPS game and i ran into a prob. heres the code

#include <iostream>  // for cout/cin
#include <cstdlib>   // for rand/srand
#include <ctime>    // for time
#include <cctype>   // for case conversion
using namespace std;
char rps;
char ai;
char redo = 'Y';    // all the variables
unsigned int num;
int win = 0;
int lose = 0;

void Convert(){  // func to check if input is lower and to conv to upper
cin>>rps;
if(islower(rps)) rps=toupper(rps);
}
void Random(){  // for random ai num
srand(time(0));
num = rand()%2;
}
void AiConvert(){   // conv random ai num to letter
switch(num){
case 0:
ai = 'R';
break;
case 1:
ai = 'P';
break;
case 2:
ai = 'S';
break;
default: ai = 'P';
}

}
void Compare();   // compare func

void Display(){  // func to display winnings and losses
cout<<"Total winnings: "<<win<<endl;
cout<<"Total loses: "<<lose<<endl;
}

int main(){   // main func
    cout<<"Welcome to my RPS!\nCall which option you want by initial!\n";
do{  // main loop
Convert();
Random();
AiConvert();
cout<<"AI picked: "<<ai<<endl;
Compare();
Display();
cout<<"Wanna try again? (Y/N)"<<endl;
cin>>redo;                             // see if still wanna play
if(islower(redo)) redo=toupper(redo);

}while(redo!='N');

}

void Compare(){     // compare func coding
if(rps=='R'||ai=='P'){
    cout<<"You lose!"<<endl;
    lose++;
}
if(rps=='R'||ai=='S'){
cout<<"You win!"<<endl;
win++;
}
if(rps=='P'||ai=='S'){
cout<<"You lose!"<<endl;
lose++;
}
if(rps=='P'||ai=='R'){
cout<<"You win!"<<endl;
win++;
}
if(rps=='S'||ai=='R'){
cout<<"You lose!"<<endl;
lose++;
}
if(rps=='S'||ai=='P'){
cout<<"You lose!"<<endl;
win++;
}
if (rps==ai){
cout<<"Tieee!"<<endl;
}

}

when i run it the ai runs like 3 time but only asks for my input once.
and im just lost. all help appreciated

Perhaps there remain something in your input buffer. You need to flush your input properly. There is an excellent article on flushing in Daniweb's C++ forum. Read that.

Last time you told me sloppy coding isnt serious lol.

and i dont understand when i would flush my input?

when i run it the ai runs like 3 time but only asks for my input once.

It means that somehow the program is receiving input for 3 times, using only onetime user input.
That means, there are some characters that remian in buffer after you have entered the first input. That's why I said to flush the input stream, that is stdin stream.

Edited 4 Years Ago by np complete

Last time you told me sloppy coding isnt serious lol.

That's not at all what I said. I said "Don't apologize for sloppy coding. It only takes a minute or two to fix formatting." That means make it unsloppy so you don't have to apologize. And so we can read and follow it.

and i dont understand when i would flush my input?

Any time you do an input and don't read everything from the stream. Such as typing in "N[enter]" and reading a single character ('N') and leaving the '[enter]' in the stream for the next input.

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