Thread: Whats wrong with my program?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    Cincinnati, Oh
    Posts
    25

    Whats wrong with my program?

    Code:
    //-----------------------------------------------------------------------
    // File-----------rock paper scissors 
    // This program plays a game of Rock-Paper-Scissors and displays some
    // statistics concerning the results. 
    //-----------------------------------------------------------------------
    
    #include <iostream>
    #include <cstdlib>
    
    char getInput();
    int decideWinner(char, char, int, int);
    void stats(int&, int&, int&, int&, char&, char&);
    
    //-----------------------------------------------------------------------
    // main
    //-----------------------------------------------------------------------
    
    int main()
    {
    	char continued=='y';
    	char input1, input2;
    	int winner;
    	int totalGames, userWins, computerWins, numR, numS, numP=0;
    
    	while (continued == 'y' || continued == 'Y');
    	{
    		do
    
    		//Get person 1's input
    		input1 = getInput( );
    
    		//Get computer's input
    		input2 = randomInput( );
    
    		//Decide who won
    		winner = decideWinner(input2, input1, computerWins, userWins);
    		
    		//Keep statistics
    		stats(totalGames, numR, numS, numP, 'P', 'R');
    
    		//Decide if want to continue
    		cout << "Play again (Y or N)? ";
    		cin >> continued; 
    		cout << endl;
    
    	}
    
    		do{
    
    	cout << totalGames << " games were played and the user won " << userWins << " games." << endl;
    	cout << "The computer won " << computerWins << " games." << endl << endl;
    	cout << "The % of R's is " << numR/(2.0*totalGames) * 100.0 << endl;
    	cout << "The % of P's is " << numP/(2.0*totalGames) * 100.0 << endl;
    	cout << "The % of S's is " << numS/(2.0*totalGames) * 100.0 << endl;
    
    	return 0;
    }
    
    //-----------------------------------------------------------------------
    // getInput
    //-----------------------------------------------------------------------
    
    	char getInput(char choice)
    	{
    		cout << "Enter R, P, or S: " ;
    		cin >> choice;
    	while (choice != 'R' && choice != 'P' && choice != 'S')
    	{
    		cout << "Enter R, P, or S: " ;
    		cin >> choice;
    	}
    	}
    
    //-----------------------------------------------------------------------
    // randomInput 
    //-----------------------------------------------------------------------
    
    char randomInput()
    {
    	int choiceInt;
    	char choiceChar;
    
    	choiceInt = rand()%3;
    	if (choiceInt == 0)
    		choiceChar= 'P';
    	else if (choiceInt == 1)
    		choiceChar = 'R';
    	else
    		choiceChar ='S';
    	cout << "Computer chooses: " << choiceChar << endl;
    	
    	return choiceChar;
    }
    
    //-----------------------------------------------------------------------
    // decideWinner
    //-----------------------------------------------------------------------
    
    int decideWinner(char choice1, char choice2, int &userTotal,int &computerTotal, int winner)
    {
    	if (choice1 == 'P')
    	{
    		else (choice2 == 'S')
    			winner = 2;
    			computerTotal++;
    		else if (choice2 == 'R')
    			 winner = 1;
    			 userTotal++;
    		else if (choice2 == 'P')
    			winner = 0;
    	}
    	else if (choice1 == 'S')
    	{
    		if (choice2 == 'S')
    			winner = 0;
    		else if (choice2 == 'R')
    			 winner = 2;
    			 computerTotal++;
    		else if (choice2 == 'P')
    		
    			winner = 1;
    			userTotal++;
    	}
    	else if (choice1 == 'R')
    	{
    		if (choice2 == 'S')
    			winner = 1;
    			userTotal++;
    		else if (choice2 == 'R')
    			 winner = 0;
    		else if (choice2 == 'P')
    			winner = 2;
    			computerTotal++;
    		}
    
    	if (winner == 1)
    		cout << endl << "You win!" << endl << endl;
    	else if (winner == 2)
    		cout << endl << "Computer wins!" << endl << endl;
    	else
    		cout << endl << "No one wins! " << endl << endl;
    
    	return winner;
    }
    
    //-----------------------------------------------------------------------
    // getInput 
    //-----------------------------------------------------------------------
    
    void stats(int &totalGames, int &numR, int &numS, int &numP, char input1, char input2)
    {
    	totalGames++;
    	
    	if (input1 == 'R')
    		numR++;
    	if (input2 == 'R')
    		numR++;
    	if (input1 == 'S')
    		numS++;
    	if (input2 == 'S')
    		numS++;
    	if (input1 == 'P')
    		numP++;
    	if (input2 == 'P')
    		numP++;
    	return;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You tell us. What's wrong with it? Does it fail to compile? Does it compile but not link? Does it compile and link but not run? Does it run but fail to get input? Is your do-while loop coded incorrectly? Does it get input but fail to give output? Does it give bad output?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    Cincinnati, Oh
    Posts
    25
    it doesnt compile it says that theres a few of my if statements wrong?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Which ones? The more information you give the easier it is to help. If you get compiler error messages, post the exact messages and indicate the lines they refer to (if you get a lot of errors, post only the first few).

    For example, I'm guessing at least one of the errors refers to the decideWinner function? Look at your first couple of if statements. Do you see anything that might be wrong?

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You don't really expect us to edit your entire program line by line, do you?

    The compiler error messages (and there are many) tell you which lines to look at and correct. Often they're not exactly clear, but you should be able to fix at least some of them. When you get stuck, post the exact, complete message that stumps you & someone will help.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Location
    Cincinnati, Oh
    Posts
    25
    I fixed the if else statements. I'm getting an error that says it cannot convert parameter 5 from char to char &? It's under the keep statistics comment. Also in the get input function it says char is an unexpected data type.
    d
    Code:
    #include <iostream>
    #include <cstdlib>
    
    char getInput();
    char randomInput(char);
    int decideWinner(char, char, int, int);
    void stats(int&, int&, int&, int&, char&, char&);
    
    //-----------------------------------------------------------------------
    // main
    //-----------------------------------------------------------------------
    
    int main()
    {
    	char continued =='y' && 'Y';
    	char input1, input2;
    	int winner;
    	int totalGames, userWins, computerWins, numR, numS, numP=0;
    
    	while (continued == 'y' || continued == 'Y');
    	{
    		do
    
    		//Get person 1's input
    		input1 = getInput( );
    
    		//Get computer's input
    		input2 = randomInput( );
    
    		//Decide who won
    		winner = decideWinner(input2, input1, computerWins, userWins);
    		
    		//Keep statistics
    		stats(totalGames, numR, numS, numP, 'P', 'R');
    
    		
    	}
    
    		do{
    		
    		cout << totalGames << " games were played and the user won " << userWins << " games." << endl;
    		cout << "The computer won " << computerWins << " games." << endl << endl;
    		cout << "The % of R's is " << numR/(2.0*totalGames) * 100.0 << endl;
    		cout << "The % of P's is " << numP/(2.0*totalGames) * 100.0 << endl;
    		cout << "The % of S's is " << numS/(2.0*totalGames) * 100.0 << endl;
    
    		//Decide if want to continue
    		cout << "Play again (Y or N)? ";
    		cin >> continued; 
    		cout << endl;
    
    		return 0;
    }
    
    //-----------------------------------------------------------------------
    // getInput
    //-----------------------------------------------------------------------
    
    	char getInput(char choice)
    	{
    		cout << "Enter R, P, or S: " ;
    		cin >> choice;
    	while (choice != 'R' && choice != 'P' && choice != 'S')
    	{
    		cout << "Enter R, P, or S: " ;
    		cin >> choice;
    	}
    	}
    
    //-----------------------------------------------------------------------
    // randomInput 
    //-----------------------------------------------------------------------
    
    char randomInput()
    {
    	int choiceInt;
    	char choiceChar;
    
    	choiceInt = rand()%3;
    	if (choiceInt == 0)
    		choiceChar= 'P';
    	else if (choiceInt == 1)
    		choiceChar = 'R';
    	else
    		choiceChar ='S';
    	cout << "Computer chooses: " << choiceChar << endl;
    	
    	return choiceChar;
    }
    
    //-----------------------------------------------------------------------
    // decideWinner
    //-----------------------------------------------------------------------
    
    int decideWinner(char choice1, char choice2, int &userTotal,int &computerTotal, int winner)
    {
    	if (choice1 == 'P' && choice2 == 'S')
    			winner = 2;
    			computerTotal++;
    		
    	else if (choice2 == 'R')
    			 winner = 1;
    			 userTotal++;
    		
    	else if (choice2 == 'P')
    			winner = 0;
    	}
    	else if (choice1 == 'S' && choice2 == 'S')
    			winner = 0;
    		
    	else if (choice2 == 'R')
    			 winner = 2;
    			 computerTotal++;
    		
    	else if (choice2 == 'P')
    		
    			winner = 1;
    			userTotal++;
    	}
    	else if (choice1 == 'R' && choice2 == 'S')
    			winner = 1;
    			userTotal++;
    		
    	else if (choice2 == 'R')
    			 winner = 0;
    		
    	else if (choice2 == 'P')
    			winner = 2;
    			computerTotal++;
    		}
    
    	if (winner == 1)
    		cout << endl << "You win!" << endl << endl;
    	else if (winner == 2)
    		cout << endl << "Computer wins!" << endl << endl;
    	else
    		cout << endl << "No one wins! " << endl << endl;
    
    	return winner;
    }
    
    //-----------------------------------------------------------------------
    // getInput 
    //-----------------------------------------------------------------------
    
    void stats(int &totalGames, int &numR, int &numS, int &numP, char input1, char input2)
    {
    	totalGames++;
    	
    	if (input1 == 'R')
    		numR++;
    	if (input2 == 'R')
    		numR++;
    	if (input1 == 'S')
    		numS++;
    	if (input2 == 'S')
    		numS++;
    	if (input1 == 'P')
    		numP++;
    	if (input2 == 'P')
    		numP++;
    	return;
    }

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Your stats function parameters are references, so you have to supply variables as arguments when you call the function. You are trying to give it two char literals, 'P' and 'R', in the function call. Those are constants, not variables.

    Look at your prototype for getInput. Does that look like the signature of getInput in your function definition?

    You also have a few problems in the first line of your main function.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Look at your prototype for stats and compare it to the actual definition of the function. Do you see any differences in the types you used? That is causing one of your problems.

    In fact, you should do that for each of your functions. The types in the prototypes should always match with those in the definition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM