4 Replies - 619 Views - Last Post: 15 October 2011 - 08:01 AM Rate Topic: -----

#1 raulian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 09-January 10

Valid data subroutine not being executed

Posted 15 October 2011 - 01:08 AM

My program is a simple game of rock paper scissors I have everything working..just not the way I want it to. My Valid_data subroutine is not being executed at least I think its not I wish for my program to give an error msg if wrong data is inputed and also close can someone explain why it's not validating


#include <iostream>
#include <conio.h>
#include <fstream>
#include <ctype.h>
using namespace std;


bool eof (char, char);
char Valid_data (char, char);
char winner (char, char);


int main()
{


	char P1, P2, W;


	while( !eof (P1, P2) )
	{
	if( Valid_data (P1, P2) )

	{	system("cls");
		cout<<"player 1, enter your choice.\n\n";
		P1=getch();
		putch('*');
		cout<<"\nplayer 2, enter your choice.\n\n";
		P2=getch();
		putch('*');
		cout<<"\nPlayer one entered "<<P1<<" and player two entered "<<P2<<".\n";
		W=winner (P1, P2);
			if( W == 1)
				cout<<"\nThe winner is: player one";
					else if( W == 2)
	 				  cout<<"\nThe winner is: player two";
		     	    	  else if (W == 3)
							cout<<"\nThe winner is: ... oh wait tie game!";
								else
									cout << "\nEither one or both users entered invalid data\n";
									getch();
									
	}
		else 
			cout<<"Bad data\n";
		
		getch();
		
	}

	
	getch();
	return 0;
}

//***********************************
bool eof (char P1, char P2)
{
	if(P1 == 'Q' || P2 == 'Q')
		return true;
	else 
		return false;
}


