Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the following code prints "draw!" as output no matter what input the user provides
pls help.

What I have tried:

C++
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>

void main()
{
	clrscr();

	int choice1;
	char choice2[50];
	int compare(const char * ,const char * )   ;        //prototype
	char s[100];
	cout<<"\n\n WELCOME TO STONE PAPER SCISSORS "<<endl<<endl;
	cout<<" enter your choice"<<endl;

	cout<<"\n 1:SCISSORS \n";
	cout<<"\n 2:ROCK \n";
	cout<<"\n 3:PAPER \n";
	cout<<"enter choice number";
	cin>>choice1;

	if(choice1==2)
	{
		cout<<"you have entered Stone!";
		strcpy(s,"ROCK");
	}
	if (choice1==1)
	{
		cout<<"you have entered scissors";
		strcpy(s,"SCISSORS");
	}
	if(choice1==3)
	{
		strcpy(s,"PAPER")  ;
		cout<<"you have entered paper";
	}
	randomize();
	float point =2;
	float compchoice;
	compchoice=random(point);

	if (compchoice<0.37)
	{
		strcpy(choice2,"ROCK")    ;

	}
	else  if(compchoice<0.64)
	{
		strcpy( choice2,"PAPER");
	}
	else
	{
		strcpy(choice2,"SCISSORS");
	}

	cout<<endl;
	cout<<"User Choice="<<s<<endl;
	cout<<"Computer Choice="<<choice2<<endl;
	cout<<s<<"\t"<<"VS"<<"\t"<<choice2<<"="<<" ";

	int p=compare(s,choice2);
	if(p==1)
	{
		cout<<"computer wins";
		if(p==0)
			cout<<"user wins";
		if(p==-1)
			cout<<"draw!";

		getch();
	}
	int compare(const char* s, const char * choice2)
	{
		if(s=="SCISSORS")
		{
			if(choice2=="ROCK")
				return 1;
			else
				return 0;
		}
		else if(s=="ROCK")
		{
			if(choice2=="SCISSORS")

				return 0;
			else
				return 1;

		}
		else if(s=="PAPER")
		{
			if(choice2=="SCISSORS")
				return 1;
			else
				return 0;
		}
		else
		return -1;
	}
Posted
Updated 13-Aug-16 9:25am
v3
Comments
[no name] 13-Aug-16 10:21am    
I would suggest you to use switch case instead of conditional statements(if and else in your case). Set break points and see what happens and I guess it is nearly impossible with your compiler so kindly change your compiler.
I see some outdated header files, default function calls etc.,

Advice:
Use a programmer's text editor and use indentation
Notepad++ Home[^]
By indenting lake I have done in your question, one can see that a } is missing at the end of main.

Another problem is that you can't compare char arrays with operator ==.
Author: Albert Einstein
“Everything should be made as simple as possible, but no simpler.”

Advice:
Simplify your code : use numbers to store choices and use strings only when you output a result.
 
Share this answer
 
Comments
Abhijit Dare 14-Aug-16 6:29am    
then how can arrays be compared? remember i have copied an array to a variable s using strcpy() .the i compared that variable with and character value which is perfectly legal
Patrice T 14-Aug-16 7:12am    
s is also an array of char, so you try to compare 2 array with ==.
See:
strcmp - C++ Reference[^]
So start with the debugger, and try to find out why!
Put a breakpoint on the line:
C++
int p=compare(s,choice2);
And run your app in the debugger. When it gets to that line, it'll stop and wait for you to tell it what to do.
Use the debugger to look at exactly what is in the various variables and work out exactly what should happen when you execute the next line before you execute it. Did it do exactly what you expected? If so, move on to the next. If not, why not? What did it do that you didn't expect, or not do that you did?

This is a skill - predictably called debugging - and like all skills you only develop it by using it. And it's a lot easier to learn it on a simple app like this than a 100,000 line behemoth written by someone else! :laugh:
So give it a try, and see what you can find out.
 
Share this answer
 
Comments
Abhijit Dare 14-Aug-16 6:26am    
can u suggest the best c++ editor with debugger?
OriginalGriff 14-Aug-16 6:35am    
Visual Studio.
It's probably free for you direct from Microsoft, and it's the best in the world.

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