Thread: Help with my do while loop please?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    33

    Help with my do while loop please?

    Hey folks, I'm running into this weird problem that ive been trying to figure out for a week now why it isn't working.

    Code:
    do{                                     if (curbing =='Y'){
                                             curbingCost=(CURB*length);
                                             pavingCost=(asphalt+curbingCost);
    
    
                                            }else if (curbing =='N'){
                                                      curbing=(CURB*length*zero);
                                                      pavingCost=asphalt;
                                            }else {
                                                    printf("Please select Y or N\n");
                                            }
    
    
                                     }while(curbing !='Y' && curbing !='N');

    the problem I'm getting is when I press Y everything proceeds into the next line however if i press N it goes nuts and says
    "Please select Y or N" over and over again.

    I asked my prof she said she knows exactly why but she wont tell me lol she said to try and figure it out, however my assignment is due tomorrow by midnight and this one problem is stopping me from completing it.

    btw I did try
    Code:
    (curbing !='Y' || curbing !='N');
    initially, however that resulted in me selecting either Y or N but nothing happens after, like:

    Please select Y / N : Y









    ....and in that space I type whatever I want and keep hitting enter, I have to cntrl+c to exit the program.

    Any help would be greatly appreciated, thank you.

    p.s Im trying to avoid posting the entire code to my program as I have class members who lurk this forum and they found my Paint Program code last time and used that to submit their assignment and they including myself received a warning for plagiarism -_-.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can't tell much from the code you submitted, but this sounds like one of the two most common problem with students.

    If you have
    Code:
    scanf("%c", &myvariable);
    and forget to include the &, you can get results like this.

    #2 is when you enter data with a scanf(), you hit the enter key, but the resulting newline char '\n' that the enter key sends into the input stream, is not included into your variable - it's still in the input stream. Lurking there, waiting to pounce.

    If you have a next scanf() that expects a number %d, and you give it a letter, it will go nuts, and run forever. If you don't get rid of the newline, you can also have scanf()'s for char's, look like they were "skipped" or jumped over, in your program. Because the newline is a char, and scanf() is waiting for a char, so it takes the newline char, and says "thanks, I'm done here". xD

    So, after every scanf(), add
    Code:
    getchar();
    which will pull the newline off of the input stream. Note that fflush(stdin), etc., don't work generally - flush operations are for OUTPUT not input streams. (You can't flush the kitchen faucet, you CAN flush the toilet). xD

    Try that. Note that you can also (with experience), put spaces into your format string of scanf(), to make it behave correctly, but that is something to experiment with when you have more time.

    I so don't understand * zero in your math there.

    P.S. If you haven't included a scanf() (or input of some type), INSIDE the do while() loop, then you need to. I thought you left it out to be "secretive" about it, but maybe NOT?
    Last edited by Adak; 02-11-2012 at 04:59 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I think this program demonstrates the problem:
    Code:
    #include <stdio.h>
    int main(void)
    {
        printf("Please select Y or N\n");
    }
    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"

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This is what's causing the infinite loop.
    Code:
    curbing=(CURB*length*zero);

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    Quote Originally Posted by oogabooga View Post
    This is what's causing the infinite loop.
    Code:
    curbing=(CURB*length*zero);

    you guys are brilliant!

    i was missing the &myvariable for something else xD

    and the solution that Oogaboo stated fixed my problem.

    like i dont think you guys understand how incredibly happy i am right now xD!!! everything is finally working esjgdfkhlx thanks so much folks.

    one last question, was it because i had *zero that was causing the problem? I tend to add more to my code than I need to, a forum member on here told me that before in my old posts, so now I'm just going to work on simplifying it.

    thanks again !!!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you have a do while loop and the program needs some value in order to exit, then you have to have code that allows the variable that holds that value, to be altered.

    Code:
    scanf("%c", &choice);
    do {
       //other stuff
       printf("enter y or n here: \n");
      
    }while (choice != 'y')
    is rough code for a loop that either always end, or will never end, since there is no way inside the do loop, to change the value of the variable choice.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    33
    ah i see. makes sense.

    what if I want to make sure that the user inputs only numerical values for length, and width. Can I do that in a do while loop without a function?

    I managed to make it so that the user must input a positive integer, using
    Code:
    (length<=0)
    but I don't know how to stop the program from accepting a character.

    Can I have


    Code:
       printf("enter length: \n");
       scanf("%d", &length);
       if (length<=0){                               
    
             printf("\nPlease enter a positive value\n\n");
    
    
      } else if (???){
             
            printf("\nPlease enter a numerical value\n\n");                                          
                                   
      } else {
    
    }
    ?
    Last edited by ShadowBoss; 02-12-2012 at 02:22 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The simple way to do this, is to use the built in functions for this, found in ctype.h.

    Include that file, and read your help file on it's components. They do everything that you want done: tests for numbers, punctuation, alpha, etc. It's all there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  2. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  3. The Infinit loop that doesn't loop.
    By errigour in forum C Programming
    Replies: 1
    Last Post: 11-09-2010, 11:31 AM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. While loop ..crating an infanat loop
    By fmchrist in forum C Programming
    Replies: 7
    Last Post: 01-14-2006, 10:52 AM