Thread: Guess What...??

  1. #31
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Lord Slytherin View Post
    Code:
    void main()
    Should be int main( void ), I'm sure someone's probably mentioned this by now to you.
    Quote Originally Posted by Lord Slytherin View Post
    Code:
    gets(guess);
    There's a FAQ (and probably a compiler warning, though since you use conio.h probably not in your case) about using something other than gets.
    Quote Originally Posted by Lord Slytherin View Post
    Code:
    guess[15] = (strcmp(guess,"JAPAN") == 0) ? 0:1;
    I don't know why you are doing this, but 15 is past the end of your array, so it's only pure luck that this actually "works" for you.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #32
    Registered User
    Join Date
    Apr 2011
    Location
    Karachi, Pakistan
    Posts
    19
    OK then see this:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int main(void)
    {
     char guess[15];
     int score;
     clrscr();
     printf("\"LET'S START\"\nPlease turn on your Caps Lock.\n\n");
      {
      score=0;
      printf("(1) Which country is called \"The Land of The Risisng Sun\" ?\n");
      gets(guess);
      guess[14]=(strcmp(guess,"JAPAN") == 0) ? 0:1;
      switch ( guess[14] )
    	 {
    	  case 0:
    		printf("That's corrrrrrect\n");
    		printf("Your score is %d.",score+4);
    		break;
    	  case 1:
    		 printf("Wrong Answer\nTry again\n");
    		 gets(guess);
    		 if (strcmp(guess,"JAPAN")==0)
    		 {
    		  printf("Correct\nYour score is %d.",score+2);
    		  break;
    		 }
    		 else
    		 printf("Wrong again\nTry once more\n");
    		 gets(guess);
    		 strcmp(guess,"JAPAN");
    		 if(strcmp(guess,"JAPAN")==0)
    		 {
    		  printf("\nright this time\nscore=%d",score+1);
    		  break;
    		 }
    	  default:
    		printf("SORRY You LOST!\nThe answer is \"JAPAN\".");
    		printf("Your score is %d.",score);
    		break;
    	 }
      printf("\n\n(2) In Which country does Mount Fuji locates?\n");
      gets(guess);
      guess[13]=(strcmp(guess,"JAPAN") == 0) ? 0:1;
      switch( guess[13] )
    	{
    	 case 0:
    		printf("\nCorrrrrect!\nscore=%d",score+4);
    		break;
    	 case 1:
    		printf("You're wrong\nTry again\n");
    		gets(guess);
    		if(strcmp(guess,"JAPAN") == 0)
    		{
    		 printf("That's Right!\nscore=%d",score+2);
    		 break;
    		}
    		else
    		printf("Wrong again.\nTry again");
    		gets(guess);
    		if(strcmp(guess,"JAPAN")==0)
    		{
    		 printf("Right this time.\nscore=%d",score+1);
    		 break;
    		}
    	 default:
    		printf("SORRY you lost!\n\"JAPAN\" is the answer.\n");
    		printf("score=%d",score);
    		break;
    	}
      }
     getch();
    }

  3. #33
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you reusing pieces of your array to temporarily store strcmp results?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #34
    Registered User
    Join Date
    Apr 2011
    Location
    Karachi, Pakistan
    Posts
    19
    Quote Originally Posted by quzah View Post
    Why are you reusing pieces of your array to temporarily store strcmp results?


    Quzah.
    what else should've been done then...??
    I just know as much that I must give 'guess' some limit or else its a compilation error.
    like
    Code:
    guess[14]=(strcmp(guess,"JAPAN") == 0) ? 0:1;
    OR

    Code:
    guess[13]=(strcmp(guess,"JAPAN") == 0) ? 0:1;
    it doesn't accept:
    Code:
    guess = (strcmp(guess,"JAPAN") == 0) ? 0:1;
    Its working fine either way, except for when I dont give ne LIMIT.
    But the actual problem is that 'score' does not retain its value when coming on to the next question.
    Please suggest reason for 'score' malfunctioning or whatever the best you think.

    P.S. I am fearful of cross-questioning.

  5. #35
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try another int variable, like

    Code:
    int answer;
    answer = (strcmp(guess,"JAPAN") == 0) ? 0:1;
    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. can u guess my number?
    By hotsox in forum C Programming
    Replies: 2
    Last Post: 01-07-2006, 01:40 PM
  2. can u guess my number?
    By strider496 in forum C Programming
    Replies: 16
    Last Post: 03-22-2005, 10:19 PM