4977 Shares

How do you get a for loop in C to repeat 8 times?

How do you get a for loop in C to repeat 8 times? Topic: How to write a while loop program
July 17, 2019 / By Edyth
Question: I have been struggling for days now to get this for loop to repeat 8 times. I am trying to get the "Enter lab score:" to display in the DOS window so I can enter a students lab score 8 times. Can someone tell me how to do this? This loop should also accumulate each lab score entered. void calcLabs(int *ptrLaccum) { int lab; int laccum=0; for (lab=0; lab <=8; lab++) { laccum=laccum + lab; printf("\n Enter lab score: ", lab); scanf("%d", &lab); } *ptrLaccum = laccum/8 *.4; return; } I should have added that the program should ask me for a new students name (up to 30) after I have entered 8 lab scores. When I input the following as you have suggested the program does not ask me for a new students name as it did before when I had my code above however it does repeat "Enter Lab Score:" 8 times now :) Any suggestions as to how I get it to go back to the mainline to ask for a new students name after I have entered 1 students 8 lab scores? for (int i=0; i<=8; i++) { printf("\n Enter lab score: "); scanf("%d", &lab); if (lab && lab > 0) laccum += lab; }
Best Answer

Best Answers: How do you get a for loop in C to repeat 8 times?

Charmaine Charmaine | 7 days ago
stdout is, on most implementations, line buffered. This means that everything is written to the screen when the '\n' character is read (or when you pass stdout to fflush). Be sure to check the return value of scanf. #include static int getints(int *dest, size_t size, const char *prompt) {         int tmp;                 while(size--) {                 if(prompt) {                         printf("%s", prompt);                         fflush(stdout);                 }                 if(scanf("%d", &tmp) != 1)                         return 1;                 *dest++ = tmp;         }         return 0; } static void printints(int *src, size_t size, const char *msg) {         if(msg)                 printf("%s", msg);         while(size--)                 printf("%d ", *src++);         printf("\n"); } #define ARRSIZE(a) (sizeof (a) / sizeof (a)[0]) int main(void) {         int labs[8];         if(getints(labs, ARRSIZE(labs), "Enter lab score: ")) {                 fprintf(stderr, "Error reading from stream\n");                 return 0;         }         printints(labs, ARRSIZE(labs), "You entered: ");         return 0; }
👍 102 | 👎 7
Did you like the answer? How do you get a for loop in C to repeat 8 times? Share with your friends

We found more questions related to the topic: How to write a while loop program


Charmaine Originally Answered: CPS or DCF procedure when they realize a repeat false caller?
What proof do you have that they know it is the same person? The reason I ask is per law, they are not allowed to inform anyone who made the complaint, unless they are court ordered. Or is the family assuming they know who the person is? EDIT: Here is the thing, even if it is the same person calling over and over again and the investigation comes up negative, that still does not prove that the caller does not believe it is not happening. There are many many times where CPS receives reports and the allegations are not true. The caller may truly believe what they are reporting is happening. I would highly doubt that CPS would do anything about this because if they do, can you imagine what would happen? That would mean everyone who thought or believed something illegal or harmful is being done to a child and it is not true, they will not report it because of possible getting in trouble. If the family this is happening to knows who it is, I would suggest they obtain an attorney, the attorney will obtain a search and seizure to determine who is making the reports anf could then file in small claims court harassment and possible pain and suffering.
Charmaine Originally Answered: CPS or DCF procedure when they realize a repeat false caller?
all allegations of child abuse/neglect to DCF must be investigated... no matter who is making the calls and no matter how many allegations have been proven false by previous investigations.

Arin Arin
void calcLabs(int *ptrLaccum) { int lab; int laccum=0; int n; for (n=0; n <8; n++) /* 0 to <8 will loop 8 times : 0 1 2 3 4 5 6 7*/ { printf("\n Enter lab score: ", lab); scanf("%d", &lab); laccum=laccum + lab; /* you want to add in the score AFTER it has a value */ } *ptrLaccum = laccum/8 *.4; return; }
👍 30 | 👎 1

Zebina Zebina
You cannot use your lab variable as both your for loop control variable AND a variable into which you you accept the user's input. As soon as you alter your lab variable by accepting the user's input, you've altered the lab variable used by the for loop's control. Instead of using the lab variable as you for loop control, use something like the following; for (int i=0; i<=8; i++) { printf("\n Enter lab score: "); scanf("%d", &lab); if (lab && lab > 0) laccum += lab; }
👍 24 | 👎 -5

Zebina Originally Answered: C++ Programming While Loop If Statements?
1) Read the chapter in the book that teaches you exactly how to do this. You'd be surprised how often reading the chapter and doing your own homework actually can teach you the correct answer! 2) You say "every code I have entered is wrong". How about posting some of the code you've come up with so far, and maybe someone can point out to you what you're doing wrong? That is, assuming you've actually entered code, and you're not just trying to pass your class without doing any work... 3) If you've read the chapter, but still don't know where to even begin, well, maybe you should find something a little different to learn...

If you have your own answer to the question how to write a while loop program, then you can write your own version, using the form below for an extended answer.