473,387 Members | 1,463 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,387 software developers and data experts.

Random rock paper scissors game... errors in code...please help

this is for our project, and i'm still a beginner in programming, so guys I need some help. I want to create a random rock paper scissors game(player vs computer)... I tried many times to delete,recreate and edit my code but i still don't know how to get it right. This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string>
#include <time.h>
using namespace std;

int main()
{
clrscr();
char b;
string words[3]={"rock","paper","scssors"}
srand(time(NULL));
words[rand()%3];
printf("Enter:");
scanf("%c",b);
for(int g=12;g<=21;g++)
{
if(words==b)
{
printf("Great!");
}
else{
printf("Wrong!");
}
getch();
return 0;
}
}


I know this looks funny to those who are pros but can you explain to me what's wrong with my code.
Feb 15 '15 #1
5 2045
weaknessforcats
9,208 Expert Mod 8TB
First, what is the type of the words array. It says string but there is no string type in C and the code does not look like C++? I suspect the array needs to be an array of char* since each element is itself a string.

Second, this does nothing:
Expand|Select|Wrap|Line Numbers
  1. words[rand()%3];
You need to relate this so something.

Third, the user enters a string so using %c in scanf is an error. %c is for a single char. You need %s for an entire string.


Fourth, I see:
Expand|Select|Wrap|Line Numbers
  1. if(words==b)
  2.         {
  3.            etc...
so I suspect you want to compare an element of the array to the value entered by the user. In C you compare strings using strcmp.

In addition, the program needs to pick one f the strings in the words array so
word[rand()%3] is the correct thing to do. You just need to compare this to what the user entered:

Expand|Select|Wrap|Line Numbers
  1. if(!strcmp(words[rand()%3],b))
  2.         {
  3.             ...they match....
  4.  
  5.            etc...
I hope this gets you along a bit further.
Feb 15 '15 #2
donbock
2,426 Expert 2GB
It looks intend to play 10 rounds of the game (12 to 21). If so, then the user should enter a new choice each round'(printf & scanf should be inside the loop).
Feb 16 '15 #3
thanks for replying weaknessforcats... I use TurboC which runs on Dosbox for windows 8.1 So here's my new code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <time.h>

int main()
{
clrscr();
char b;
char words[]={'r','p','s','\0'};
srand(time(NULL));
printf("Enter:");
scanf("%s",b);
for(int g=12;g<=21;g++)
{
if(!strcmp(words[rand()%3],b)
{
printf(" %s\nGreat!",words);
}
else
{
printf("Wrong!");
}}
getch();
return 0;
}

Errors are:Cannot convert 'int' to 'const char *'
Type mismatch in parameter '_s1' in call to 'strcmp(const char *, const char *)'
Cannot convert 'int' to 'const char *'
Type mismatch in parameter '_s2' in call to 'strcmp(const char *, const char *)'

Here i am again asking you to help me beacause i don't fully understand what your point is, i'm sorry for being annoying. What i am trying to do is to make a rock,paper,scissors game which will be randomly picked by the computer to opposed the player's choice.
Feb 17 '15 #4
donbock
2,426 Expert 2GB
strcmp(words[rand()%3,b)
Both of these arguments are chars (a variety of integers), but strcmp expects strings (a variety of pointer). That's the mismatch that the compiler is complaining about. By the way, adding '\0' as the fourth element of words doesn't accomplish anything useful.

Comparing two chars is straightforward:
if( words[rand()%3] == b)
However, notice that 'R' will be considered different than 'r'. Is that what you want?

The user makes a single choice and then the program makes 10 choices. Is that what you want?
Feb 17 '15 #5
What I really want is the user could pick his/her own choice every round,whether it is small or capital letter,same as the program picks random choice every round.Also, I don't know how to reset the game automatically (not the whole game but repicking the user's choice again) after the user inputs an invalid letter. Um, can you send me a complete working algorithm that I can view so that I can analyze it and know my mistakes? As you can see, this code of mine isn't really complete and neat. Also,the real purpose of these rock-paper-scissors game is to identify whether the computer or the player will take the turn first on my bigger game project in TC... "Battleship". Please don't mind how many rounds I did put in this code 'cause this is just a sample or a training to me of how would I make a player and a computer take turns alternately. Sorry for some English grammatical error.
Feb 17 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: 1111111111 | last post by:
I am writing a program that tells users to enter 2 sides of a rectangle. From that the program will output a drwaing of the rectangle using "*"'s... Here is my problem. Say I enter 5 and 10 for...
11
by: SZ | last post by:
Hi, I've hit this core multiple times when running an application (coded in C) running on BSD OS. Using gdb getting to the core, it shows: ..... (gdb) bt #0 0x8541fa3 in mTimerQUnlink...
1
SKJoy2001
by: SKJoy2001 | last post by:
PLEASE HELP ME!!! P E R L!!! I have a CGI (PERL) file namely 'test.cgi' and it has the correct permission (755) on the FTP server and it is within the CGI path. I have the following code in it:...
5
by: alem | last post by:
Hi all i could not joine MS Access table to VB the code i write below Private Sub Form_Load() Set cn = New ADODB.connection cn.Provider = "Microsoft.jet.oledb.4.0" cn.Open "C:\db1.mdb"...
5
Tarantulus
by: Tarantulus | last post by:
Hi Guys/Girls, I have been coding a website for a friend, everything worked fine on my localhost setup, then I FTP to their host and now it's broken.... the error is Fatal error: Unknown():...
13
by: ishakarthika | last post by:
how can i compare the a private variable of a class and a value in the column of a text file. there is a syntax error in my code while comparing. senario is i am getting bus details like busno,...
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...
7
by: chickendavis | last post by:
Hi folks, I have what seems like a fairly straight forward question which im sure has a simple answer as well..but i can't seem to find it. I'm using the Microsoft .NET WSE2 library in order to...
2
by: Ecot | last post by:
Hi all, I have not got any php knowledge and I have found a php code on the net and using it to send e-mail from a web html form.So far the form and the code works fine. I have the following php...
0
by: Sitar | last post by:
Hello all, I have serious problem with valgrind errors. Code is 1400lines long :( .so, it is imposible to post it in here. Is it someone able to check my code and help me with that errors. I think...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.