Code:
scanf("%d",&num);
char no[num][50],name[num][50],sur[num][50];
This is very illegal - the reason why is because if you are declaring
an array in C, the dimensions need to be constant at compile
time (static memory allocation). If you don't know the dimensions
at compile time, malloc must be used (the dynamic in dynamic
memory allocation), you cannot do it in this manner. Should look
like this:
char no[10][50];
Also I'm getting errors for not having all variable declarations
at the start of the code block - i thought that this was changed
in the last C standard, but in case it wasn't and Visual Studio
is correct, move those lines directly under main(){...
Code:
printf("ID: ");
scanf("%s",&no[k][0]);
scanf("%s",&name[k][0]);
scanf("%s",&sur[k][0]);
k++;
very wrong as well - scanf takes the memory address of where
it is reading in to, but when dealing with strings it should just
take the name of the string it is reading into. what you have
set it up to do is read it into the kth row of the 0th column - a
single element within the array. because scanf does't do
bounds checking it keeps on reading into the 1st, 2nd columns
and so on. Normally this would crash your program but since
you declared arrays it is operating, but its not working correctly
to say the least. these lines of code should be as follows:
Code:
printf("ID: ");
scanf("%s", no[k]); /*notice no &?*/
Ideally you should be using fgets to read in strings anyway.
>>my compiler did not encountered any errors
That surprises me - dev-cpp should throw a fit at the array declarations - too lazy to try, but trust me - that code was very
wrong. It wouldn't complain about not declaring variables at
the start of main because you probably have the file saved
as .cpp - in C++ it would be legal, but as I said it may/may not
be legal in C.
Also, do you realise that your case 2 does its work in a very
ugly way - printf can print whole strings using %s - you don't
need to loop and print every character individually
On another note, your code indentation is very inconsistent and
it makes it very awkward to read. To that end, I've beautified it
and reposted it below. I've made the changes that make it work
properly and introduced some improvements.
Code:
#include <stdio.h>
#include <stdlib.h>
#define NUM 5
/*The above allows for arrayname [NUM] because the program knows NUM is always 5*/
/*This means that the compiler will create an array of 5*/
int main(void)
{
int choice = 0;
int k = 0, l = 0;
int a1[NUM];
float res = 0;
char no[NUM][50], name[NUM][50], sur[NUM][50];
printf("\n 1. Student registration.\n"
" 2. List of course.\n"
" 3. Enter final results.\n"
" 4. Show average of final results.\n"
" 5. Exit\n");
while(choice!=5)
{
printf("your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
while(k<NUM)
{
printf("ID: ");
scanf("%s",no[k]);
printf("Name: ");
scanf("%s",name[k]);
printf("Surname: ");
scanf("%s",sur[k]);
k++;
}
break;
case 2:
printf("\n");
printf("ID\tName\tSurname\n");
do
{
printf("%s",no[l]);
printf("\t");
printf("%s",name[l]);
printf("\t");
printf("%s",sur[l]);
printf("\n");
l++;
}while(l<NUM); /*a for loop would work well here instead of do-while*/
l = 0; /*output is scrambled if you choose case 2 twice - this repairs it*/
break;
case 3:
for(l=0;l<NUM;l++)
{
printf("%s:",name[l]);
scanf("%d",&a1[l]);
}
l = 0; /*as above*/
break;
case 4:
for(k=0;k<NUM;k++)
{
res=res+a1[k];
}
k = 0; /*as above again*/
res=res/(float)NUM;
printf("average : %.2f\n",res);
res = 0.0; /*again as above*/
break;
case 5:
exit(1); /*this is a bit redundant since entering 5 should quit anyway*/
}
}
return 0;
}