Well this program is suppose to not accept anything but s, S, r, R, p, P, or q, Q. When some one enters anything whether it be those or somethign different the while loop continues (it won't let me escape)
Well any ways here it is.

#include <iostream>
#include <string>
#include <istream>
using namespace std;

int main ()
{
	char player1, player2;
cout << "Welcome to Rock Paper Sissors. Please select r for rock"
	 << " p for paper, and s for sissors. If you would like to quit"
	 << " please press q now.";
	 cin >> player1;
	 
	 while ((player1!='r')||(player1!='R')		 
		 || (player1!='p') || (player1!='P')
		 || (player1!='s') || (player1!='S')
		 || (player1!='q') || (player1!='Q'))

	 {
	 cout << "Wrong input ";
	 cin >> player1;
	 }
	 if ((player1=='q') || (player1=='Q'))
	 
		cout << "You selected quit, Good buy";
		return 1;
	 
	  	
cout << "Player two please select your type ";
cin >> player2;
	 
if (player1=='r' && player2=='p')
	{
	cout << "Rock beats Paper";
	}
	return 0;
}

Recommended Answers

All 9 Replies

Check those loop conditions...
They're not checking for what you think they are.

replace:

while ((player1!='r')||(player1!='R')		 
		 || (player1!='p') || (player1!='P')
		 || (player1!='s') || (player1!='S')
		 || (player1!='q') || (player1!='Q'))

with

while ((player1!='r')&&(player1!='R')		 
		 && (player1!='p') && (player1!='P')
		 && (player1!='s') && (player1!='S')
		 && (player1!='q') && (player1!='Q'))

Hmm I did that, but now it always says I am pressing Q or q. SO it quits me

post your new code

post your new code

Okay I fixed it. But I get a huge error now

// Craig Truzzi Vantha Doun
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main ()
{
	char player1, player2;
	cout << "Welcome to Rock Paper Sissors. Please select r for rock"
		 << " p for paper, and s for sissors. If you would like to quit"
		 << " please press q now.";
	cin >> player1;
	player1=toupper(player1);
	{ 
	 		while ((player1!='R') && (player1!='P')
				  && (player1!='S') && (player1!='Q'))
		{
		cout << "Wrong input ";
		cin >> player1;
		system ("cls");
		}
		if ((player1=='q') && (player1=='Q'))
		{
			cout << "You selected quit, Good buy";
			return 1;
		}  
	}

	cout << "Player two please select your type ";
	cin >> player2;
	player2=toupper(player2);
	{
	 		while ((player2!='R') && (player2!='P')
				&& (player2!='S') && (player2!='Q'))			
		{
		cout << "Wrong input ";
		cin >> player2;
		system ("cls");
		}
	}
if (player1=='r' && player2=='p')
	
{
	cout << "Rock beats Paper";
	}
	return 0;
}

It seems that we got it to work. But we where having an internal error. I had to save it to my C drive instead of H and it worked perfectly. Any idea on what a internal error is?

The reason why your loop isn't working is due to the fact that you dont have a break statement if the condition has been finally fullfilled in the loop. That is the problem about while loops like yours. When you ask for a reentry for the failed cin, place an if statement like this:

if((blah,blah,blah,)
break;

This should work, I tried it on your program. Hope it works out! PS try using case, it works a lot better for a short program like this. Good Luck/


Muthu

Well this program is suppose to not accept anything but s, S, r, R, p, P, or q, Q. When some one enters anything whether it be those or somethign different the while loop continues (it won't let me escape)
Well any ways here it is.

#include <iostream>
#include <string>
#include <istream>
using namespace std;

int main ()
{
	char player1, player2;
cout << "Welcome to Rock Paper Sissors. Please select r for rock"
	 << " p for paper, and s for sissors. If you would like to quit"
	 << " please press q now.";
	 cin >> player1;
	 
	 while ((player1!='r')||(player1!='R')		 
		 || (player1!='p') || (player1!='P')
		 || (player1!='s') || (player1!='S')
		 || (player1!='q') || (player1!='Q'))

	 {
	 cout << "Wrong input ";
	 cin >> player1;
	 }
	 if ((player1=='q') || (player1=='Q'))
	 
		cout << "You selected quit, Good buy";
		return 1;
	 
	  	
cout << "Player two please select your type ";
cin >> player2;
	 
if (player1=='r' && player2=='p')
	{
	cout << "Rock beats Paper";
	}
	return 0;
}

Well we have learned it's bad coding to have breaks in while statements. It worked fine in the end, but we had some internal error that didn't let it work until we switched computers.

I like case statements myself, god luck with the computer, thats kind of bizarre.

AM

Well we have learned it's bad coding to have breaks in while statements. It worked fine in the end, but we had some internal error that didn't let it work until we switched computers.

break statements are no problem, it's labelled break statements that are asking for trouble.
And of course you should always know what you're doing (in one program I do use labelled breaks, because in that specific situation it's the cleanest solution I could come up with, everything else requiring a mess of nested if statements to prevent further execution under specific conditions).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.