Problem with the loop

I need to write a program so the output will become like this:

How many students? 3
Student 1 mark : 987
INVALID
Student 1 mark : 25
Student 2 mark : 90
Student 2 mark : 65
Total pass: 2
Total fail: 1
The average mark is 60

I just made the source code but I stuck because the infinite loop..Here's my source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
]#include <stdio.h>
int main()
{ int N,x=1,pass=0,fail=0,mark;
float avrgmark;

do
{printf("How many Students?\a");
scanf("%d",&N);
}while (x>N);

while(x<=N)
 {
      printf("Student %d mark:",x);
      scanf("%d",&mark);x++;
while(mark<0 || mark>100)       
             printf("INVALID\n");
        //printf("Student %d mark:",x);
      //scanf("%d",&mark);//x++;
      }
      



system ("pause");
return 0;}
Last edited on
I have a suggestion. Use for loop instead.

for(int i = 0; i < N; i++)
{

}

Also use the for loop for printing the data out!

Wish you best luck
next time use Code tag to input code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
int main()
{ int N,x=1,pass=0,fail=0,mark;
float average=0;

do
{printf("How many Students?\a");
scanf("%d",&N);
}while (x>N);

while(x<=N)
{
printf("Student %d mark:",x);
scanf("%d",&mark);
if(mark<0 || mark>100) 
printf("INVALID\n");
else 
{x++;
average+=mark;
if(marks<33)
fail++;
else
pass++;
}
}
average/=N;
system ("pause");
return 0;}
Last edited on
Ok..I will try..thank you..
Last edited on
There are no infinite loops in this program.I don't know how you got stuck!
(Maybe you think it as infinite because you are not outputting anything on screen)
owh. I see. Thanks for helping me. My source code just now print INVALID continuously. Is it because I use the while loop?
Last edited on
I do not use printf and scanf and know very little about it.I use simple cin and cout.But printing INVALID continuously is not the preoblem with while loop.I think scanf do not take input.It just scans input stream.If so then you need to clear input buffer before using scanf.
@Nur Zafri
Welcome to the forum. Here are a few tips:

1- Do not title threads "need help"... Be more descriptive.
2- Do not use text message spellings. This is forum for learning and discussion, not a teenager chatroom service.
3- On the subject:

It is very easy to make a while loop malfunction, if you do not understand how C++ logic statements work. Like right there:

1
2
3
4
5
6
7
8
9
while(x<=N)
 {
      printf("Student %d mark:",x);
      scanf("%d",&mark);x++;
while(mark<0 || mark>100)       
             printf("INVALID\n");
        //printf("Student %d mark:",x);
      //scanf("%d",&mark);//x++;
      }


This appears to be a nested while loop. The inner loop, while(mark<0 || mark>100) would keep looping forever unless the break statement is used. In fact, I don't think there should be a control statement here. Use conditional (if...else) statement instead.

Edit: I might add that system("pause") is strongly advised against. Here: http://www.cplusplus.com/forum/articles/11153/
I would suggest that you use a hanging scanf() at the end, instead of system("pause")...
Last edited on
I see. Now I understand why it keeps looping forever. Thanks for your guidance and I apologize for my mistakes.
Last edited on
@ Nur
In program I posted above you forgot to see if instead of while?
Last edited on
I tried if but not work. Maybe something wrong with my source code. Then, I compared mine with your source code. Now, I know all the mistakes I have done. Thank you so much akshit.
Last edited on
Topic archived. No new replies allowed.