Rock Program

G

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
 
P

pemo

gc said:
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.
 
B

Barry Schwarz

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
 
C

Christopher Benson-Manica

gc said:
/*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.
 
C

Christopher Benson-Manica

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.
 
G

gc

pemo said:
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.

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.
 
G

gc

Christopher said:
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
 
E

Ed Prochak

gc said:
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
 
O

Old Wolf

pemo said:
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.
 
O

Old Wolf

gc said:
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;
 
K

Keith Thompson

gc said:
/*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.
 
P

pemo

Old said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top