Click here to Skip to main content
15,562,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an assignment for a coding class where I have to create a code for the rock, paper scissors game, with different functions, calling rock 1 and paper 2 and scissors 3, and create a menu, then have the person chose, save that, then have the computer generate a random number and then print what they played, what the computer played and who won. However, the last part isn't connecting, and I don't understand where I went wrong, so I'm really really lost and I would really appreciate any help. Thank you in advance.

What I have tried:

For reference here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float response, choice;
int playerInput()
{
printf(" For rock press 1\n ");
printf("For paper press 2\n ");
printf("For scissors press 3\n ");
printf("What would you like to play? ");
scanf ("%f", &response);
return response;
}
int computerChoice()
{
int lower = 1, upper = 3, count = 1;
srand(time(0));
printf("The random number that: ");
for (int i = 0; i < count; i++) {
int choice = (rand() % (upper - lower + 1)) + lower;
printf("the computer chose is %d ", choice);
}
return choice;
}
void displayWinner()
{
if (response == choice)
{
printf("You tied, lets play again");
}
if (choice == 1 && response == 2){
printf("you won");
}
if (choice == 1 && response == 3){
printf("computer won");
}
if (choice == 2 && response == 1){
printf("computer won");
}
if (choice == 2 && response == 3){
printf("you won");
}
if (choice == 3 && response == 1){
printf("you won");
}
if (choice == 3 && response == 2){
printf("computer won");
}
}
int main(void)
{int pinput, cinput, dinput;
pinput = playerInput();
cinput = computerChoice();
displayWinner();
return 0;}
Posted
Updated 8-Oct-21 20:10pm
Comments
jeron1 8-Oct-21 21:36pm    
"However, the last part isn't connecting"

Could you elaborate on what this means?
paula0522805 8-Oct-21 22:33pm    
sorry for getting back to you so late, I mean that it'll show what the user inputted and what the computer chose, but not the winner, because something is going wrong in the computer choice part.

It's pretty simple really. Let me condense it a little for you:
C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float response, choice;
int playerInput()
    {
    return 1;
    }
int computerChoice()
    {
    return 2;
    }
void displayWinner()
    {
    printf("%f:%f\n", response,choice);
    }
int main(void)
    {
    int pinput, cinput;
    pinput = playerInput();
    cinput = computerChoice();
    displayWinner();
    return 0;
    }

When you run that, you will get:
0.000000:0.000000
Can you see why?
Where in that code do you set the values of response and choice?

You don't. You save the values that the two functions return in local variables but you use global variables to decide the result in displayResult

What you should be doing is storing them in local variables, and then passing those values to the displayResult function as parameters:
C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int playerInput()
    {
    return 1;
    }
int computerChoice()
    {
    return 2;
    }
void displayWinner( int player, int computer)
    {
    printf("%u:%u\n", player, computer);
    }
int main(void)
    {
    int pinput, cinput;
    pinput = playerInput();
    cinput = computerChoice();
    displayWinner(pinput, cinput);
    return 0;
    }
Now you get the results you expect:
1:2

You need to do something very similar in your code.

Oh, and do yourself a favour: indent your code. It's a lot easier to see where functions, loops, conditions, and so on start and end when you indent it!
 
Share this answer
 
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
For reference here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float response, choice;
int playerInput()
{
	printf(" For rock press 1\n ");
	printf("For paper press 2\n ");
	printf("For scissors press 3\n ");
	printf("What would you like to play? ");
	scanf ("%f", &response);
	return response;
}
int computerChoice()
{
	int lower = 1, upper = 3, count = 1;
	srand(time(0));
	printf("The random number that: ");
	for (int i = 0; i < count; i++) {
		int choice = (rand() % (upper - lower + 1)) + lower;
		printf("the computer chose is %d ", choice);
	}
	return choice;
}
void displayWinner()
{
	if (response == choice)
	{
		printf("You tied, lets play again");
	}
	if (choice == 1 && response == 2){
		printf("you won");
	}
	if (choice == 1 && response == 3){
		printf("computer won");
	}
	if (choice == 2 && response == 1){
		printf("computer won");
	}
	if (choice == 2 && response == 3){
		printf("you won");
	}
	if (choice == 3 && response == 1){
		printf("you won");
	}
	if (choice == 3 && response == 2){
		printf("computer won");
	}
}
int main(void)
{int pinput, cinput, dinput;
	pinput = playerInput();
	cinput = computerChoice();
	displayWinner();
return 0;}

Indentation style - Wikipedia[^]
Best C++ Formatter and Beautifier[^]
Online C/C++ Formatter, Indenter and Beautifier – Techie Delight[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900