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; Originally Posted by beta3designs I think you still have the duplicated asking for student's ID... Ok, let's see... If you ...

  1. #16
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by beta3designs View Post
    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

    Yeah but look at this, it's a sample from my assignment;
    As you can see it asks for the Section code (without 0 to exit) and then asks again with the zero to exit. How do I do that? :s

    Enter the Section Code: 0
    Invalid value entered. Must be 1 to 4, please re-enter: 1
    Enter the Student's ID: 456789
    Enter mark#1: 10
    Enter mark#2: -20
    Invalid grade entered. Must be 0.0 to 20.0, please re-enter: 20
    Enter mark#3: 5.5
    Enter mark#4: 5
    Enter mark#5: 10
    000456789's total mark is: 50.5

    Enter the Student's ID ['0' to quit]: 123654987
    Enter mark#1: 1
    Enter mark#2: 2
    Enter mark#3: 3
    Enter mark#4: 4
    Enter mark#5: 5
    123654987's total mark is: 15.0

    Enter the Student's ID ['0' to quit]: 0
    The average for the section is 32.75%

    Enter the Section Code [0 to quit]: 5
    Last edited by Sonny; 07-31-2011 at 03:11 PM.

  2. #17
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by beta3designs View Post
    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
    So i'm good right? Even with the two student ID prompts? Is there any way around that?

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sonny View Post
    Wait so for a validation loop I don't have to scanf?!
    Actually that's the only thing that should be in your loop...

  4. #19
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by CommonTater 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;                          <---- delete this line
          average = (store / (loop * 100)) * 100;      <---- and this one
        }
        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);
    }
    What you need to do is stop and think like the program... BE the program... "Do I want to do this in the middle of an error condition?"....
    I AM the program. lol I just posted my updated code on the last page I fixed it.

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Sonny... same advice as yesterday... think, plan, do...

    C does stuff in the exact order you tell it... so your question is "Do I want to do this now?"...
    quzah likes this.

  6. #21
    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);
            
          }
          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);
    
    }
    The parts in red are where you ask the user for student id. So if you didn't want the first part to repeat in your do-while loop, where would you move it to?
    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.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Sonny... same advice as yesterday... think, plan, do...

    C does stuff in the exact order you tell it... so your question is "Do I want to do this now?"...
    Sometimes the order doesn't work out like that, but it really should. A number of years ago I wrote something drunk, and couldn't figure out what it was doing the next day when I was sober.

    But it worked.

    Think. Plan. Do-while( sober );


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by AndrewHunter View Post
    The parts in red are where you ask the user for student id. So if you didn't want the first part to repeat in your do-while loop, where would you move it to?
    Before the do while loop?

  9. #24
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Hmm....why don't you try that out and see what you get.
    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. #25
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by AndrewHunter View Post
    Hmm....why don't you try that out and see what you get.
    I did and it works perfectly. How'd you decipher it so fast?

  11. #26
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sonny View Post
    I did and it works perfectly. How'd you decipher it so fast?
    Quzah and I phoned him and told him what to post in a conference call
    AndrewHunter likes this.

  12. #27
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Just experience. No magic there, except for quzah's drunk coding that is. Keep working on the techniques everyone has been telling you and you will be able to do it as well.
    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.

  13. #28
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by CommonTater View Post
    Quzah and I phoned him and told him what to post in a conference call
    Very efficient. :P haha thanks a lot for the help guys. Hopefully after a little more experience I can help future noobs like you're helping me.

  14. #29
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    Quzah and I phoned him and told him what to post in a conference call
    D**n, now my secret is out! ...LOL


    EDIT: BTW Tater, I like your new tag line.
    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.

  15. #30
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by AndrewHunter View Post
    Just experience. No magic there, except for quzah's drunk coding that is. Keep working on the techniques everyone has been telling you and you will be able to do it as well.
    lmaoo! I can't even properly take a leak when i'm drunk...I will bud thank you.

Page 2 of 5 FirstFirst 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