Paper, Scissors, Rock -- Small problem, but still works

Discussion in 'Programming General' started by Michael3455, Mar 10, 2007.

Paper, Scissors, Rock -- Small problem, but still works
  1. Unread #1 - Mar 10, 2007 at 2:58 AM
  2. Michael3455
    Joined:
    Sep 11, 2005
    Posts:
    134
    Referrals:
    0
    Sythe Gold:
    0

    Michael3455 Active Member

    Paper, Scissors, Rock -- Small problem, but still works

    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!
    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();
    }
    
    
    
    
     
  3. Unread #2 - Mar 14, 2007 at 3:18 AM
  4. SidStudios
    Joined:
    Mar 14, 2007
    Posts:
    201
    Referrals:
    0
    Sythe Gold:
    0

    SidStudios Active Member

    Paper, Scissors, Rock -- Small problem, but still works

    Thanks! From your code I learnt how to use cin.get() in C++
    Greatly appreciated!
     
< Set focus on keypress (newb) | C# select process... need help... >

Users viewing this thread
1 guest


 
 
Adblock breaks this site