Like Tree11Likes

Nested while loop inside for loop

This is a discussion on Nested while loop inside for loop within the C Programming forums, part of the General Programming Boards category; Ok so I used this site yesterday and it was a great help you guys are amazing but today I ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    38

    Nested while loop inside for loop

    Ok so I used this site yesterday and it was a great help you guys are amazing but today I ran into another little problem. I have a nested while loop inside a for loop. The while loop is an error message that asks for user input if the input from the for loop was incorrect. The thing is, the incorrect data input by the user will still add onto the total when I do not want it to. For example; 5 students enter their grades out of 20. Student 1 enters 1 Student 2 enters 2 etc...student 3 enters 300 when the user enters 300 for student 3 it does give an error message stating that invalid grade was entered then student 3 enters 3, student 4 enters 4 and student 5 enters 5. The total should be 15 (1+2+3+4+5) but for some reason my program adds the incorrect number input by the user (300) and adds it to my total. Any help would be greatly appreciated, I can't figure this one out.

    Code:
    do {
      total = 0;
      loop++;
      printf("Enter the Student's ID: ");
      scanf("%d", &studID);
    
      for (m = 1; m < 6; m++)
      {
        printf("Enter Mark #%d: ", m);
        scanf("%f", &mark);
        while (mark > 20 || mark < 0)
        {
          printf("Invalid grade entered. Must be 0.0 to 20.0, please re-enter: ");
          scanf("%f", &mark);
          total = total + mark;
          average = (store / (loop * 100)) * 100;
        }
        store = store + mark;
        total = total + mark;
        average = (store / (loop * 100)) * 100;
      }
    
      printf("%d's total mark is: %.2f\n", studID, total);
      printf("Enter the Student's ID ['0' to quit]: ");
      scanf("%d", &doAgain);
    } while (doAgain);
    
    printf("The average for the section is: %.2f%\n", average);
    }

  2. #2
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    I know it must be some small logical error but I haven't found it after hours of searching.

  3. #3
    C <3er
    Join Date
    Jul 2011
    Posts
    46
    Mmmm I tried your program and I can't reproduce the bug I entered 1, then 200 and then 0, 5 times so that the average etc. was 0 and I could see if it was adding the 200 but the average was 0... maybe as you haven't posted them, try to check your types and in the while loop add .0 after your limits:
    Code:
    while (mark > 20.0 || mark < 0.0)
    (Added ".0")

    Btw you are asking for students ID twice... at the start of the while loop and at the end of the while loop... but you'll end up having only the second ID you enter. Because it overrides the one that is asked for at the end of the while loop, hope you understand what I mean.
    Last edited by beta3designs; 07-31-2011 at 02:23 PM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    Why are you changing the value of total and average inside the while loop? You should just be getting the proper mark.

    Jim

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by beta3designs View Post
    Mmmm I tried your program and I can't reproduce the bug I entered 1, then 200 and then 0, 5 times so that the average etc. was 0 and I could see if it was adding the 200 but the average was 0... maybe as you haven't posted them, try to check your types and in the while loop add .0 after your limits:
    Code:
    while (mark > 20.0 || mark < 0.0)
    (Added ".0")

    Btw you are asking for students ID twice... at the start of the while loop and at the end of the while loop... but you'll end up having only the second ID you enter. Because it overrides the one that is asked for at the end of the while loop, hope you understand what I mean.
    Hey there I tried your way adding the zeros but it didn't work. And as for asking for the student ID twice the first message has to ask for the student ID without saying press 0 to exit because the program will run at least once, so I have to have to ask twice no?

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by jimblumberg View Post
    Why are you changing the value of total and average inside the while loop? You should just be getting the proper mark.

    Jim
    I tried doing it the other way yesterday but I ran into many errors. For one I can only use loops for this program nothing else so it only makes it harder.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by Sonny View Post
    Code:
    do {
      total = 0;
      loop++;
      printf("Enter the Student's ID: ");
      scanf("%d", &studID);
    
      for (m = 1; m < 6; m++)
      {
        printf("Enter Mark #%d: ", m);
        scanf("%f", &mark);
        while (mark > 20 || mark < 0)
        {
          printf("Invalid grade entered. Must be 0.0 to 20.0, please re-enter: ");
          scanf("%f", &mark);
          total = total + mark;
          average = (store / (loop * 100)) * 100;
        }
        store = store + mark;
        total = total + mark;
        average = (store / (loop * 100)) * 100;
      }
    
      printf("%d's total mark is: %.2f\n", studID, total);
      printf("Enter the Student's ID ['0' to quit]: ");
      scanf("%d", &doAgain);
    } while (doAgain);
    
    printf("The average for the section is: %.2f%\n", average);
    }
    You do not want to do anything in your while loop that affects your total or average. This loop just validates input and will exit upon the input being the right value.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,361
    Code:
        printf("Enter Mark #%d: ", m); // prompt for "mark"
        scanf("%f", &mark);            // receive input for "mark"
        while (mark > 20 || mark < 0)  // while an invalid input, enter loop
        {
          printf("Invalid grade entered. Must be 0.0 to 20.0, please re-enter: ");
          scanf("%f", &mark);          // receive a new input for "mark"
          total = total + mark;        // total is updated with new "mark" input before it is validated!
          average = (store / (loop * 100)) * 100;
        }
        store = store + mark;
        total = total + mark;
        average = (store / (loop * 100)) * 100;

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by Sonny View Post
    I tried doing it the other way yesterday but I ran into many errors. For one I can only use loops for this program nothing else so it only makes it harder.
    You can't do it this way though. Think about it. If I entered -35.4, according to your logic, even though it is bad input it still effects the rest of the program. Now why would you do that? The whole reason for checking the input is to ensure bad input doesn't effect the outcome.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by AndrewHunter View Post
    You do not want to do anything in your while loop that affects your total or average. This loop just validates input and will exit upon the input being the right value.
    Wait so for a validation loop I don't have to scanf?!

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Your scanf stores the value in mark. It doesn't change total or average until you tell it to do so. Which you do not want to do until you validate mark. Which is the whole point of a validation loop.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by AndrewHunter View Post
    You can't do it this way though. Think about it. If I entered -35.4, according to your logic, even though it is bad input it still effects the rest of the program. Now why would you do that? The whole reason for checking the input is to ensure bad input doesn't effect the outcome.
    Yeah when you put it that way...what I did, doesn't really make too much sense. :P
    Do you know anything about segmentation errors? I keep getting them when I run my program on linux. :s

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Post your updated code. The only thing that immediately jumps out at me is where do you define loop? Did you give it a starting value anywhere, like 0?

    EDIT: Same question for doAgain.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    It's working perfectly now, I just added another part into the program it's working fine too. Tell me what you think.

    Oh btw should the part in bold be after the validation loop or before?

    Code:
    do {
        total = 0;
        loop++;
        printf("Enter the Student's ID: ");
        scanf("%d", &studID);
    
        for (m = 1; m < 6; m++)
        {
          printf("Enter Mark #%d: ", m);
          scanf("%f", &mark);
          while (mark > 20 || mark < 0)
          {
            printf("Invalid grade entered. Must be 0.0 to 20.0, please re-enter: ");
            scanf("%f", &mark);
            
          }
          store = store + mark;
          total = total + mark;
          average = (store / (loop * 100)) * 100;
        }
    
        printf("%d's total mark is: %.2f\n", studID, total);
        printf("Enter the Student's ID ['0' to quit]: ");
        scanf("%d", &studID);
      } while (studID != 0);
    
    printf("Enter the Section Code ['0' to quit]: ");
    scanf("%d", &sCode);
    } while (sCode != 0);
    printf("The average for the section is: %.2f%\n", average);
    
    }
    Last edited by Sonny; 07-31-2011 at 02:57 PM.

  15. #15
    C <3er
    Join Date
    Jul 2011
    Posts
    46
    I think you still have the duplicated asking for student's ID...
    Ok, let's see... If you ask for an ID let's say you enter '1' then you finish entering the marks and it asks you to enter an ID (with the press 0 to exit) and you enter '2' but then it asks again (without the press 0) and you input '3' then you'll be entering the marks for student '3' not your first selection '2'...
    You only need to ask once! Not at the start and at the end... you should only ask at the start

    EDIT: Bold should be after the validation loop and I didn't notice that you were modifying the values inside of it and maybe my tests didn't affect the values but I can see that's solved now :P
    Last edited by beta3designs; 07-31-2011 at 03:00 PM.

Page 1 of 5 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. change var inside loop and run loop again
    By d387420489 in forum C Programming
    Replies: 5
    Last Post: 07-29-2011, 01:19 AM
  2. For Loop inside While Loop
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-29-2008, 11:29 AM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  4. for loop inside of a for loop
    By pippen in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2003, 03:11 PM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM

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