Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c++ string arrays vs. char arrays

Status
Not open for further replies.

RenderedAwake

Technical User
Feb 23, 2005
2
US
Ok, I'm writing a paper, rock, scissors program & I've run into a bit of a problem. I think I have everything set up for the most part but I'm stuck when it comes to these strcmpr string thing. I want the program to have the computer randomly play against the player, so I set it up so that according to the random numbers, the computer choice becomes equal to a word ( a string array). However, for the player choice I set it up as a char array, and when I compare the computer choice to the player choice it doesn't work. I'm guessing this is because one is a char array and the other is a string array, but I don't know how to do this properly. I tried changing the player choice into a string but it didn't even recognize the word string. Any help and suggestions would be greatly appreciated!

(this is my code thus far)
Code:
   srand((unsigned)time(NULL)); 
   int d=rand()%3;  
   if (d==0)
   {
      cout<<"Computer choice: Paper"<<endl;
      computerChoice="Paper";
   }
   else if (d==1)
   {
      cout<<"Computer choice: Rock"<<endl;
      computerChoice="Rock";
   }

   else if (d==2)
   {
      cout<<"Computer choice: Scissors"<<endl;
      computerChoice="Scissors";
   }
   cout<<d;
   return 0;
 
Store the choice as an int, not a string. Then make some function like ChoiceToString() which converts your number to a string. I don't see any reason to work with strings here for comparison since ints can be used just as easily and are simpler to work with.

That said, you may need to look into strcmp(). A word should be a single string, definitely not a string array. When you say it "doesn't work", what do you mean, it won't compile or it will compile but doesn't do what you want. Post more code and your specific error message.
 
Well the reason I'm working with strings is because I want to familiarize myself with string and char arrays and strcmp. I think I have most of the code pretty much figured out (I pasted the whole thing below). However, it doesn't do what I want. The program runs without errors, the intro screen works well, the player input works, the computer choice is shown..but it doesn't display the results of the comparisons.


Code:
#include<iostream>
#include<math.h>
#include<string>
#include<stdlib.h>
#include<cstdlib>
#include<ctime>

int checkLoop;
char yesOrno[10]="";		

using namespace std;

int main()
{


while(checkLoop == 0){   				
	
		//intro screen
		cout << "                                                                             " << endl;
		cout << "            Rock          Paper         Scissors                             " << endl;
		cout << " ____________________________________________________________________________" << endl;
		cout << "| This is a game of rock, paper, scissors. You will the best of three rounds |" << endl;
		cout << "|  against the computer. To pick either rock, paper, or scissors simply      | " << endl;
		cout << "|  type in your choice and hit enter. The computer will randomly pick its    | " << endl;
		cout << "|  choice. At the end of the game type y to play again & n to end the game.  | " << endl;
		cout << "|               Do you understand?  please enter y or n                      | " << endl;
		cout << "|____________________________________________________________________________| " << endl; 
		
		//user input
		cin.getline(yesOrno, 10);
	
if((strcmp(yesOrno,"n")==0)||(strcmp(yesOrno,"y")==0))			//if the user input is a y or n go into this other loop
	{	
		if(strcmp(yesOrno,"y")==0)
			{
				checkLoop=1;									
				system("CLS");						
				cout<< "Get ready to start the game"<<endl;
				}
			else												//if the user input is no repeat the directions
			
			{
				system("CLS");
				cout<< "Read the directions again";
				checkLoop=0;									//makes this loop repeat until y or n entered
			}	
				}	 
else
	{ 
	 				
	 	cout<< " Enter a y or n "<<endl;						//if the user enters something other than a y or n print this statement										
	 	cout<<endl;
	 	cout<<endl;	
	 	}

	}



 
string computerChoice;

char playerChoice[20]="";

cout << "Input player choice."<<endl;
cin.getline(playerChoice,10);




if((strcmp(playerChoice,"paper")==0)||(strcmp(playerChoice,"rock")==0)||(strcmp(playerChoice,"scissors")==0))

{

   srand((unsigned)time(NULL)); 
   int d=rand()%3;  
   
   if (d==0)
   {
      cout<<"Computer choice: Paper"<<endl;
      computerChoice=="Paper";
   }
    if (d==1)
   {
      cout<<"Computer choice: Rock"<<endl;
      computerChoice=="Rock";
   }

   if (d==2)
   {
      cout<<"Computer choice: Scissors"<<endl;
      computerChoice=="Scissors";
   }
   cout<<d;
  


	if((strcmp(playerChoice,computerChoice.c_str()))==0)cout<<"Tie"<<endl;
	
	if ((strcmp(playerChoice,computerChoice.c_str()))==2)cout<< " paper beats rock, computer wins this round"<<endl;
	
	if((strcmp(playerChoice,computerChoice.c_str()))==3)cout<<"scissors over paper,player wins this round "<<endl;
		
	if((strcmp(playerChoice,computerChoice.c_str()))==-2)cout<<"paper beats rock,player wins this round"<<endl;
	
	if((strcmp(playerChoice,computerChoice.c_str()))==-3)cout<<"scissors beats paper, computer wins"<<endl;
	
	if ((strcmp(playerChoice,computerChoice.c_str()))==1)cout<<"rock beats scissors, computer wins this round"<<endl;
	
	if ((strcmp(playerChoice,computerChoice.c_str()))==-1)cout<<"rock beats scissors, player wins this round"<<endl;
	
}	

else

{
	cout<<"you made a mistake in typing"<<endl;
}

 


return 0;

}
 
Oh my! strcmp() returns (not defined by the language standard) negative (arg1 < arg2) or positive (arg1 > arg2) or (exactly) zero (arg1 == arg2) values - that's all about strcmp() values...
You can't compare strcmp(...) == 2 etc (only <0, >0 or ==0).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top