Thread: Need help with "if" function

  1. #16
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Is there a specific part of it you don't understand? You've already used or had explained everything in this code, as far as I can tell.

    Not having to press enter after entering the character isn't very simple. There are simple ways provided by a lot of compilers let getch() in conio.h, but that isn't standard.

  2. #17
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    for function u need () and read abt function return and parameters etc anyways here a code it will return 1 if num is 13 then we can l8er chk if its return is 1 on right and 0 on wrong.
    Code:
    #include <stdio.h>
    int int_guesser()
    {
        int num;
        puts("Guess a num : ");
        scanf("%d",&num);
        if(num==13)
            return 1;
        else
            return 0;
    }
    int main()
    {
        int num;
        num=int_guesser();
        if(num)
            puts("gratz u guessed right");
        else
            puts("Wrong guess try again");
        
      return 0;
    }
    ;.

  3. #18
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    In order to make you not have to press <enter> after entering a char, you would have to look outside the ANSI C library, and into the POSIX C library, I believe. Or just use getch()/getche(). getche() works on the gcc supplied with Dev-C++, getch() on other compilers I believe. Not certain, it's been a while since I did interactive console programming

    I'm sorry, I really should have explained in greater depth what I was trying to do. I usually program without comments for programs < 250 lines.

    Here's a nice breakdown of how it all fits together . . .

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    The includes. stdio.h for printf(), stdlib.h for srand() and rand(), time.h for time(), and string.h for tolower().

    Code:
    int main(int argc, char *argv[]) {
        int i, num, tries;
        char cont;
    Main function declaration and variable declarations.

    Code:
        srand(time(NULL));
    Seed the random number generator with the current UNIX time in seconds.

    Code:
        
        do {
    I'm assuming you've run into do...while loops before. If not, well . . . a do...while loop is much like a while() loop, but instead, do...while loops check the condition at the bottom of the loop rather than the top. This means that they will always run at least once.

    Code:
            num = (rand()%100)+1;
    Set the random number to guess for this game. Since rand()%100 returns a number in the range 0-99, you want to add 1 to it to get 1-100.

    Code:
            i = -1;
            tries = 0;
    Initialize the variables for the game. i is the entered number, which is set to -1 to indicate that no number has been entered yet. tries is the amount of tries that the player has gone through in order to guess the number, zero at the first round.

    Code:
            while(num != i) {
    Loop until the entered number (i) is the same as the randomly-generated number (num).

    Code:
                if(i != -1) {
    If this is not the first round . . .

    Code:
                    if(i > num) {
                        printf("Too high!\n");
                    }
                    else printf("Too low!\n");
    Compare i to num. Since i will never be equal to num because of the while() loop condition, simply print "Too high!" if the number entered was > the randomly-generated number, else print "Too low!".

    Code:
                }
                printf("Enter a number between 1 and 100: ");
                scanf("%i", &i);
    Finish off the if(i !=-1) if block first. Then, print a prompt for the player and retrieve the entered number into the variable i.

    Code:
                tries ++;
    Increment the number of tries.

    Code:
            }
    Finish off the while(i != num) loop. If it gets past this point, then the number entered was the same as the random number, so . . .

    Code:
            printf("Got it in %i %s. Again? [yY/nN] ", tries, tries == 1?"try":"tries");
    This is a little bit of fancy footwork. The %i should be easy enough. The %s, on the other hand . . . Suffice to say that if the condition (tries == 1) is true, then it will print "try", otherwise it'll print "tries". (1 tries doesn't sound too good ).

    Code:
            while((cont = tolower(getchar())) && cont != 'y' && cont != 'n') ;
    Again, some fancy footwork. This loop will continue going around until the keyboard buffer is cleared, and until the player enters 'y' or 'n', in either case. The function tolower() converts an uppercase char into a lowercase char, or leaves it alone if it's either a non-alpha char or already lowercase.

    Code:
        } while(cont == 'y');
    aaand continue the do...while loop until the user enters 'n', to indicate that they don't want to try playing again.

    Code:
        return 0;
    }
    Finish up and return 0.

    Hope this helps a tad.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM