while loop problem

This is a discussion on while loop problem within the C Programming forums, part of the General Programming Boards category; Hey everybody. I was just making an algebraic equation calculator that has a little bug in it and I don't ...

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230

    while loop problem

    Hey everybody. I was just making an algebraic equation calculator that has a little bug in it and I don't see whats wrong with it. The glitch is... When it gets into a while loop it will skip the users input once then it will complete the loop and the next time ask for it. Here is the code... Please help this has me stumped.

    Code:
    #include <stdio.h>
    
    main()
    {
          int var;
          int letnum = 0;
          int varcheck = 0;
          int varletter[100];
          int vardisplay = 1;
          
          printf("enter the number of variables (max 100):");
          scanf("%d",&var);
         
          
          
          while (var >= varcheck)
          {
          printf("\nplease enter the letter for variable %d:",vardisplay);
          scanf("%c",&varletter[letnum]);
          vardisplay++;
          letnum++;
          varcheck++;
          }
          
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    The problem is you're reading characters with scanf using the %c format specifier (or rather it is how you are currently using the %c format specifier and processing user input). At the first scanf call you enter an integer followed by the Enter key. The number is parsed and stored into your var variable. The newline character remains in the input buffer/stream. When you get to the scanf calls in the loop, it is looking for a single character and finds one - that pesky newline character - and reads that from the stream and continues on with the loop. You expect it to stop and wait for you to enter something but its already too late.

    The second time through the while loop there is nothing available in the input buffer to read so the scanf call then waits for input from the user (you). Again, you can type a character and again you press Enter which once more results in two characters being stored in the input buffer, the one you really want plus that newline character. This means the second run through the loop processes the character you mean to but the third time through that newline char is once again mucking things up. You see where I'm going with this?

    The easiest way to deal with this is to alter slightly the scanf call (the one in the while loop) from this:
    Code:
    scanf("%c",&varletter[letnum]);
    To this:
    Code:
    scanf(" %c",&varletter[letnum]);
    There is a subtle difference here, the leading space tells scanf to ignore leading whitespace (newline/tabs/spaces). The effect of that is that it will eat up and throw away the newline character and then wait for more user input as you are wanting it to. Of course other methods to solving this issue are to use entirely different means for reading/parsing user input other than scanf.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple loop problem I cant seem to solve
    By LightYear in forum C Programming
    Replies: 8
    Last Post: 03-21-2010, 06:59 PM
  2. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 01:15 PM
  3. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  4. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  5. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM

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