473,324 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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!
Expand|Select|Wrap|Line Numbers
  1. while(ID<999)
  2.  
  3.  {
  4.           printf("\n\nEnter 3 digit student ID or 999 to end input: ");
  5.  
  6.           scanf("%d",&ID);
  7.             if (ID==999)
  8.                 break;
  9.          printf("Enter Quiz 1 Score (out of 10):  ");
  10.  
  11.           scanf("%d", &quiz1);
  12.  
  13.           printf("Enter Quiz 2 Score (out of 10): ");
  14.  
  15.           scanf("%d",&quiz2);
  16.  
  17.           printf("Enter Quiz 3 Score (out of 10): ");
  18.  
  19.           scanf("%d", &quiz3);
  20.  
  21.           printf("Enter Test 1 Score (out of 20): ");
  22.  
  23.           scanf("%d",&test1);
  24.  
  25.           printf("Enter Test 2 Score (out of 20): ");
  26.  
  27.           scanf("%d",&test2);
  28.  
  29.           printf("Enter Final Score (out of 30): ");
  30.  
  31.           scanf("%d", &final);
  32.  
  33.  
  34.  
  35.           total = quiz1+quiz2+quiz3+test1+test2+final;
  36.  
  37.           quiz = quiz1 + quiz2 + quiz3;
  38.  
  39.           test = test1 + test2;
  40.  
  41.           student=fopen(FILENAME, "w");
  42.           fprintf(student,"STUENT QUIZ MID_TERM FINAL GRADE\n"); 
  43.  
  44.           if (total<55 && total>0)
  45.  
  46.             t=0;
  47.           else
  48.              t= ((total-1)/10);
  49.           switch(t)
  50.             {
  51.                 case 10:  case 9:
  52.                     {
  53.                         printf("%d %5d %5d %5d \t A",ID, quiz, test, final);
  54.                         fprintf(student,"%d %5d %5d %5d \t A\n",ID, quiz, test, final);
  55.                     }
  56.                 break;    
  57.  
  58.                 case 8:
  59.                     {
  60.                         printf("%d %5d %5d %5d \t B",ID, quiz, test, final);
  61.                         fprintf(student,"%d %5d %5d %5d \t B\n",ID, quiz, test, final);
  62.                     }
  63.                 break;
  64.  
  65.                 case 7: case 6: case 5:
  66.                     {
  67.                         printf("%d %5d %5d %5d \t C",ID, quiz, test, final);
  68.                         fprintf(student,"%d %5d %5d %5d \t C\n",ID, quiz, test, final);
  69.                     }
  70.                 break;
  71.  
  72.                 case 0:
  73.                     {
  74.                         printf("%d %5d %5d %5d \t F\n",ID, quiz, test, final);
  75.                         fprintf(student,"%d %5d %5d %5d \t F\n",ID, quiz, test, final);
  76.                     }
  77.                 break;
  78.  
  79.                 default:
  80.                     printf("You have entered invalid information, please try again.");
  81.  
  82.  
  83.           }
  84.             fclose(student);
Feb 27 '08 #1
5 1633
whodgson
542 512MB
What type is t? It appears as though you expect it to be an integer of value between 10 and five.
Feb 27 '08 #2
sicarie
4,677 Expert Mod 4TB
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?
Feb 27 '08 #3
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.
Feb 27 '08 #4
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.
Feb 27 '08 #5
sicarie
4,677 Expert Mod 4TB
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.
Feb 27 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Poster | last post by:
I have a script, its outputs are in HTML. It displays perferctly in a browser, however when I view source, it gives me the JS scripts, but I want to view the HTML output. Is there a way I can view...
15
by: Mark C | last post by:
All, I have exhaustingly been looking through the newsgroups in search of a way to systemically output an Access 97 report to a pdf file using the full version of Adobe Acrobat. I want the user...
7
by: Xh | last post by:
Hi All, I have problems with generating valid HTML output there are few HTML elements that i don't what to output as <tagname/> but as <tagname></tagnamebut Xalan keeps generating them as...
0
by: Linda Liu[MSFT] | last post by:
Hi Al, Based on my understanding, you have an application which prints something to printers. The problem is that the font of "OCR A Extended" cannot be printed properly on some of printers. If...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.