Thread: Help with loop stacking

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

    Help with loop stacking

    I'm not sure if it's called loop stacking but I think it makes sense. Basically I have this program I need to write using ONLY loops. Nothing else. The program has to calculate 5 marks (input by the user), adds the 5 marks and then asks if the user wants another students' 5 marks to be calculated. I got the looping part done I just need help because my program "stacks" the totals of all the students and does not separate them. For example; if student 1's combined 5 marks equals 15, when the program loops and the user enters the marks for the second student it will literally stack onto the total set by the previous student. So instead of having student 2 with a mark of 15 it will be 30 instead. Thanks in advance for the help I know there's a logical answer to this but I can't find it.

    Code:
    do {
      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)
        {
          printf("Invalid grade entered. Must be 0.0 to 20.0, please re-enter: ");
          scanf("%f", &mark);
        }
    
      }
      total += mark;
      printf("%d's total mark is: %.2f\n", studID, total);
    } while (doAgain);

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Do you need to save the value of all the students marks after the program run? If not just reset your total to zero at the beginning of your do loop.

    EDIT: Also, shouldn't your total be inside your for 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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So where do you set the value to be 0 initially? Why not do it at the start of the loop?

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    I'm sorry, the value of m? or mark?

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    mark.
    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. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    If you're talking about m, I set the value to m = 1; at the beginning of my program.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Ok give me a second brother, i'm going to try that out.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Brilliant man, thanks a bunch I got it. I knew it was some silly little logical error I made, i'm new to programming and all this info is overwhelming. Thanks again.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    No problem. The best thing to do when starting a program is sit down and represent what you would do if you had to perform the same task without a computer . Think carefully about all the steps you would have to take and then write them down in some form of pseudocode or shorthand that you are comfortable with. Then start writing the code.
    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
    Yeah good idea, I usually just jump into the code and come up with a whole bunch of little errors later. I should really try writing the pseudo code first.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sonny View Post
    Yeah good idea, I usually just jump into the code and come up with a whole bunch of little errors later. I should really try writing the pseudo code first.
    It doesn't matter how you do it... The goal is to understand the problem before you start trying to solve it.

    As I'm want to tell people from time to time: There is no programmer anywhere on this earth who can code the solution to a problem he doesn't understand.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Oh of course that's pretty obvious. I knew the blueprint of this problem in my mind but there were some little logical errors. I don't understand how programmers can keep all that information in their minds so that even the smallest error isn't overlooked.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sonny View Post
    Oh of course that's pretty obvious. I knew the blueprint of this problem in my mind but there were some little logical errors. I don't understand how programmers can keep all that information in their minds so that even the smallest error isn't overlooked.
    Oh that's as easy as pie... We don't.

    When I'm working a project the fist step is to think about it, almost becoming obsessed with understanding what has to be done. Then once I think I have a clear picture of what needs doing, I will start making notes, concentrating on different parts of the programs, one by one. Then once my notes are more or less complete, I start "modularizing" the program, breaking it down to ever smaller pieces, until my notes are covered in little bits of code and function names. These form a rather messy blueprint of the program.

    Then it's coding time, going through the blueprint bit by bit, writing functions and building source code... Each function is written and tested separately, as I add it to my code. When I'm not 100% certain of a function call, I stop and look it up. It's like assembling a jigsaw puzzle with my notes working as "the picture on the box" as a guide. Each section of code gets written, then rewritten until it's all working to my satisfaction. Then working out from smaller pieces to larger ones I keep adding and modifying until the last piece drops in place and the program is complete.

    During the coding process, I don't care about the big picture --that's in my blueprint-- I only care about the problem before me ... open a disk file, draw a window, handle messages, calculate memory usage... whatever I'm working on at the moment is the only thing on my mind. If you paint all the small parts correctly the big picture will paint itself...

    Just like any other kind of project .... Understand -- Plan -- Do.

    You don't think a carpenter is worried about the doors when he's putting on the roof, do you?
    Last edited by CommonTater; 07-30-2011 at 04:36 PM.

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    38
    Quote Originally Posted by CommonTater View Post
    Oh that's as easy as pie... We don't.

    When I'm working a project the fist step is to think about it, almost becoming obsessed with understanding what has to be done. Then once I think I have a clear picture of what needs doing, I will start making notes, concentrating on different parts of the programs, one by one. Then once my notes are more or less complete, I start "modularizing" the program, breaking it down to ever smaller pieces, until my notes are covered in little bits of code and function names. These form a rather messy blueprint of the program.

    Then it's coding time, going through the blueprint bit by bit, writing functions and building source code... Each function is written and tested separately, as I add it to my code. When I'm not 100% certain of a function call, I stop and look it up. It's like assembling a jigsaw puzzle with my notes working as "the picture on the box" as a guide. Each section of code gets written, then rewritten until it's all working to my satisfaction. Then working out from smaller pieces to larger ones I keep adding and modifying until the last piece drops in place and the program is complete.

    During the coding process, I don't care about the big picture --that's in my blueprint-- I only care about the problem before me ... open a disk file, draw a window, handle messages, calculate memory usage... whatever I'm working on at the moment is the only thing on my mind. If you paint all the small parts correctly the big picture will paint itself...

    Just like any other kind of project .... Understand -- Plan -- Do.
    Yeah alright I got it, thanks for the help i'll use your advice to finish this assignment I have.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  2. Help why this program stacking...
    By arnobie in forum C Programming
    Replies: 11
    Last Post: 10-19-2009, 11:35 AM
  3. Replies: 4
    Last Post: 11-05-2008, 11:57 PM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. Problem with keystroke stacking
    By Flakster in forum C++ Programming
    Replies: 13
    Last Post: 10-20-2005, 02:18 PM