Thread: please help about switch

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    3

    please help about switch

    /*Program code for reference only*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    int main(void)
    {
    int play1,play2;
    int rand1;
    srand(time(NULL)); /*generate a random seed*/
    rand1=rand()%3+1;/*generate a random number range from 1 to3*/


    printf("\n\nPlayer 1 - Enter your choice?");
    scanf("%d",&play1);

    system("cls");

    printf("\n\nPlayer 2 - Enter your choice?");
    scanf("%d",&play2);

    system("cls");

    switch(play1)
    {
    case'1':
    printf("\nPlayer 1 : Paper\n\n");
    break;
    case'2':
    printf("\nPlayer 1 : Scissors\n\n");
    break;
    case'3':
    printf("\nPlayer 1 : Stone\n\n");
    break;
    default:
    printf("-");
    }
    switch(play2)
    {
    case'1':
    printf("\nPlayer 2 : Paper\n\n");
    break;
    case'2':
    printf("\nPlayer 2 : Scissors\n\n");
    break;
    case'3':
    printf("\nPlayer 2 : Stone\n\n");
    break;
    default:
    printf("-");
    }


    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    why i can't show out what input of player 1 & 2
    this is a part of my "c". please help

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    1) because you didn't use code tags
    2) because it's ROCK, paper, scissors
    3) because play1 & play2 are declared as ints yet they are compared to char's
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    '1' is NOT the same as 1. '1' is a char, 1 is an integer.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >'1' is a char, 1 is an integer.

    No, '1' is an int; but it's value is not likely to be 1.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Dave_Sinkula
    >'1' is a char, 1 is an integer.

    No, '1' is an int; but it's value is not likely to be 1.
    no, '1' is a char that is represented internaly with binary numbers and may be cast as an integer, however it's ASCII value is not 1.....if ya wanna get technical
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Again, no.
    An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'.
    An integer character constant has type int.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i supose i will concede to that
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    int main(int argc, char *argv[]){
    	printf("%d\n", sizeof('a'));
    	return 0;
    }
    OUTPUT using MSVC++ 6:
    1
    OUTPUT using GCC in DJGPP:
    4

    The size of a constant char depends on the compiler implementation, but if its size is smaller that the variable to which is assigned, the leftover bytes are filled with 0's.
    Me thinks....

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I believe this is because the type of a character literal depends on the language -- C (int) or C++ (char).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The size of a constant char depends on the compiler implementation
    It depends on the compiler (language) as mentioned by Dave
    http://david.tribble.com/text/cdiffs...9-char-literal
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM