Thread: twinkie problem

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    25

    twinkie problem

    Looked through my code several times and cant seem to fins why the program continually takes the value of coin to be l or L making it always take 1.00 of the total. I'm looking for help with that as well as how to display my float number to only 2 decimal points. I think I remember you being able to use modulus but can't remember exactly how. Thanks in advance.
    Code:
    #include <stdlib.h>
    
    int main()
    {
    	char coin, l , L , q , Q , d , D , n , N ;
    		float total = 0, tobepaid = 3.50, change;
    
    printf("Welcome to the world of deep-fried Twinkies\n Each Twinkie costs $3.50\n\n");
    
    while( total <= 3.50)
    {
    printf("$%f\n\n" , tobepaid - total);
    	printf(" L or l for loonie\n Q or q for quarter\n D or d for dime\n N or n for nickel\n");
    		scanf("%c" , &coin);
    
    getchar();
    
    
    if((coin = 'l')||(coin = 'L'))
    {
    	total = total + 1.00;
    }
    else if((coin = 'q')||(coin = 'Q'))
    {
    	total = total + 0.25;
    }
    else if((coin = 'd')||(coin = 'D'))
    {
    	total = total + 0.10;
    }
    else if((coin = 'n')||(coin = 'N'))
    {
    	total = total + 0.05;
    }
    else
    {
    	printf(" This machine only accepts Loonies, Quarters, Dimes and Nickels");
    }
    }
    change = total - tobepaid;
    printf(" Enjoy your Twinkie!\n You have %f in change\n" , change);
    
    system("pause");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Code:
    l , L , q , Q , d , D , n , N ;
    You don't need to declare these. When you say 'L', you mean the character L, you are not referring to any declared variable.

    Code:
    if((coin = 'l')||(coin = 'L'))
    use == for comparison, not =
    = is assignment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To print out a float to 2 decimal places:

    Code:
    printf("%.2f", myfloatVariable);

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, try to avoid system("pause"). It's platform specific, inefficient and dangerous.

    Also, this thread has the most delicious title I have seen in a while
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Also, try to avoid system("pause"). It's platform specific, inefficient and dangerous.
    Not that I disagree, but when you make a function specifically designed to waste time, does it matter how efficient it is? ^_^

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Quote Originally Posted by claudiu View Post
    Also, try to avoid system("pause"). It's platform specific, inefficient and dangerous.

    Also, this thread has the most delicious title I have seen in a while
    Except on any other platform, you won't have this problem. And efficiency? really? how often is this line run?

    I think it's the least ugly of the ugly solutions to this ugly problem.

    It's very clear in what it does, versus some cryptic getchar() or whatever. And it works.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It is clear, but it is also a dangerous practice. What happens if you have another program called "pause" in the path?

    Read this:
    http://www.gidnetwork.com/b-61.html

    Note that in terms of efficiency the author notes :" making a system call to have your program wait for input is sort of like using a bulldozer to open your front door".
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by cyberfish View Post
    Except on any other platform, you won't have this problem.
    Uhh... How do you figure? Are you assuming every other platform requires you to run programs from the command line or something?
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What happens if you have another program called "pause" in the path?
    Then you have a bigger problem to worry about. If someone can put it there for you, why can't they also run it for you?

    Or would you suggest -
    Code:
    system("C:\Windows\system32\pause.exe");
    or whereever it actually is?

    No one writes "real" command line programs that are supposed to be double clicked. It's just a workaround for beginners trying to experiment. Clarity and functionality are more important than the theoretical obscure vulnerability in this case.

    Read this:
    http://www.gidnetwork.com/b-61.html

    Note that in terms of efficiency the author notes :" making a system call to have your program wait for input is sort of like using a bulldozer to open your front door".
    I read it. And I disagree.

    Since you quoted it, am I right to assume that you agree and it is the argument you are making?

    In which case I'm going to ask - what's wrong with using a bulldozer to open your front door?

    Uhh... How do you figure? Are you assuming every other platform requires you to run programs from the command line or something?
    I don't need to assume. I have used them.

    People generally don't run command line programs by double clicking on other platforms. Even if they did, I believe only cmd closes after it finishes executing the program.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    The basic point is that in this instance:

    1. We are not interested in running this on multiple platforms as it is a simple learning tool
    2. The efficiency of a call that is executed once and is specifically designed to waste time is a non-issue - you could optimized the living hell out of it without achieving anything useful.
    3. The OP does not care even slightly how efficient his program that executes in milliseconds is.

    Sure, that line should be avoided in general application, and it is good to point this out to people, but at least make the argument relevent to the problem.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by cyberfish View Post
    Then you have a bigger problem to worry about. If someone can put it there for you, why can't they also run it for you?

    Or would you suggest -
    Code:
    system("C:\Windows\system32\pause.exe");
    or whereever it actually is?

    No one writes "real" command line programs that are supposed to be double clicked. It's just a workaround for beginners trying to experiment. Clarity and functionality are more important than the theoretical obscure vulnerability in this case.


    I read it. And I disagree.

    Since you quoted it, am I right to assume that you agree and it is the argument you are making?

    In which case I'm going to ask - what's wrong with using a bulldozer to open your front door?


    I don't need to assume. I have used them.

    People generally don't run command line programs by double clicking on other platforms. Even if they did, I believe only cmd closes after it finishes executing the program.
    The argument I am making is that it is important to give beginners advice containing GOOD PRACTICES, because that's what they will remember next time they run into the same problem.

    This idea that, "well, this is not good practice but it's not going to kill him using it in this case" is just absurd in my view. It sort of yells "freedom" in a way that endangers the actual programmer and the users of its programs. Once you KNOW C, sure, do whatever you want, when you are learning C though, start by learning the right way to do things. Because if you don't, you will be trailing these bad practices throughout your career and possibly even in someone else's code.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The argument I am making is that it is important to give beginners advice containing GOOD PRACTICES, because that's what they will remember next time they run into the same problem.
    This I agree. And I'm arguing using system("pause"); is GOOD PRACTICE.

    The efficiency argument is a very bad one. Premature optimization is the root of all evil.

    In this case there is absolutely 0 need to optimize.

    If you look at some other pages on the site you quoted, the author also suggests using getchar() instead of scanf() for efficiency, and "proving" it using code size. This is the same kind of pre-mature (evil) optimization that we want beginners to avoid like a plague. And in this case, the argument is not even valid at all. The author seems to have never heard of this wonderful new technology called dynamic linking.

    This is not just for toy programs. Even in high performance programs, you'll want to write a correct and working program first, then profile and find out the bottlenecks, THEN optimize.

    system("pause"); is most certainly not where your bottleneck is.

    The security argument is the only valid argument here, but I don't think it's more important than code clarity, conciseness, and functionality, all of which system("pause"); demonstrate.

    Sure, there's nothing wrong about letting them know about it, but I don't think it should stop them from using it.

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I understand what you are saying, but I guess we will have to agree to disagree. As for the other articles of that author, I haven't read any of them. I have always avoided making system calls from my programs unless it was absolutely imperative, for the reasons I mentioned and I just thought that his article pointed out most of the reasons, that's all, I am in no way sympathetic to the author I quoted in general terms, since I haven't read his other work. If you want to find out what people generally think of system("pause") just Google it. I have personally found forums where they are neutral towards it and forums where they discourage it. I have never seen someone recommend it as "good practice".

    On the upside, I think this debate has done well to exemplify the pros and cons over this situation and have given the OP with sufficient reasons to judge for himself, which I believe was the idea anyway.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, if a beginner can understand the pros and cons of this issue, then the same beginner can avoid it entirely by learning how to run the program from a separate console window.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Well, if a beginner can understand the pros and cons of this issue, then the same beginner can avoid it entirely by learning how to run the program from a separate console window.
    I agree. We should tell people to do that instead . Why workarounds when we have a proper solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM