Thread: Mini Game

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    I have managed to compile the code correctly, however I would like to make the program to instead ask the user again to enter a number rather than close. How should I go around this?

    By the way, sorry if it sounds stupid but what does WRT mean?
    Last edited by bonett09; 12-27-2011 at 01:34 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bonett09 View Post
    I have managed to compile the code correctly, however I would like to make the program to instead ask the user again to enter a number rather than close. How should I go around this?
    Use a while(condition). Eg, if you initialize numofbees to 0, the condition could be while(numofbees <= 0).

    By the way, sorry if it sounds stupid but what does WRT mean?
    With Regard To. You can find these if you google "acronym WRT" or whatever, I have to do it pretty often too; recently, "TLA"...

    Quote Originally Posted by acronymfinder.com
    TLA: Three Letter Acronym
    O_O
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    Is this the correct placement for the while loop? If so, I have a problem that when I enter a letter an infinite loop prints out 'How many bees would you like to purchase?' and on the other hand if I enter a number an 'Invalid amount...' prints out everytime.

    Examples:
    How many bees would you like to purchase?
    5
    Invalid amount, please try again.
    That will be $25, confirm? (Y/N)


    OR


    How many bees would you like to purchase?
    0
    Invalid amount, please try again.
    How many bees would you like to purchase?
    5
    Invalid amount, please try again.
    That will be $25, confirm? (Y/N)

    Code:
    #include<stdio.h>
    
    
    int main()
    {
        int numofbees = 0;
        float price = 5;
        char query;
    
    
        while(numofbees <= 0){
        printf("How many bees would you like to purchase?\n");
             (!scanf("%u", &numofbees));
        printf("Invalid amount, please try again.\n");
        }
    
    
        printf("That will be $%f, confirm? (Y/N)\n", price*numofbees);
            scanf(" %c", &query);
    
    
            if(query == 'y'|| query == 'Y') {
                printf("Transaction completed.");
        }
            else {
                printf("Transaction cancelled.");
        }
            getchar();
            getchar();
            return 0;
        }
    Last edited by bonett09; 12-29-2011 at 03:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Critique my mini-AES
    By Hughesz93 in forum C Programming
    Replies: 7
    Last Post: 05-04-2011, 03:49 PM
  2. need some help to upgrade a mini game in C
    By Exutus in forum C Programming
    Replies: 2
    Last Post: 11-23-2009, 12:10 AM
  3. Mini If
    By Milhas in forum C Programming
    Replies: 4
    Last Post: 03-27-2008, 04:04 PM
  4. Mini-screens
    By pianorain in forum Tech Board
    Replies: 2
    Last Post: 07-25-2004, 11:21 PM
  5. mini-itx
    By whistlenm1 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-18-2003, 03:58 PM