C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 28th, 2012, 09:58 AM
archael07 archael07 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 15 archael07 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 31 sec
Reputation Power: 0
Question An activity that i cant solve

This code runs, but i can't have the second switch statement to work and i can't input anything for player 2...this is a rock-paper-scissor program using switch/case





#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream>
#include<string.h>
main()
{
char x,y;
printf("This is a Rock-Paper-Scissor game\nPlease enter \n[R] for Rock\n[P] for Paper\n[S] for Scissor");
printf("\nEnter letter for Player 1: ");
scanf("%c",&x);
printf("\nEnter letter for Player 2: ");
scanf("%c",&y);
switch(x)
{

case 'r':case 'R':
switch(y)
{
case 'r':case 'R':
printf("Draw");break;
case 'p':case 'P':
printf("Player 2 wins");break;
case 's':case 'S':
printf("Player 1 wins");break;
default:
printf("Invalid input");break;
}break;
case 'p':case 'P':
switch(y)
{
case 'p':case 'P':
printf("Draw");break;
case 's':case 'S':
printf("Player 2 wins");break;
case 'r':case 'R':
printf("Player 1 wins");break;
default:
printf("Invalid input");break;
}break;
case 's':case 'S':
switch(y)
{
case 's':case 'S':
printf("Draw");break;
case 'r':case 'R':
printf("Player 2 wins");break;
case 'p':case 'P':
printf("Player 1 wins");break;
default:
printf("Invalid input");break;
}break;
default:
printf("INVALID INPUT");
}
getch();
}

Reply With Quote
  #2  
Old November 28th, 2012, 10:39 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,254 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 46 m 26 sec
Reputation Power: 1985
[QUOTE=archael07]Rule One: Use code tags! If you don't, which you didn't, then your code loses all its formatting and becomes an unreadable mess. If we can't read it, we won't bother and you won't get any help.

Here's what your code looks like when you use code tags:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream>
#include<string.h>
main()
{
      char x,y;
      printf("This is a Rock-Paper-Scissor game\nPlease enter \n[R] for Rock\n[P] for Paper\n[S] for Scissor");
      printf("\nEnter letter for Player 1: ");
      scanf("%c",&x);
      printf("\nEnter letter for Player 2: ");
      scanf("%c",&y);
      switch(x)
          {
                          
                    case 'r':case 'R': 
                             switch(y)
                             {
                             case 'r':case 'R':
                             printf("Draw");break;                     
                             case 'p':case 'P':
                             printf("Player 2 wins");break;
                             case 's':case 'S':
                             printf("Player 1 wins");break;
                             default:
                             printf("Invalid input");break;
                             }break;
                    case 'p':case 'P':        
                    switch(y)
                             {
                             case 'p':case 'P':
                             printf("Draw");break;                     
                             case 's':case 'S':
                             printf("Player 2 wins");break;
                             case 'r':case 'R':
                             printf("Player 1 wins");break;
                             default:
                             printf("Invalid input");break;
                             }break;
                    case 's':case 'S':
                             switch(y)
                             {
                             case 's':case 'S':
                             printf("Draw");break;                     
                             case 'r':case 'R':
                             printf("Player 2 wins");break;
                             case 'p':case 'P':
                             printf("Player 1 wins");break;
                             default:
                             printf("Invalid input");break;
                             }break;      
                    default:
                             printf("INVALID INPUT");              
          }
          getch();
}


Rule Two: Use proper indenting! I've seen a lot worse, but your style still makes your code hard to read, what with that excessive indenting where it's not needed and lack of indenting where it is needed. And hiding close-braces is never a good idea.

Though I didn't have to try to read your code to spot the problem:
scanf("%c",&y);
I'm sure there must be an entry for this in the "commonly asked questions" thread, but the problem is that you are telling scanf to read in the next character no matter what kind of character it is. That means that the ENTER placed in the input buffer by the previous scanf is what got placed into y.

ENTER is one of several characters that are called white space, some of the others being space and tab. What you want the program to do is to read in the next non-white-space character, meaning that you want scanf to ignore all intervening white space. Here is how you do that:
scanf(" %c",&y);
Please note the space added before the %. That is what tells scanf to ignore all white space that precedes the character that you actually want it to read.

I can't speak for any of the rest of your barely readable code, except for the main function header. By not declaring what datatype it returns, you're forcing it to default to int, but then you completely fail to return that int that you had promised it. Keep your promises! Declaring it as void isn't even right, even though you can get away with doing it wrong.

Declare main as:
int main()
and make the last line in the main() function:
return 0;
That tells the operating system that your program ran successfully. If it had not for some reason (eg, wrong number of arguments passed in, unable to open a file, division by zero attempted), then you would return a non-zero value to indicate failure. Other programs can make use of that information, as can shell scripts, so returning it is a requirement for all well-behaved programs.
Comments on this post
archael07 agrees!

Reply With Quote
  #3  
Old November 28th, 2012, 03:49 PM
archael07 archael07 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 15 archael07 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 31 sec
Reputation Power: 0
THANKS!!! the space before the % works!!

though my bad for the format i was half-asleep when i'm doing that hahha and i did not know about the code tags
here is what it looks like know, and working!!!

Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream>
#include<string.h>
int main()
{
      char x,y;
      printf("This is a Rock-Paper-Scissor game\nPlease enter \n[R] for Rock\n[P] for Paper\n[S] for Scissor");
      printf("\nEnter letter for Player 1: ");
      scanf("%c",&x);
      printf("\nEnter letter for Player 2: ");
      scanf(" %c",&y);
      switch(x)
          {                          
                    case 'r':case 'R': 
                             switch(y)
                             {
                             case 'r':case 'R':
                             printf("\nDraw");break;                     
                             case 'p':case 'P':
                             printf("\nPlayer 2 wins");break;
                             case 's':case 'S':
                             printf("\nPlayer 1 wins");break;
                             default:
                             printf("\nInvalid input");break;
                             }
                             break;
                    case 'p':case 'P':        
                    switch(y)
                             {
                             case 'p':case 'P':
                             printf("\nDraw");break;                     
                             case 's':case 'S':
                             printf("\nPlayer 2 wins");break;
                             case 'r':case 'R':
                             printf("\nPlayer 1 wins");break;
                             default:
                             printf("\nInvalid input");break;
                             }
                             break;
                    case 's':case 'S':
                             switch(y)
                             {
                             case 's':case 'S':
                             printf("\nDraw");break;                     
                             case 'r':case 'R':
                             printf("\nPlayer 2 wins");break;
                             case 'p':case 'P':
                             printf("\nPlayer 1 wins");break;
                             default:
                             printf("\nInvalid input");break;
                             }
                             break;      
                    default:
                             printf("\nINVALID INPUT");              
          }
          getch();
          return 0;
}

Reply With Quote
  #4  
Old November 28th, 2012, 04:16 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
Your problem can be solved thus:

Code:
char input_char()
{
    char x = getchar();
    while( x != '\n' && getchar() != '\n' )
    {
        // empty - flushing the line input
    }
    return x ;
}


and then replace your scanf() calls with input_char() calls.

There is also a much simpler algorithm to determine the winner:

Given a function to convert an input of r, p or s to 0,1 or 2 respectively, for example thus:
Code:
int get_rps_index( char ch )
{
    const char* rps = "rps" ;
    char* p = strchr( rps, tolower(ch) ) ;
    return p != 0 ? p - rps : -1 ;
}


You can then determine the winner thus:

Code:
int xi = get_rps_index( x ) ;
int yi = get_rps_index( y ) ;

if( yi > 0 && yi > 0 )
{
    if( yi > xi )
    {
        yi += 3 ;
    }

    switch( yi - xi )
    {
        case 0:
            // Draw
        break ;

        case 1:
            // Player 2 (y) wins
        break ;

        case 2:
            // Player 1 (x) wins
        break ;
    }
}
else
{
    // invalid input
}

Last edited by clifford : November 28th, 2012 at 04:32 PM.

Reply With Quote
  #5  
Old November 28th, 2012, 04:26 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
Quote:
Originally Posted by archael07
THANKS!!! the space before the % works!!

Only until the user mis-keys and enters: r#<enter> or similar - the program will not wait for player two input which will take the # and be invalid input. Equally if you typed rp<enter>, it would not wait for new input but this time player 2 will win.

You might find that input method acceptable, but it would normally be better to ensure that the program always stops and waits for input for player 2 and disregards any input after the first character entered - the method I suggested does that.

Last edited by clifford : November 28th, 2012 at 04:29 PM.

Reply With Quote
  #6  
Old November 30th, 2012, 04:48 AM
archael07 archael07 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 15 archael07 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 31 sec
Reputation Power: 0
yours is way cooler than mine!! unfortunately we are not that far in the lesson in school yet, so am a little blurry on what you said or how to do what you said...if you would be so kind as to explain further?? esp the one where it will only accept one input as required. We are going to create a program where user must input 1 character only,,have tried arrays but am messing up the code..hehehe MANY THANKS!!!

Reply With Quote
  #7  
Old November 30th, 2012, 08:36 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,254 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 46 m 26 sec
Reputation Power: 1985
Rather, Clifford made you aware of a far greater problem and concern. An older, more curmudgeonly member, DaWei, would put it thus: "I could easily make your program run out to the weeds and puke all over its shoes."

Right now you are learning the bare essentials of how to get input from the user and to process it and to provide output. Simple tasks, extremely simple tasks. Not really what you will need to do in the real world.

In the real world, your software needs to be able to handle any kind of input from the user and to keep running reliably no matter what happens. That's called being "robust". That involves input validation. That means that your program should be expected to receive any kind of input from the user, validate it, and if it is not correct to either ask the user to input it correctly or allow him to exit gracefully.

Clifford was trying to introduce you to that extremely important idea. Your first professional project outside of school will be totally unlike what you had been doing in school. School is only getting you ready to learn what you will really be doing. Don't let this discourage you! It is fun and exciting and there's so much more to learn!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > An activity that i cant solve

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap