Thread: Nested while loop inside for loop

  1. #61
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Sonny View Post
    That's the thing this isn't a difficult problem, but it's just the little things that are getting me into trouble. I wrote the program myself and only in the most direst circumstance when every other option I have has been spent do I ask for help.
    Which is why doing it on paper would be relatively non-difficult. My point is that by meticulously following your code, you will be able to see all those little things that are causing trouble. I'm not putting you down for asking the question - I'm just trying to share a technique with you - and this technique is the only reason that I've never had to ask a question here yet (soon though...).

  2. #62
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Matticus View Post
    Which is why doing it on paper would be relatively non-difficult. My point is that by meticulously following your code, you will be able to see all those little things that are causing trouble. I'm not putting you down for asking the question - I'm just trying to share a technique with you - and this technique is the only reason that I've never had to ask a question here yet (soon though...).
    Oh crap... no you've jinxed it...

    Seriously though the "paper debug" is a very helpful troublshooting tool...

  3. #63
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Matticus View Post
    .... - I'm just trying to share a technique with you - and this technique is the only reason that I've never had to ask a question here yet (soon though...).
    And when you do I am going to tell you to google it. I can't wait......
    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.

  4. #64
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by AndrewHunter View Post
    The outline I gave you was general. You need to reset your counters as well. Ok, you know what look at this. NOTE: I did not do percentages for this. I am sure you can figure out that math
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
    	int sCode, studID, m, loop = 0, loopS = 0;
    	float mark, totalPrint, totalStored, totalSchool=0,averageSection = 0, averageSchool = 0;
    
    
    	printf("\n\nEnter the Section Code: ");
    	scanf("%d", &sCode);
    	do{
    		totalStored = 0;
    		averageSection = 0;
    		loop = 0;
    		loopS++;
    
    		while (sCode > 4 || sCode < 1){
    			printf("Invalid value entered. Must be 1 to 4, please re-enter: ");
    			scanf("%d", &sCode);
    		}
      
    		printf("Enter the Student's ID: ");
    		scanf("%d", &studID);
    		do{
    			totalPrint = 0;
    			loop++;
    			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);
    				}
    				totalPrint = totalPrint + mark;
    			}
    			totalStored = totalStored + totalPrint;
    			printf("%d's total mark is: %.2f\n", studID, totalPrint);
    			printf("Enter the Student's ID ['0' to quit]: ");
    			scanf("%d", &studID);
    		} while (studID != 0);
    		
    		averageSection = (totalStored / loop);
    		totalSchool = totalSchool + averageSection;
    		printf("\nThe average for the section is: %.2f%\n\n", averageSection);
    		
    		printf("Enter the Section Code ['0' to quit]: ");
    		scanf("%d", &sCode);
    
    	} while (sCode != 0);
    	
    	averageSchool = (totalSchool / loopS);
    	printf("\nThe average for the course is: %.2f%\n\n", averageSchool);
    
    	
    	return (0);
    }

    Oh dang, I didn't even see this. If I may ask what did you change around?

  5. #65
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Look through it and compare it to the code you have written. You will see it. It follows the outline I posted for you and adds a couple variables to accomplish the task. Also the importance of resetting some loop counters.
    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.

  6. #66
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by AndrewHunter View Post
    And when you do I am going to tell you to google it. I can't wait......
    Me neither! Maybe I'll throw you a super-softball ... something like this, perhaps.

  7. #67
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    LOL....this board is a constant source of amusement.
    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. #68
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by AndrewHunter View Post
    Look through it and compare it to the code you have written. You will see it. It follows the outline I posted for you and adds a couple variables to accomplish the task. Also the importance of resetting some loop counters.
    May I ask why you initiated the loop = 0; in both the variable declaration at the top and in the first do-while loop?

  9. #69
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    It was just leftover from your initial declarations that I copied. Didn't feel like changing them. Anything initialized in the loops doesn't have to be in the declarations. I should have specified that I didn't look that closely at your code, just worked it so the logic control flow would work out. I am confident in your ability to go through and make the appropriate changes. Feel free to ask additional questions so you understand exactly why what you were doing didn't work.

    This is my freebie though. In the future you may want to start a little earlier and work out the programs as has been suggested to you. For instance, I am sure you are going to get another project tomorrow after you turn this one in. Sit down, work it out in psuedo code, then start to code. If you get questions or stuck, come back here and post your issue, your psuedo code, and what you did in code. Start early. That is the way to get the best help on this board, showing the effort, which includes proper planning ahead.
    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. #70
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by AndrewHunter View Post
    It was just leftover from your initial declarations that I copied. Didn't feel like changing them. Anything initialized in the loops doesn't have to be in the declarations. I should have specified that I didn't look that closely at your code, just worked it so the logic control flow would work out. I am confident in your ability to go through and make the appropriate changes. Feel free to ask additional questions so you understand exactly why what you were doing didn't work.

    This is my freebie though. In the future you may want to start a little earlier and work out the programs as has been suggested to you. For instance, I am sure you are going to get another project tomorrow after you turn this one in. Sit down, work it out in psuedo code, then start to code. If you get questions or stuck, come back here and post your issue, your psuedo code, and what you did in code. Start early. That is the way to get the best help on this board, showing the effort, which includes proper planning ahead.
    Yeah I agree, thanks a bunch! You don't know how much it is appreciated. Maybe in the near future i'll be helping you. :P

  11. #71
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by AndrewHunter View Post
    It was just leftover from your initial declarations that I copied. Didn't feel like changing them. Anything initialized in the loops doesn't have to be in the declarations. I should have specified that I didn't look that closely at your code, just worked it so the logic control flow would work out. I am confident in your ability to go through and make the appropriate changes. Feel free to ask additional questions so you understand exactly why what you were doing didn't work.

    This is my freebie though. In the future you may want to start a little earlier and work out the programs as has been suggested to you. For instance, I am sure you are going to get another project tomorrow after you turn this one in. Sit down, work it out in psuedo code, then start to code. If you get questions or stuck, come back here and post your issue, your psuedo code, and what you did in code. Start early. That is the way to get the best help on this board, showing the effort, which includes proper planning ahead.
    So basically if the counters are declared at the top of my program, I don't need to declare them again inside the loops?

  12. #72
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    No, the loop counters need to be reset to 0 where they are in the loops. If they are initialized in the loops they do not need to be initialized when they are declared.

    Note the difference between declared and initialized.
    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.

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, 12:29 PM
  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, 04:11 PM
  5. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM