Jump to content



Photo

how do i get this to continue until esc is pressed?


  • Please log in to reply
8 replies to this topic

#1 Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 06 October 2013 - 20:59

Okay everyone.  I have run into another bit of trouble.  I have the code, and while it is working...its only doing one repetition.  Even after I press one of the options R, P, or S (rock, paper, scissors) and then press ESC it will not quit.  Here is what I have and underneath that is the printout of what it is doing.  Any ideas would be great.  I tried using a continue statement, but it's not working with that either. 

/* 
Rock, Paper, Scissors
Asgn4-1 Ross
2013
CISP360
*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h> //this is where rand is located
#include <time.h> // contains the time structure
#pragma warning (disable:4996) //stops the warnings for scanf

void main()
{
	char ch1; //user input
	char ch2; //puter input
	char chok; //flag
	time_t t; //time structure


	//seed rand # generator
	srand((unsigned) time(&t));

	printf("Let's play Rock, Paper, Scissors!\n May the best player win!\n"); //ouputs to the screen
	printf("Enter R, P, or S.  Press ESC to end the game.\n\n\n"); //outputs instructions to the game
	printf("Player		Computer		Winner\n");
	printf("------------------------------------\n");

	//main loop
	do{
		//gets player's move
		ch1 = getch();
		switch(ch1)
		{
		case 'R':
		case 'r':
		case 'P':
		case 'p':
		case 'S':
		case 's':
		case 27: //ESC
			chok = 1;
			break;
		default:
			chok = 0;
		}
	}while(!chok); //input validation loop
	//convert to upper case
	if(ch1 > 'Z') ch1 -= ' '; 
	/*
	ASCII value for a space (' ') is 32. 
	Integer representations of lower case chars are 32 higher than those of their equivalent upper case chars. 
	So if you subtract 32 from a lower case char it will become an upper case char.
	*/

	//gets computer's move
	ch2 = rand() % 3; // code in C to get a random #
	switch(ch2)
	{
	case 0:
		ch2 = 'R';
		break;
	case 1:
		ch2 = 'P';
		break;
	case 2:
		ch2 = 'S';
		break;
	}

	//checks who won
	if(ch1 == 'R' && ch2 == 'R')
		printf("Rock		Rock		Draw\n");
	if(ch1 == 'R' && ch2 == 'P')
		printf("Rock		Paper		Computer\n");
	if(ch1 == 'R' && ch2 == 'S')
		printf("Rock		Scissors		Player\n");
	if(ch1 == 'P' && ch2 == 'R')
		printf("Paper		Rock		Player\n");
	if(ch1 == 'P' && ch2 == 'P')
		printf("Paper		Paper		Draw\n");
	if(ch1 == 'P' && ch2 == 'S')
		printf("Paper		Scissors		Computer\n");
	if(ch1 == 'S' && ch2 == 'R')
		printf("Scissors		Rock		Computer\n");
	if(ch1 == 'S' && ch2 == 'P')
		printf("Scissors		Paper		Player\n");
	if(ch1 == 'S' && ch2 == 'S')
		printf("Scissors		Scissors		Draw\n");
	
		while(ch1 != 27);
	
}














and here is what it is doing:

 

Let's play Rock, Paper, Scissors!
 May the best player win!
Enter R, P, or S.  Press ESC to end the game.

Player          Computer                Winner
------------------------------------
Paper           Paper           Draw

 




#2 abecedarian paradoxious

abecedarian paradoxious

    cell tech

  • Tech Issues Solved: 1
  • Joined: 04-November 07

Posted 06 October 2013 - 21:15

- nevermind, misread.



#3 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 9
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 06 October 2013 - 22:16

Okay, so your bug is in the very last line. See this line?

while(ch1 != 27);
This line is the equivalent to:

while (ch1 != 27)
{
    // Do nothing.
}
This means that if you choose a character that isn't ESC, then you'll just get stuck in an infinite loop, because the variable "ch1" will never be 27!

I think what you meant to do was put all the code between the main loop and the results printing into it's own do...while loop.

Hope this helps :)

#4 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 06 October 2013 - 22:27

okay I see what you mean, but when I do

}while(ch1 != 27)

continue;

{

 

it won't accept it.  it keeps telling me that it expects a declaration.  am I just missing something?



#5 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 9
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 06 October 2013 - 22:29

Where are you putting your "do {" section?

#6 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 06 October 2013 - 22:31

around the main loop with the switch and the cases.  why?



#7 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 9
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 06 October 2013 - 22:39

You didn't include it, that's all :)

You should just be able to remove the continue and "{". The do...while loop doesn't need it. Typically a do...while loop looks like this:

do {
    // Do stuff!
} while (something != something_else);
The "continue; {" you posted isn't required :)

#8 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 06 October 2013 - 22:49

I have that setup already.. 

do{
		//gets player's move
		ch1 = getch();
		switch(ch1)
		{
		case 'R':
		case 'r':
		case 'P':
		case 'p':
		case 'S':
		case 's':
		case 27: //ESC
			chok = 1;
			break;
		default:
			chok = 0;
		}
	}while(!chok); //input validation loop

do I need another do?



#9 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 9
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 06 October 2013 - 23:21

Yes. You're going to need two loops:
  • One that ensures that the user enters a valid character (you already have this)...
  • ... and another one to repeat the whole process until the user presses the escape key.
Remember that every time you open a loop, you also need to close it. Take the following examples:

Example #1: A valid For loop
for (i = 0; i < 10; i++)  // <-- Loop opened!
{
    printf("%d", i);
}                         // <-- Loop closed!
Example #2: A valid do...while loop


int i = 0;
do                        // <-- Loop opened!
{
    i += 1;
} while (i < 10)          // <-- Loop closed!
Example #3: An invalid do...while loop


int i = 0;
int m = 0;

do {                      // <-- Loop #1 Opened!
    i += 1;
} while (i < 10)          // <-- Loop #1 Closed!

    m += 1;
} while (m < 100)         // <-- Loop #2 Closed (but not opened?!?!?!?!)
To fix example #3, you'd need an additional "do {" section to start the outer loop. Every "do...while" loop requires both a "do" part and a "while" part. If you've got two loops (as you will need), you need two "do" parts and two "while" parts.

That help?

Remember, indentation helps. If you indent properly, it should be fairly obvious when you need to open and close your loops. If you've got an indentation without a curly bracket ("{"), you've probably not done something right. Every time you open a curly bracket, the lines underneath should be indented. That way, you can easily see where a loop begins and ends. For example:

do
{                      // <-- Indent after this line!
    do                 // <-- This line is inside the loop. We can see this because it is indented!
    {                  // <-- Indent AGAIN after this line!
        // Do stuff.
    }
    while (w != x);    // <-- Loop ends, reduce your indentation!

    // Do more stuff.
}
while (y != z);        // <-- Outer loop ends, reduce your indentation again!

// Finish doing stuff!