473,396 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Rock Program

gc
Hi

I'm working on a rock, scissors, paper program. I think I have most of
it, but I am having trouble with my main function, here is my code:
#include <stdio.h>
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */
/*User Inputs R,P,S,Q*/
int getValidInteger(int min, int max, char prompt[])
{
char userInput;
printf("\nEnter R(ock), P(aper),or S(cissors) or Q(uit): ");
scanf("%c",&userInput);
//userInput=toupper(userInput);
switch(userInput){
case 'Q':return 0;
case 'R':return 1;
case 'P':return 2;
case 'S':return 3;
default: printf("Invalid Choice!%s\n");
}
}
/*Computer Cal it's choice*/
int getCompChoice()
{
int comp = rand()%3;
switch (comp){
case 0: return 'R';
case 1: return 'P';
case 2: return 'S';
}
return comp;
}

void result(int user, int comp)
{

if (user=comp)
printf("Draw\n");
switch (user) {
case 1:
if (comp == 'R')
printf("You Win\n");
case 2:
if (comp == 'P')
printf("You Win\n");
case 3:
if (comp == 'S')
printf("You Win\n");
default:
printf("You Loose\n");
exit(1);
}

return;
}
main(void)
{
int score = 0;
srand(time(NULL));/* seed the random number generator*/
int getValidInteger();
if (getValidInteger=getCompChoice) score++;
return score;
}
I think I am going about it the wrong way. Could someone steer my in
the right direction, without giving me the answer.
GC

May 5 '06 #1
12 2198
gc wrote:
Hi

I'm working on a rock, scissors, paper program. I think I have most
of it, but I am having trouble with my main function, here is my code:
#include <stdio.h>
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */
/*User Inputs R,P,S,Q*/
int getValidInteger(int min, int max, char prompt[])
{
char userInput;
printf("\nEnter R(ock), P(aper),or S(cissors) or Q(uit): ");
scanf("%c",&userInput);
//userInput=toupper(userInput);
switch(userInput){
case 'Q':return 0;
case 'R':return 1;
case 'P':return 2;
case 'S':return 3;
default: printf("Invalid Choice!%s\n");
}
}
/*Computer Cal it's choice*/
int getCompChoice()
{
int comp = rand()%3;
switch (comp){
case 0: return 'R';
case 1: return 'P';
case 2: return 'S';
}
return comp;
}

void result(int user, int comp)
{

if (user=comp)
printf("Draw\n");
switch (user) {
case 1:
if (comp == 'R')
printf("You Win\n");
case 2:
if (comp == 'P')
printf("You Win\n");
case 3:
if (comp == 'S')
printf("You Win\n");
default:
printf("You Loose\n");
exit(1);
}

return;
}
main(void)
{
int score = 0;
srand(time(NULL));/* seed the random number generator*/
int getValidInteger();
if (getValidInteger=getCompChoice) score++;
return score;
}
I think I am going about it the wrong way. Could someone steer my in
the right direction, without giving me the answer.
There are a few problems, but as you mention 'main', perhaps that's a good
place to start.

This is not required - as the compiler has already seen the definition of
this function. int getValidInteger();
getValidInteger and getCompChoice - when used like this resolve to the
addresses of those functions - i.e., constant values. You're then trying to
assign one constant to another - did you mean == instead of = ? To invoke a
function ... well, you know how to do that. if (getValidInteger=getCompChoice)


You might want to check for other problems with == vs. = in the code, and
also that you're calling printf /honestly/ throughout.
--
==============
Not a pedant
==============
May 5 '06 #2
On 4 May 2006 23:05:02 -0700, "gc" <gr*********@bigpond.com> wrote:
Hi

I'm working on a rock, scissors, paper program. I think I have most of
it, but I am having trouble with my main function, here is my code:
#include <stdio.h>
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */
/*User Inputs R,P,S,Q*/
int getValidInteger(int min, int max, char prompt[])
What are these parameters for?
{
char userInput;
printf("\nEnter R(ock), P(aper),or S(cissors) or Q(uit): ");
scanf("%c",&userInput);
//userInput=toupper(userInput);
switch(userInput){
case 'Q':return 0;
case 'R':return 1;
case 'P':return 2;
case 'S':return 3;
default: printf("Invalid Choice!%s\n");
}
If you are going to call this function more than once, you need to
remove the '\n' (from the ENTER key) that is sitting in the buffer.
Otherwise, your next call will result in the error message.

If the user does enter an invalid character, it would be nice to let
him re-enter.
}
/*Computer Cal it's choice*/
int getCompChoice()
int getCompChoice(void)
{
int comp = rand()%3;
switch (comp){
case 0: return 'R';
case 1: return 'P';
case 2: return 'S';
}
return comp;
}