//*************************************
char Valid_data (char P1, char P2)
{

		if(P1 == 'R' || 'P' || 'S' && P2 == 'R' || 'P' || 'S')
			return true;
		else 
			return false;
	
}
//**************************************
char winner (char P1, char P2)
{
	if((P1 == 'P' && P2 == 'R'|| P1 == 'R' && P2 == 'S') || P1 == 'S' && P2 == 'P')
		return 1;
	else 
		if((P1 == 'R' && P2 == 'P'|| P1 == 'S' && P2 == 'R') || P1 == 'P' && P2 == 'S')
			return 2;
			else if(P1 == 'R' && P2 =='R' || P1 == 'P' && P2 == 'P' || P1 == 'S' && P2 == 'S')
				return 3;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Valid data subroutine not being executed

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: Valid data subroutine not being executed

Posted 15 October 2011 - 02:29 AM

if(P1 == 'R' || 'P' || 'S' && P2 == 'R' || 'P' || 'S')


"'P'" is not a condition expression. It can be evaluated as a boolean expression, though. It's non zero, so it's true.

You did the syntax for winner correctly, though? Ok, closer. You need more (). Simply, you need "X==Y" for most everything and be explicit about the grouping of it.
Was This Post Helpful? 0
  • +
  • -

#3 raulian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 09-January 10

Re: Valid data subroutine not being executed

Posted 15 October 2011 - 03:07 AM

Thank you I finally have the code please let me know if you have any suggestions on the programs such as capitalization restrictions which just crossed my mind.

#include <iostream>
#include <conio.h>
#include <fstream>
#include <ctype.h>
using namespace std;


bool eof (char, char);
char Valid_data (char, char);
char winner (char, char);


int main()
{


	char P1, P2, W;


	while( !eof (P1, P2) )
	{
	

		system("cls");
		cout<<"player 1, enter your choice.\n\n";
		P1=getch();
		putch('*');
		cout<<"\nplayer 2, enter your choice.\n\n";
		P2=getch();
		putch('*');

	if( Valid_data (P1, P2) )
		{
			cout<<"\nPlayer one entered "<<P1<<" and player two entered "<<P2<<".\n";
			W=winner (P1, P2);
			if( W == 1)
				cout<<"\nThe winner is: player one";
					else if( W == 2)
	 				  cout<<"\nThe winner is: player two";
		     	    	  else if (W == 3)
							cout<<"\nThe winner is: ... oh wait tie game!";
								else if (W == 4) 
									cout<<"\A player has entered Q now Quiting Game\n";
									getch();
									
		}
	else 
		cout<<"Bad data please enter a valid character R,P,S or Q\n";
		getch();
	}
	getch();
	return 0;
}

//***********************************
bool eof (char P1, char P2)
{
	if(P1 == 'Q' || P2 == 'Q')
		return true;
	else 
		return false;
}


//*************************************
char Valid_data (char P1, char P2)
{

		if((P1 == 'R' || P1 == 'P' || P1 == 'S') && (P2 == 'R' || P2 == 'P' || P2 == 'S') || (P1 == 'Q' || P2 == 'Q'))
			return true;
		else 
			return false;
	
}
//**************************************
char winner (char P1, char P2)
{
	if((P1 == 'P' && P2 == 'R'|| P1 == 'R' && P2 == 'S') || P1 == 'S' && P2 == 'P')
		return 1;
	else 
		if((P1 == 'R' && P2 == 'P'|| P1 == 'S' && P2 == 'R') || P1 == 'P' && P2 == 'S')
			return 2;
				else
					 if(P1 == 'R' && P2 =='R' || P1 == 'P' && P2 == 'P' || P1 == 'S' && P2 == 'S')
						return 3;
							else 	
								if (P1 == 'Q' || P2 == 'Q')
									return 4;
}


Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: Valid data subroutine not being executed

Posted 15 October 2011 - 03:56 AM

The biggest thing is return types. You have Valid_data and winner as char. Valid_data should be bool and winner should be int.

The winner function has several states, they should have a name that maps to a number. However, I don't think you need them at all, or eof.

When a user enters a char, check it immediately. If it =='Q', leave. If it's invalid, make them enter it again. Use more functions. Don't repeat things. If you find yourself entering the same thing in reverse, you want another function.

Logic is your friend and you can use it to reduce your complexity. If you're evaluating something and it returns boolean, why not just return the evaluation?

e.g.
bool isValidEntry(char P) { return (P == 'R' || P == 'P' || P == 'S' || P == 'Q'); }

bool Valid_data (char P1, char P2) { return isValidEntry(P1) && isValidEntry(P2); }

bool isWinner(char P1, char P2) {
	// note I added parens
	return (P1 == 'P' && P2 == 'R') || (P1 == 'R' && P2 == 'S') || (P1 == 'S' && P2 == 'P');
}

int winner (char P1, char P2) {
	if (P1 == 'Q' || P2 == 'Q') { return 4; }
	if (P1 == P2) { return 3; }
	if (isWinner(P1, P2) { return 1; }
	// logic says if we got to here, the other guy won
	return 2;
}



In C++, I like all variable names to start lower case. Your else if indents needn't bee so intented.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 raulian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 09-January 10

Re: Valid data subroutine not being executed

Posted 15 October 2011 - 08:01 AM

Well I took your advice into account and included many functions the only problem I am having to complete this program is "how do I check the input first to see if it is a Q or q to give the return value 4 msg then exit note this does not occur when you enter an invalid character such as

player 1

E

player 2

Q

invalid data please enter R,P,S or Q when it should echo A player has chosen to quit or whatever the return value 4 msg is.

#include <iostream>
#include <conio.h>
#include <fstream>
#include <ctype.h>
using namespace std;


bool eof (char, char);
bool Valid_data (char, char);
int winner (char, char);
bool isWinner(char, char);
bool isValidEntry(char);
bool lower_Valid_data (char, char);
bool islowerValidEntry(char); 
bool islowerWinner(char, char);
int lower_winner (char, char);


int main()
{
	char P1, P2, W;

	while( !eof (P1, P2) )
	{
	

		system("cls");
		cout<<"player 1, enter your choice.\n\n";
		P1=getch();
		putch('*');

		cout<<"\nplayer 2, enter your choice.\n\n";
		P2=getch();
		putch('*');

	if( Valid_data (P1, P2) )
		{
			cout<<"\nPlayer one entered "<<P1<<" and player two entered "<<P2<<".\n";
			W=winner (P1, P2);
			if( W == 1)
				cout<<"\nThe winner is: player one";
					else if( W == 2)
	 				  cout<<"\nThe winner is: player two";
		     	    	  else if (W == 3)
							cout<<"\nThe winner is: ... oh wait tie game!";
							else if (W == 4) 
								cout<<"\nA player has entered Q now Quiting Game\n";
									getch();
									
		}
	else 
		if(lower_Valid_data (P1, P2))
		{
			cout<<"\nPlayer one entered "<<P1<<" and player two entered "<<P2<<".\n";
			W=lower_winner (P1, P2);
			if( W == 1)
				cout<<"\nThe winner is: player one";
					else if( W == 2)
	 				  cout<<"\nThe winner is: player two";
		     	    	  else if (W == 3)
							cout<<"\nThe winner is: ... oh wait tie game!";
								else if (W == 4) 
									cout<<"\nA player has entered Q now Quiting Game\n";
									getch();
									
		}
	else
		cout<<"\nA user entered invalid data please enter a valid character R,P,S or Q\n";
		getch();
	}
	getch();
	return 0;
}

//**********************************************************************************
bool eof (char P1, char P2)
{
	if(P1 == 'Q' || P2 == 'Q' || P1 == 'q' || P2 == 'q')
		return true;
	else 
		return false;
}


//************************************************************************************
bool Valid_data (char P1, char P2)
{
return isValidEntry(P1) && isValidEntry(P2); 
}
//***************************************************************************************
bool isValidEntry(char P) 
{ 
return (P == 'R' || P == 'P' || P == 'S' || P == 'Q'); 
}
//***************************************************************************************
bool lower_Valid_data (char P1, char P2)
{
return islowerValidEntry(P1) && islowerValidEntry(P2); 
}
//***************************************************************************************
bool islowerValidEntry(char P) 
{ 
return (P == 'r' || P == 'p' || P == 's' || P == 'q'); 
}
//***************************************************************************************
int winner (char P1, char P2)
{

	if (P1 == 'Q' || P2 == 'Q') { return 4; }
	if (P1 == P2) { return 3; }
    if (isWinner(P1, P2)) { return 1; }
    return 2;
	
}
//***********************************************************************************************
bool isWinner(char P1, char P2) 
{
   return (P1 == 'P' && P2 == 'R') || (P1 == 'R' && P2 == 'S') || (P1 == 'S' && P2 == 'P');
}
//****************************************************************************************
bool islowerWinner(char P1, char P2) 
{
   return (P1 == 'p' && P2 == 'r') || (P1 == 'r' && P2 == 's') || (P1 == 's' && P2 == 'p');
}
//********************************************************************************************
int lower_winner (char P1, char P2)
{

	if (P1 == 'q' || P2 == 'q') { return 4; }
	if (P1 == P2) { return 3; }
    if (islowerWinner(P1, P2)) { return 1; }
    return 2;
	
}
//********************************************************************************************


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1