Hey Guys!
I have a weired one here, it must be a post-test loop, and here is how result has to look like:

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 100.0, please re-enter: 20
Enter mark#3: 5.5
Enter mark#4: 5
Enter mark#5: 10
Enter mark#6: 6
Enter mark#7: 7
000456789's total mark is: 63.5
Enter the Student's ID : 123654987
Enter mark#1: 1
Enter mark#2: 2
Enter mark#3: 3
Enter mark#4: 4
Enter mark#5: 5
Enter mark#6: 6
Enter mark#7: 7
123654987's total mark is: 28.0
Enter the Student's ID : 0
The average for the section is 45.75%
Enter the Section Code [0 to quit]: 5
Invalid value entered. Must be 0 to 4, please re-enter: 2
Enter the Student's ID: 987654
Enter mark#1: 7
Enter mark#2: 6
Enter mark#3: 5
Enter mark#4: 4
Enter mark#5: 3
Enter mark#6: 2
Enter mark#7: 1
000987654's total mark is: 28.0
Enter the Student's ID : 0
The average for the section is 28.00%
Enter the Section Code [0 to quit]: 0
The average for the course is 39.83%


i got everything working except looping the main sequence, take a look at my results:


Enter the Section Code:44

Invalid value entered. Must be 1 to 4, please re-enter:1


Enter Student's ID:11

Enter Mark#1:5
Enter Mark#2:5
Enter Mark#3:5
Enter Mark#4:5
Enter Mark#5:5
Enter Mark#6:5
Enter Mark#7:5
000000011's total mark is: 5.0

Enter Student's ID[0 to Quit]:0


Enter the Section Code[0 to quit]:3

Enter Mark#1:

everything works fine if i hit 0 at "Enter the Section Code[0 to quit]:"
but just like the log shows, when i hit anything but 0 it goes to "Enter Mark1:" instead of "Enter Students ID:", i just cant loop it properly.,
here comes my code a part of my main():

section=PreSecCode();
       student_id=PreStudentId();

do
{
       do
   {

       mark=Mark();
       PrintIt(student_id, mark);

   student_id=StudentId();
   }while(student_id!=0);

       section=SecCode();

}while(section!=0);

any help would be greatly appreciated!!
chheers

Dani AI

Generated

The immediate cause is control-flow order: the inner loop in the snippet calls the mark-reading function before it asks for the student ID, so when the outer loop repeats it re-enters the inner body at the mark prompt. A do/while runs the body once before checking the condition, so using a do/while in that position hides the student-id prompt on subsequent iterations. was right to mention sentinel logic, and was correct to ask for the helper-function code — the fix is to read the student ID at the top of the inner loop (or use a while loop), validate it, then read the marks.

A clearer, minimal structure (skeleton) that avoids the problem:

int section = getSectionCode();   /* validated 0..4 */
while (section != 0) {
    /* reset section accumulators here */
    while (1) {
        int student_id = getStudentId();    /* "Enter Student's ID [0 to quit]:" */
        if (student_id == 0) break;        /* end of this section */

        double student_total = 0.0;
        for (int i = 1; i <= 7; ++i) {
            double mark = getMark(i);      /* validate 0.0 - 100.0 inside getMark */
            student_total += mark;
        }
        printStudentTotal(student_id, student_total);
        /* update section/course sums and counts here */
    }
    printSectionAverage(/* section sums/counts */);
    section = getSectionCode();
}
printCourseAverage(/* course sums/counts */);

Practical tips and cautions:

  • Put the student-id read at the top of the inner loop (while is clearer than do/while here).
  • Reset per-section accumulators when a new section starts.
  • Watch stdin buffering: mixing scanf and fgets can skip inputs. Either use scanf with leading space format specifiers (e.g. " %d") or use fgets+sscanf for robust parsing.
  • Add temporary debug prints (section and student_id) if flow still looks wrong.
  • Posting the implementations of SecCode()/StudentId()/Mark() will reveal whether they consume extra characters or return unexpected values.

Fixing the order of reads and using a top-of-loop student-id check will match the intended prompts and sentinel behavior that described.

Recommended Answers

All 4 Replies

Member Avatar for Member #46692

Is your program accepting integers as well as strings(letters)?

Is your program accepting integers as well as strings(letters)?

integers only

Identify the portion of the code that isn't working and explain what it is supposed to do and I will gladly try to help you. Also, if we're supposed to identify your problem based on that 100 lines of input & output and that small segment of code, how are we going to do so if you didn't post the code for your functions that were called in that segment of code?

The problem is that your outer loop is checking for 0 to exit. So when you have an error condition you are exiting the loop. Is there a char you enter to exit the program? That's the value that you'll want to use in that loop. I think you'll want to do something like this...

while(c != SENTINAL){
   if(c == 0){
      PrintIt("error must be 1-4");
      continue;
   }
 //other loop code
}

SENTINAL is the value the user enters to exit your program

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.