Output not printing properly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chevon1920
    New Member
    • Feb 2008
    • 12

    Output not printing properly

    Hi, I just have a question for anyone who can help. I am trying to write a program that will print to a file. The program has a while loop so that when I run it, it should print to the file each time the loop cylces. It is only printing the 2nd to last cycle, which makes no sense to me. Everything else seem in order, here is my loop.

    I'm sorry I know I'm supposed to put it in some special format to post here, but I dont know how to do that. Thanks for any help!
    [code=c]
    while(ID<999)

    {
    printf("\n\nEnt er 3 digit student ID or 999 to end input: ");

    scanf("%d",&ID) ;
    if (ID==999)
    break;
    printf("Enter Quiz 1 Score (out of 10): ");

    scanf("%d", &quiz1);

    printf("Enter Quiz 2 Score (out of 10): ");

    scanf("%d",&qui z2);

    printf("Enter Quiz 3 Score (out of 10): ");

    scanf("%d", &quiz3);

    printf("Enter Test 1 Score (out of 20): ");

    scanf("%d",&tes t1);

    printf("Enter Test 2 Score (out of 20): ");

    scanf("%d",&tes t2);

    printf("Enter Final Score (out of 30): ");

    scanf("%d", &final);



    total = quiz1+quiz2+qui z3+test1+test2+ final;

    quiz = quiz1 + quiz2 + quiz3;

    test = test1 + test2;

    student=fopen(F ILENAME, "w");
    fprintf(student ,"STUENT QUIZ MID_TERM FINAL GRADE\n");

    if (total<55 && total>0)

    t=0;
    else
    t= ((total-1)/10);
    switch(t)
    {
    case 10: case 9:
    {
    printf("%d %5d %5d %5d \t A",ID, quiz, test, final);
    fprintf(student ,"%d %5d %5d %5d \t A\n",ID, quiz, test, final);
    }
    break;

    case 8:
    {
    printf("%d %5d %5d %5d \t B",ID, quiz, test, final);
    fprintf(student ,"%d %5d %5d %5d \t B\n",ID, quiz, test, final);
    }
    break;

    case 7: case 6: case 5:
    {
    printf("%d %5d %5d %5d \t C",ID, quiz, test, final);
    fprintf(student ,"%d %5d %5d %5d \t C\n",ID, quiz, test, final);
    }
    break;

    case 0:
    {
    printf("%d %5d %5d %5d \t F\n",ID, quiz, test, final);
    fprintf(student ,"%d %5d %5d %5d \t F\n",ID, quiz, test, final);
    }
    break;

    default:
    printf("You have entered invalid information, please try again.");


    }
    fclose(student) ;[/code]
    Last edited by sicarie; Feb 27 '08, 03:40 AM. Reason: Code tags
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    What type is t? It appears as though you expect it to be an integer of value between 10 and five.

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      chevon1920-

      I would make sure it is entering the loop (right in front of the while, set id so that it will definitely enter the loop) which will make sure the loop itself is only printing twice.

      I'm pretty rusty on my C, but did you try flushing stdin?

      Comment

      • chevon1920
        New Member
        • Feb 2008
        • 12

        #4
        Originally posted by whodgson
        What type is t? It appears as though you expect it to be an integer of value between 10 and five.
        thanks for the reply,
        I defined everything as an integer. And yes it should be between 10 and 5.

        Comment

        • chevon1920
          New Member
          • Feb 2008
          • 12

          #5
          Originally posted by sicarie
          chevon1920-

          I would make sure it is entering the loop (right in front of the while, set id so that it will definitely enter the loop) which will make sure the loop itself is only printing twice.

          I'm pretty rusty on my C, but did you try flushing stdin?
          I didnt think I needed an initial value for ID. Do you always need one, Its not going to a certain value, I was trying to set up and infinite loop. LOL, I have no clue what flushing stdin means, but Ill figure it and try it, thanks.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by chevon1920
            I didnt think I needed an initial value for ID. Do you always need one, Its not going to a certain value, I was trying to set up and infinite loop. LOL, I have no clue what flushing stdin means, but Ill figure it and try it, thanks.
            Infinite loops, in general, are bad, but you can set them up very easily. In this case I'm sure you know that they are in the following format:

            while ( condition )
            {
            do something
            }

            So all you really want is for condition to register as "true". In C and C++, this is pretty funny, it can be either a conditional statement (as you have a comparison, if ID is less than 999), or it can be a boolean - true or false. The funny thing about values and how they are read in conditional expressions is that 0 is false and any other number is true. This means an infinite loop is as easy as

            while (1)

            Simple, true, and will execute until your scanf() statement is read and breaks out from it.

            Comment

            Working...