Michael3455
Active Member
- Joined
- Sep 11, 2005
- Sythe Gold
- 0
- Messages
- 134
Here's my completed Paper, Scissors, Rock program. Written in c++ (Problem fixed, if a mod see's please delete last portion of the topic title). A big thanks has to go to Lnostdal from the ubuntuforums.org website as he helped fix my poorly implemented input checking code.
So, here it is!
So, here it is!
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define M_inline
template<typename T>
M_inline void readStdin(T& t, std::string msg, std::string on_wrong_input){
std::cout << msg << std::flush;
while(!(std::cin >> t)) {
std::cout << on_wrong_input << std::flush;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << msg << std::flush;
}
}
int main()
{
srand((unsigned)time(0));
int compchoice = ((rand()%3)+1);
int rock = 1;
int paper = 2;
int scissors = 3;
int i = 0;
do{
readStdin(i, "Enter 1 for Rock, 2 for Paper, 3 for Scissors: ",
"That wasn't a correct input. Try again!\n");
} while((i < 1 || i > 3) && (cout << "Number not within range" << endl));
switch (i){
case 1:
if (compchoice == rock){
cout<<"Rock doesn't beat rock, DRAW!\n";
}
else if (compchoice == paper){
cout<<"Paper beats rocks, you loose!\n";
}
else{
cout<<"Rock beats scissors, you win!\n";
}
break;
case 2:
if (compchoice == paper){
cout<<"Paper doesn't beat Paper, DRAW!\n ";
}
else if (compchoice == rock){
cout<<"Paper beats rocks, you WIN!\n";
}
else{
cout<<"Scissors beats paper, you LOOSE!\n";
}
break;
case 3:
if (compchoice == scissors){
cout<<"Scissors doesn't beat scissors, DRAW!\n";
}
else if (compchoice == paper){
cout<<"Scissors beats paper, you WIN!\n";
}
else{
cout<<"Rock beats scissors, you LOOSE!\n";
break;
}
}
return 0;
cin.get();
}

