473,722 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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",&use rInput);
//userInput=toupp er(userInput);
switch(userInpu t){
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 (getValidIntege r=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 2225
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",&use rInput);
//userInput=toupp er(userInput);
switch(userInpu t){
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 (getValidIntege r=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 (getValidIntege r=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*********@bi gpond.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",&use rInput);
//userInput=toupp er(userInput);
switch(userInpu t){
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(v oid)
{
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 (getValidIntege r=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*********@bi gpond.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",&use rInput);
//userInput=toupp er(userInput);
switch(userInpu t){
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)cybers pace.org | don't, I need to know. Flames welcome.
May 6 '06 #4
Christopher Benson-Manica <at***@otaku.fr eeshell.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)cybers pace.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 (getValidIntege r=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",&use rInput);
switch(userInpu t){
case 0 :return 'Q';
case 1 :return 'R';
case 2 :return 'P';
case 3 :return 'S';
}
}
/*Computer Cal it's choice*/
int getCompChoice(v oid)
{
int comp = rand()%3;
printf("Compute r choses:%c\n", comp["RPS"]);
return comp+1;
}
int main(void)

{
int score;
int validInput =getValidIntege r(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 (getValidIntege r=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

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

Similar topics

354
15872
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 hooked up to interfaces. The Gtk is more than enough gui.
7
3806
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 players choice, and rock is the computers choice, for reference i was given the p_r_s.h, compare.c, main.c, selection.c, and wrt.c, and i have had to build the report.c to describe the
102
5133
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 button. This hits an AS400 database looks for matches and returns a dataset that is used to populate a datagrid. The user then selects one of the entries from the list, this entry is used to populate a couple of textboxes on the page and a couple...
1
1616
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 had developed a couple years ago that would open these files through Excel. This allowed me to grab specific information that would then in turn be used in some AutoCAD drawings. Well my nice shiny new computer with it's nice shiny new Office suite...
11
2425
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 of what i want Rock beats Scissor Paper beats rock Scissor beats paper msgbox 'Enter your choce' If txta.text = "Rock" then
0
1126
by: arulprakash456 | last post by:
SUJI S ROCK YOU PLEASE CLICK HERE $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ http://voltage_voltageextra_lowvoltage.blogspot.com $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
0
874
by: meisnernel73884 | last post by:
crack rock cooking http://crack.cracksofts.com
1
3118
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 load up with the program? I am using JCreator for this project. I have created the Basic Java Application project, and then added in the 3 .java files that I need to run the program, but I just can not figure out how or where I need to upload the...
8
5462
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 declared in select.cpp (listed below). My question is how would I go about telling, the report.cpp code below to check the select.cpp code for the variable to print? #include "p_r_s.h" void report(outcome result, int *win_cnt_ptr, int...
0
8863
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.