Thread: keeping score trough a loop

  1. #1
    new to c++
    Join Date
    Feb 2009
    Posts
    53

    keeping score trough a loop

    hello
    im writing a rock paper scissors game and i want to keep the users scoreand computers score after the user says they want to play again. how would i do this?
    Code:
    #include<iostream>
    #include <cstdlib>  //to use rand function
    #include <ctime>  // to use time as the seed for rand
    using namespace std;
    
    
    /*Randall Foor
    Rock Paper Scissors program
    10/7/10
    */
    
    int main(){
    	int number,usernumber;
    	char repeat,userpick;
    
    	
    
    	do{
    		int userscore,compscore;
    		system("cls");
    		srand(time(0));
    		number = 1 + (rand() % 3) ;
    		
    		userscore=0;
    		compscore=0;
    		cout<<"\nYour Score: "<<userscore<<endl;
    		cout<<"Computers Score: "<<compscore<<endl;
    		cout<<"Pick either (R)ock,(P)aper, or (S)cissors. \n";
    		cin>>userpick;
    
    		if(userpick=='r'|| userpick=='R'){
    			usernumber=1;
    			cout<<"so you picked rock";}
    		if(userpick=='p'|| userpick=='P'){
    			usernumber=2;
    			cout<<"so you picked paper";}
    		if(userpick=='s'|| userpick=='S'){
    			usernumber=3;
    			cout<<"so you picked scissors";}
    	
    
    		if(usernumber==number){
    			cout<<"\nuser picked "<<userpick<<" and the computer picked the same thing, so it is a draw";
    			userscore++;
    			compscore++;}
    		if(usernumber==1 && number==2){
    			cout<<"\npaper covers rock, you lose";
    			compscore++;}
    		if(usernumber==1 && number==3){
    			cout<<"\nrock beats scissors, you win";
    			userscore++;}
    		if(usernumber==2 && number==1){
    			cout<<"\npaper covers rock, you win";
    			userscore++;}
    		if(usernumber==2 && number==3){
    			cout<<"\nscissors cuts paper, you lose";
    			compscore++;}
    		if(usernumber==3 && number==1){
    			cout<<"\nrock beats scissors, you lose";
    			compscore++;}
    		if(usernumber==3 && number==2){
    			cout<<"\nscissors cut paper, you win";
    			userscore++;}
    
    		cout<<"\nYour Score: "<<userscore<<endl;
    		cout<<"Computers Score: "<<compscore<<endl;
    
    	
    
    
    
    
    		cout<<"\ndo you want to repeat<y or n>?\n";
    		cin>>repeat;
    	}while(repeat=='y'|| repeat=='Y');
    
    system("pause");
    	return 0;
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    declare and initialize the variables to hold the scores outside of the loop
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    thanks i got it heres the code if you want to play
    Code:
    #include<iostream>
    #include <cstdlib>  //to use rand function
    #include <ctime>  // to use time as the seed for rand
    using namespace std;
    
    
    /*Randall Foor
    Rock Paper Scissors program
    10/7/10
    */
    
    int main(){
    	int number,usernumber;
    	char repeat,userpick;
    	int userscore,compscore,value;
    	value=0;
    	userscore=0;
    	compscore=0;
    	
    	
    
    	do{
    		
    		
    		srand(time(0));
    		number = 1 + (rand() % 3) ;
    		
    		userscore=userscore+value;
    		compscore=compscore+value;
    		cout<<"\nYour Score: "<<userscore<<endl;
    		cout<<"Computers Score: "<<compscore<<endl;
    		
    		cout<<"Pick either (R)ock,(P)aper, or (S)cissors. \n";
    		cin>>userpick;
    
    		if(userpick=='r'|| userpick=='R'){
    			usernumber=1;
    			cout<<"so you picked rock";}
    		if(userpick=='p'|| userpick=='P'){
    			usernumber=2;
    			cout<<"so you picked paper";}
    		if(userpick=='s'|| userpick=='S'){
    			usernumber=3;
    			cout<<"so you picked scissors";}
    	
    
    		if(usernumber==number){
    			cout<<"\nuser picked "<<userpick<<" and the computer picked the same thing, so it is a draw";
    			userscore++;
    			compscore++;}
    		if(usernumber==1 && number==2){
    			cout<<"\npaper covers rock, you lose";
    			compscore++;}
    		if(usernumber==1 && number==3){
    			cout<<"\nrock beats scissors, you win";
    			userscore++;}
    		if(usernumber==2 && number==1){
    			cout<<"\npaper covers rock, you win";
    			userscore++;}
    		if(usernumber==2 && number==3){
    			cout<<"\nscissors cuts paper, you lose";
    			compscore++;}
    		if(usernumber==3 && number==1){
    			cout<<"\nrock beats scissors, you lose";
    			compscore++;}
    		if(usernumber==3 && number==2){
    			cout<<"\nscissors cut paper, you win";
    			userscore++;}
    
    		cout<<"\nYour Score: "<<userscore<<endl;
    		cout<<"Computers Score: "<<compscore<<endl;
    
    	
    
    
    
    
    		cout<<"\ndo you want to repeat<y or n>?\n";
    		cin>>repeat;
    
    
    		system("cls");
    	}while(repeat=='y'|| repeat=='Y');
    
    	if(repeat=='n'|| repeat=='N'){
    		if(userscore>compscore)
    			cout<<"you win"<<endl;
    		if(userscore<compscore)
    			cout<<"you lose"<<endl;
    	}
    	
    
    system("pause");
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Frustated with mess table printing
    By benedicttobias in forum C Programming
    Replies: 6
    Last Post: 04-16-2009, 05:50 PM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM