Like Tree1Likes

Mini Game

This is a discussion on Mini Game within the C Programming forums, part of the General Programming Boards category; Insult completely taken to heart, we shall now be mortal enemies!!! Not really, like I said though, meant no insult, ...

  1. #16
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Insult completely taken to heart, we shall now be mortal enemies!!!

    Not really, like I said though, meant no insult, it's just my OCD acting up. If I wasn't so tired I'd be trying to fix the not-so-straight lines in my flowchart attached. I couldn't find a decent flowchart maker for linux yet, so I had to use OODraw, it did alright... I guess.

    Anyway, I have to clean now. See you guys in a bit. Hopefully OP has found some kind of solution?
    Attached Images Attached Images  

  2. #17
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    Thanks for the feedback guys. Following the flowchart, I have encountered an error when trying to reject a letter/character. I am not sure whether 'char' is actually equivalent to every letter but I tried it nonetheless.The error is:

    In function 'int main()':
    12:19: error: expected primary-expression before 'char'
    12:19: error: expected ')' before 'char'

    Code:
    #include<stdio.h>
    
    
    int main()
    {
    	int numofbees;
    	float price = 5;
    	char query;
    
    
    	printf("How many bees would you like to purchase?\n");
    		scanf("%d", &numofbees);
    
    
    		if(numofbees == char){
    			printf("Invalid amount, please try again.\n");
    			printf("How many bees would you like to purchase?\n");
    				scanf("%d", &numofbees);
    	}
    		else {
    	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;
    	}
    Any ideas?

    P.S MK27 Is it still inefficient if I write a function separately and then implement it after it has compiled correctly?

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The way to check numofbees is to check the return value of scanf(), which is the number of items read. If the user did not type a number, scanf("%d", ...) will return 0. You could also set numofbees to zero initially and then use if (!numofbees), since ordering zero bees is probably wrong. Ordering negative bees would be wrong too, so you could use scanf("%u", ...) for unsigned.

    Is it still inefficient if I write a function separately and then implement it after it has compiled correctly?
    Not sure what you mean. Basically, don't refer to code you haven't written yet -- write in such a way so that you can compile and test at fairly short intervals. Some people do stuff like this:

    Code:
    void someUnfinishedFunc() {
    // nothing in the parameter list will accept anything
    // nothing in the function body will do nothing with anything
    }
    
    switch (choice) {
        case (1) : someUnfinishedFunc("abc", 666);
                       break
    Then go back and finish the functions one at a time.

    Which I guess is useful if you are collaborating and there is no plan here, so you want to make your intention clear. Or this represents some kind of brilliant idea you might forget but can't complete right away.
    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

  4. #19
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    So it should be something like this? I'm not sure if I understood you correctly.

    Code:
    #include<stdio.h>
    
    
    int main()
    {
    	int numofbees = 0;
    	float price = 5;
    	char query;
    
    
    	printf("How many bees would you like to purchase?\n");
    		scanf("%u", &numofbees);
    
    
    		if(numofbees = !numofbees){
    			printf("Invalid amount, please try again.\n");
    			printf("How many bees would you like to purchase?\n");
    				scanf("%u", &numofbees);
    	}
    		else {
    	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;
    	}

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    ! is logical NOT. Something "is not" in C if it is false. The only false values are 0 and NULL. So if (!numofbees) will be true if numofbees == 0. "if (numofbees = !numofbees)" does not make much sense, lol -- I think it will just set numbofbees to either 1 or 0.

    WRT to the return value of scanf():

    Code:
        printf("How many bees would you like to purchase?\n");
        if (!scanf("%u", &numofbees)) {   // ie, scanf returned 0
           printf("Invalid amount, please try again.\n");
    You could also use:
    Code:
    check = scanf("%u", &numofbees)
    if (!check) { // ie, scanf() returned 0
    Last edited by MK27; 12-27-2011 at 11:27 AM.
    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

  6. #21
    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 12:34 PM.

  7. #22
    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
    andrew89 likes this.
    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

  8. #23
    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 02:16 AM.

  9. #24
    Registered User
    Join Date
    Dec 2011
    Posts
    20
    Help?

  10. #25
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Don't get a bee in your bonett (no idea whether your name was intentional or not, sorry if someone has made that joke already).

    Have you looked at your code? Why do you expect it not to print "Invalid Price". There is nothing to branch past it?

  11. #26
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    Fix your indentation and it should somewhat reveal the problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 2 of 2 FirstFirst 12
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-22-2009, 11:10 PM
  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

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