void result(int user, int comp)
{

if (user=comp)
printf("Draw\n");
switch (user) {
case 1:
if (comp == 'R')
printf("You Win\n");
You need a break statement between each case.
case 2:
if (comp == 'P')
printf("You Win\n");
case 3:
if (comp == 'S')
printf("You Win\n");
default:
printf("You Loose\n");
exit(1);
After one loss you quit the whole program? Use EXIT_FAILURE instead
of 1 for portability.
}

return;
}
main(void)
int main(void)
{
int score = 0;
srand(time(NULL));/* seed the random number generator*/
int getValidInteger();
getValidInteger currently requires three arguments.
if (getValidInteger=getCompChoice) score++;
Surely this gave you a syntax error. Without the parentheses, an
unadorned function name evaluates to the address of the function. The
left of the = is not a modifiable l-value so it cannot receive the
address of getCompChoice.

I think what you want here is a loop like

get user's input
if quit
exit loop
compute computer's choice
determine result of game (and keep statistics?)
repeat loop
return score;
After exiting the loop, you may print the stats but you should return
0 (or EXIT_SUCCESS) to indicate normal completion of the program.
}
I think I am going about it the wrong way. Could someone steer my in
the right direction, without giving me the answer.


It might be easier if result() returned an indicator of who won and
the messages and statistics were processed in main().
Remove del for email
May 5 '06 #3
gc <gr*********@bigpond.com> wrote:
/*User Inputs R,P,S,Q*/
int getValidInteger(int min, int max, char prompt[])
{
char userInput;
printf("\nEnter R(ock), P(aper),or S(cissors) or Q(uit): ");
scanf("%c",&userInput);
//userInput=toupper(userInput);
switch(userInput){
case 'Q':return 0;
case 'R':return 1;
case 'P':return 2;
case 'S':return 3;
default: printf("Invalid Choice!%s\n");
}
}
As Barry noted, your users (including your instructor, if this is a
class assignment) would probably like another chance to enter a
character if necessary. Using scanf() is fraught with perils and
gotchas, so I would heartily recommend using the much more
straightforward getc() since it is adequate for your task.
main(void)
It's probably not of interest to you, but functions without a
specified return type are no longer permissible under the C99
standard. It wasn't a great idea even when it was legal, so you
should simply write

int main( void )

and avoid the situation altogether.
I think I am going about it the wrong way. Could someone steer my in
the right direction, without giving me the answer.


You look like you're pretty close. Observe that the mistake pete
pointed out - using '=' instead of '==' - has a major impact on your
program's behavior, and fixing them (there are at least two) will work
wonders.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
May 6 '06 #4
Christopher Benson-Manica <at***@otaku.freeshell.org> wrote:
I think I am going about it the wrong way. Could someone steer my in
the right direction, without giving me the answer.
You look like you're pretty close. Observe that the mistake pete


Er, that would be pemo that noted this mistake. Pete's posts are
certainly worth reading, however. Oops.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
May 6 '06 #5
gc

pemo wrote:

getValidInteger and getCompChoice - when used like this resolve to the
addresses of those functions - i.e., constant values. You're then trying to
assign one constant to another - did you mean == instead of = ? To invoke a
function ... well, you know how to do that.
if (getValidInteger=getCompChoice)


You might want to check for other problems with == vs. = in the code, and
also that you're calling printf /honestly/ throughout.
--

This may seem like a silly question, but can you please elaborate on
your comment about the printf statement.

May 8 '06 #6
gc

Christopher Benson-Manica wrote:
You look like you're pretty close. Observe that the mistake pete
pointed out - using '=' instead of '==' - has a major impact on your
program's behavior, and fixing them (there are at least two) will work
wonders.

--

I've made a lot of changes, and I'm trying to get a if then loop
happening. What I want to happen is if the user doesn't equal the
computer, ie not a tie then I want the game to continue and keeping the
score at the same time:
include <stdio.h>
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */

/*User Inputs R,P,S,Q*/
int getValidInteger(int min, int max, char prompt[])
{
int userInput;
printf("Enter 1(Rock), 2(Paper),or 3(Scissors) or 0(Quit): ");
scanf("%i",&userInput);
switch(userInput){
case 0 :return 'Q';
case 1 :return 'R';
case 2 :return 'P';
case 3 :return 'S';
}
}
/*Computer Cal it's choice*/
int getCompChoice(void)
{
int comp = rand()%3;
printf("Computer choses:%c\n", comp["RPS"]);
return comp+1;
}
int main(void)

{
int score;
int validInput =getValidInteger(0,3, "enter number") ;
int compChoice =getCompChoice();
srand((unsigned) time(NULL));
if (validInput != compChoice) score++;{
validInput = getValidInteger(0,3, "enter number");
}
return score;
}

I think it is in the if statement, could someone steer me in the right
direction.

Greg

May 8 '06 #7
gc wrote:
pemo wrote:

getValidInteger and getCompChoice - when used like this resolve to
the addresses of those functions - i.e., constant values. You're
then trying to assign one constant to another - did you mean ==
instead of = ? To invoke a function ... well, you know how to do
that.
if (getValidInteger=getCompChoice)


You might want to check for other problems with == vs. = in the
code, and also that you're calling printf /honestly/ throughout.
--

This may seem like a silly question, but can you please elaborate on
your comment about the printf statement.


printf("Invalid Choice!%s\n");

--
==============
Not a pedant
==============
May 8 '06 #8

gc wrote:
Christopher Benson-Manica wrote:
You look like you're pretty close. Observe that the mistake pete
pointed out - using '=' instead of '==' - has a major impact on your
program's behavior, and fixing them (there are at least two) will work
wonders.

-- I've made a lot of changes, and I'm trying to get a if then loop


There is not such thing as an if-then loop.

Look up while() and for() loops.

and BTW you still need to debug switch logic where you compare the user
and computer selections. It is not doing what you think you told if to
do.

[] I think it is in the if statement, could someone steer me in the right
direction.

Greg


HTH,
ed

May 8 '06 #9
pemo wrote:

printf("Invalid Choice!%s\n");


Causes undefined behaviour (there's no argument corresponding to
the %s). I don't see anything wrong with the OP's printf statements.

May 8 '06 #10
gc wrote:
int getValidInteger(int min, int max, char prompt[])
{
int userInput;
printf("Enter 1(Rock), 2(Paper),or 3(Scissors) or 0(Quit): ");
scanf("%i",&userInput);
switch(userInput){
case 0 :return 'Q';
case 1 :return 'R';
case 2 :return 'P';
case 3 :return 'S';
}
You need a 'default' case, for when the person enters something
other than 1,2,3,0.

You have to check scanf() to see if it succeeded or not.

Even better, you could use a loop and ask the user the
question again until they enter a valid number.

Also it would be good if you actually used min, max, and prompt.

Finally, as someone else mentioned, you need to eat up
any newline characters (eg. the person is going to be typing
'P' 'ENTER' so you need to get that ENTER).

After the scanf, use getchar() to grab characters until you have
gotten a '\n'. (Use a loop for this; see below for an example).
int main(void)

{
int score;
int validInput =getValidInteger(0,3, "enter number") ;
int compChoice =getCompChoice();
srand((unsigned) time(NULL));
Calling srand before you actually get the random number, would
be a good idea :)
if (validInput != compChoice) score++;
Note that this is not the way that rock scissors paper is
normally scored...:)
{
validInput = getValidInteger(0,3, "enter number");
}
return score;
}


That doesn't make any sense. Try using a "forever" loop.
With loops, everything that you want to happen repeatedly
must go INSIDE the loop. For example I guess you want
to call getCompChoice() and getValidInteger() every time.

int score = 0;
srand( time(NULL) );

for (;;) /* keep looping forever until we say otherwise */
{
int compChoice =getCompChoice();
int validInput =getValidInteger(0,3, "enter number") ;

if ( validInput == 0 )
break; /* we want to exit the loop when they press Quit */

if (validInput != compChoice)
score++; /* i'll let you improve this one yourself */
}

return score;

May 8 '06 #11
"gc" <gr*********@bigpond.com> writes:
[...]
/*User Inputs R,P,S,Q*/
int getValidInteger(int min, int max, char prompt[])
{
int userInput;
printf("Enter 1(Rock), 2(Paper),or 3(Scissors) or 0(Quit): ");
scanf("%i",&userInput);
switch(userInput){
case 0 :return 'Q';
case 1 :return 'R';
case 2 :return 'P';
case 3 :return 'S';
}
}

[...]

Your comment says the user inputs R, P, S, or Q. In fact, you're
forcing the user to input 1, 2, 3, or 0, and then translating this to
'R', 'P', 'S', or 'Q'.

Why not just prompt for and input a character, check whether it's
valid, and return it if it is? It makes for more intuitive user
interface and makes your code simpler.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 9 '06 #12
Old Wolf wrote:
pemo wrote:

printf("Invalid Choice!%s\n");


Causes undefined behaviour (there's no argument corresponding to
the %s). I don't see anything wrong with the OP's printf statements.


That's taken from the OP's original code.
--
==============
Not a pedant
==============
May 10 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
7
by: TooNaive | last post by:
Hello all, I'm taking a C class and am having to write a program to play a game of rock, paper scissors, and with the output of: You chose paper and I chose rock. You Win where paper is the...
102
by: BoogieWithStu22 | last post by:
I am running into a problem with a web page I have created when viewing it in IE6 on some machines. The page has a database lookup. The user enters an account name or number and clicks a lookup...
1
by: Marty | last post by:
By the subject you may be able to tell that I am not pleased... Here is my situation: We have an old but very useable program that exports information out into a wk1 file. I had a program that I...
11
by: blackhacker | last post by:
Please can anyone help me to make a simple simple Java code to force the game scissor,paper,rock to work ? for example i know how to do it in Visual Basic but not in java,for example this is kind...
0
by: arulprakash456 | last post by:
SUJI S ROCK YOU PLEASE CLICK HERE $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ http://voltage_voltageextra_lowvoltage.blogspot.com $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
0
by: meisnernel73884 | last post by:
crack rock cooking http://crack.cracksofts.com
1
by: flg22 | last post by:
Hi I am working on this Rock, paper, scissors java game and the program works, but I can not figure out how to get the images to load onto the program. So my question is how do I get the images to...
8
by: jmf777 | last post by:
Hi here is my problem I want to have the outcome of my rock paper scissors game to print outcomes like: You chose paper and I chose rock. You win. The value for player_choice and machine_choice is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.