Thread: while loop problem

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Unhappy while loop problem

    Hi,
    I'm working onn an assignment and I'm stuck! Maybe it's because I'm still new at programming but I can't get my program to exit after you enter 'N' for no when the user is asked "Do you want to try again?", it just starts over as if I had typed in 'Y'.
    Also can someone tell me if it's possible to empty the memory so that the coins don't add up after each try?
    Any help would be great I've spent hours trying to fix this on my own!
    thanks,
    here is the relavent code.
    Code:
    while(ch='Y'){
             
                      
        printf("\n Your total is $", amount);
        scanf(" %f", &amount);
        
        change=(5.00-amount);
       
        while(change>=0 && change<=500){
    .....(enclosed while statements, I checked the curly brackets!)
    
    // print results
           printf(" You have %d coins of 2 dollars \n", toonie);
           printf(" You have %d coins of 1 dollar \n", dollar);
           printf(" You have %d coins of 25 cents \n", quarter);
           printf(" You have %d coins of 10 cents \n", dime);
           printf(" You have %d coins of 5 cents \n", nickel);
           printf(" You have %d coins of 1 cent \n", cent);
           break;
            
          {
           printf("\n Do you want to try again? (Y/N)\n");
           ch=toupper(getche());
            printf("\n");
             
           } getch();
           }
    I tried and tried to fix it without result.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    ch='Y' does not compare ch to the letter 'Y'. (It does, however, assign the letter 'Y' to ch.)

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Amazingly, I still have this in big red marker above my function keys:

    ==

    it looks very ichingish here, probably something about an existing balance
    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. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    7
    For your first line, try:

    Code:
    while (ch == 'Y')
    {
       ...
    }
    Alternatively you could use:

    Code:
    do
    {
       ...
    }
    while ((ch = toupper(getche())) == 'Y');

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    thank you

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I mentioned this type of error here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM