Like Tree1Likes
  • 1 Post By quzah

[newbie] cant leave the program!

This is a discussion on [newbie] cant leave the program! within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<math.h> #include<time.h> #include<stdlib.h> int fnCompare(int luckyNum,int inNum) { int i; if ((inNum <1) || (inNum > 100)) {i ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Question [newbie] cant leave the program!

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<time.h>
    #include<stdlib.h>
    
    
    int fnCompare(int luckyNum,int inNum)
        {
        int i;
        if ((inNum <1) || (inNum > 100))
        {i =1;}
        else if (luckyNum > inNum)
        {i =2;}
        else if (luckyNum < inNum) 
        {i =3;}
        else  
        {i =4;}
        return(i);
    }
    
    
    int fnLuckyNum()
        {
        int value;
        srand(time(NULL));
        value= 1 + (int) rand()%100;
        return (value);
    }
    
    
    int main()
    {
     
        int luckyNum,input,temp,count,bestScore;
        int boolean;
        char choice,enterKey;
    
    
        
        
        bestScore=100;
        choice='y';
        fflush(stdin);
        do
        {
         luckyNum=fnLuckyNum();
         count=1;
         {input =1;
          while ((input>0)&&(input<=100))
          {
              printf("\nGuess what is the lucky number< range 1 to 100>: ");
              scanf("%d",&input);
              printf("\n");
              temp=fnCompare(luckyNum,input);
              switch (temp)
              {
                     case 1:
                          {printf("The program will be quit...");break;}
                     case 2:
                          {printf("   Too small! Guess again < or -ve number to quit>.\n\n");count++;break;}
                     case 3:
                          {printf("   Too large! Guess again < or -ve number to quit>.\n\n");count++;break;}
                     case 4:
                          {printf("---Lucky! Right. The lucky number is <%d>. \n",luckyNum);
                          printf("---Number of attempts [%d]\n",count);
                          input=-1;
                          if (count<bestScore)
                             bestScore=count;
                          break;}
              }
          }
         }
         printf("\nBest Score so far <%d>.\n",bestScore);
         printf("\nPlay again? Type 'y' for YES [all other characters for No] : ");
         scanf("%c%c",&choice,&enterKey);
         printf("%c",choice);
        } while (choice != 'y');   
        getchar();
        return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ask Questions The Smart Way (click!) Or even the wrong way at this point, since you didn't actually ask anything...


    Quzah.
    CommonTater likes this.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    First off, fflush(stdin) is undefined behavior. Secondly, you have many newlines still in the buffer after your while loop; read: How to get my program to wait for a keypress-FAQ. Pay particular attention to flushing the input stream.

    Now, your implementation defined fflush() is in the wrong place for your loop based on what you want it to do.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Pay close attention to the spacing...
    Code:
         scanf(" %c",&choice);
        } while (toupper(choice) != 'Y');   
        return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enter to Continue, Esc to leave
    By julianenepom in forum C Programming
    Replies: 9
    Last Post: 10-08-2010, 05:47 AM
  2. error won't leave
    By Yarin in forum C++ Programming
    Replies: 5
    Last Post: 10-16-2007, 04:04 PM
  3. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 06:44 PM
  4. Gah.... leave me be......
    By Fordy in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-15-2001, 08:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21