1. ## array

when i put name[30].. what it actually means?the 30.. izzit i can have 31 character inside it?.. if so when i type name like name: john ian something then the then row which matrix needed 2 be key in will be skipped..

EG normal case:
Name: John
Matrix: bk110110
sex: Male

ABnormal case:
Name: John ian ian
Matrix: Sex:Male

i cant key in the matrix

2. name[30] means you have declared an array with a length of 30. If this array is of type char, then it can hold 30 characters - not 31.

I'm not sure I understand the second part of your question. Could you post some code which demonstrates the problem you are having?

3. Originally Posted by bithub
name[30] means you have declared an array with a length of 30. If this array is of type char, then it can hold 30 characters - not 31.

I'm not sure I understand the second part of your question. Could you post some code which demonstrates the problem you are having?
Code:
```#include<stdio.h>

void writeoutput(int i);

typedef struct {
char matrix[30];
char name[30];
int  age;
char sex[4];
} record;

record students[10];

main(void)
{
int i, n, j, average;
printf("how many student");
scanf("%d", &n);

for (i = 0; i < n; ++i){
}

{
printf("\nStudents no. %d\n", i+1);
printf("	Name	");
getchar(students[i]);
printf("	Matrix	");
scanf("%s", students[i].matrix);
printf("	sex	");
scanf("%s", &students[i].sex);
printf("	Age	");
scanf("%d", &students[i].age);
return;
{```
if i have spacing.. then the second input(matrix) will be skipped

4. No, it means you can have 29 characters plus the terminating zero. If you have 31 characters, you have gone 2 characters over.

The other part of the problem is that scanf() stops as soon as it sees a blank/newline/tab (or other "whitespace").
There are two possible solutions:
Change the format string of your "%s" to "%29[^\n]" - that means "accept up to 29 of the characters specified in the brackets. The ^ means "invert", so we are basically saying "accept any input except (or up to) newline".

The second option is to use fgets(string, size, stdin) - this reads a line of text, and you can use sizeof or some such to to define "size" - it will not read beyond size. You will have to remove the newline at the end of the string tho', as fgets() reads UP AND INCLUDING the newline. scanf() leaves newlines until the next scanf() - which also means you have to get rid of the OLD newline that is from the previous scanf() before using fgets().

You can check the FAQ for "how to clear the input buffer".

--
Mats

5. Originally Posted by matsp
No, it means you can have 29 characters plus the terminating zero. If you have 31 characters, you have gone 2 characters over.

The other part of the problem is that scanf() stops as soon as it sees a blank/newline/tab (or other "whitespace").
There are two possible solutions:
Change the format string of your "%s" to "%29[^\n]" - that means "accept up to 29 of the characters specified in the brackets. The ^ means "invert", so we are basically saying "accept any input except (or up to) newline".

The second option is to use fgets(string, size, stdin) - this reads a line of text, and you can use sizeof or some such to to define "size" - it will not read beyond size. You will have to remove the newline at the end of the string tho', as fgets() reads UP AND INCLUDING the newline. scanf() leaves newlines until the next scanf() - which also means you have to get rid of the OLD newline that is from the previous scanf() before using fgets().

You can check the FAQ for "how to clear the input buffer".

--
Mats
thx Change the format string of your "%s" to "%29[^\n] <-- this thing works..

btw can u tell me why i cant put %s? izzit %s a string?

6. Code:
```#include<stdio.h>
#include<string.h>

void writeoutput(int i);

typedef struct {
char name[80];
char matrix[80];
int  age;
char sex[6];
} record;

record students[30];

main(void)
{
int i, n, maleaverage, femaleaverage,malesum = 0, femalesum = 0, mcount = 0, fcount = 0, bfcount = 0, bmcount = 0;
printf("how many student");
scanf("%d", &n);

for (i = 0; i < n; ++i) {
}

for (i = 0; i < n; ++i)
{
if (strcmp(students[i].sex, "male") == 0){
malesum = malesum + students[i].age;
++mcount;
}

if (strcmp(students[i].sex, "female") == 0){
femalesum += students[i].age;
++fcount;
}

}

maleaverage = malesum/mcount;
femaleaverage = femalesum/fcount;

for(i = 0; i < n; ++i){
if (strcmp(students[i].sex, "male") == 0){
if ( students[i].age < maleaverage){
++bmcount;
}
}
if (strcmp(students[i].sex, "female") == 0){
if (students[i].age < femaleaverage){
++bfcount;
}
}
}

for (i = 0; i < n; ++i){
writeoutput(i);
}

printf("\n Average male age %d",maleaverage);
printf("\n Below Average of male age have %d students", bmcount);
printf("\n Average female age %d\n", femaleaverage);
printf("\n Below Average of female age have %d students\n", bfcount);

}

{
printf("\nStudents no. %d\n", i+1);
printf("	Name	");
scanf(" %29[^\n]", students[i].name);
printf("	Matrix	");
scanf("%s", students[i].matrix);
printf("	sex	");
scanf("%s", &students[i].sex);
printf("	Age	");
scanf("%d", &students[i].age);
return;
}

void writeoutput(int i)
{
printf("\nName	%s", students[i].name);
printf("\n Matrix %s", students[i].matrix);
printf("\n Sex %s", students[i].sex);
printf("\n Age %d\n", students[i].age);
return;
}```
my exe crashed when i only enter 1 type of sex..i think is becuz of

Code:
```printf("\n Average male age %d",maleaverage);
printf("\n Below Average of male age have %d students", bmcount);
printf("\n Average female age %d\n", femaleaverage);
printf("\n Below Average of female age have %d students\n", bfcount);```
how do i modified this part into
Code:
```if(strcmp(students[i].age, "female) == 0)
rintf("\n Average female age %d\n", femaleaverage);
printf("\n Below Average of female age have %d students\n", bfcount);```
[/CODE]

but i dont want use the for( i = 0; i < n; ++i) because i only want print out 1 general average.. i dun wan every students oso have 1 average.. so what shud i do?

7. just check if fcount/mcount is zero before doing the calculation.

As to why %s doesn't work: it's part of how scanf is designed. It reads a string TO THE FIRST WHITESPACE (space, tab, newline, newpage).

--
Mats

8. Originally Posted by matsp
just check if fcount/mcount is zero before doing the calculation.

As to why %s doesn't work: it's part of how scanf is designed. It reads a string TO THE FIRST WHITESPACE (space, tab, newline, newpage).

--
Mats
ic.. so how do i set the %29[^\n] to infinite length?

9. Originally Posted by archriku
ic.. so how do i set the %29[^\n] to infinite length?
But you don't have an infinite length buffer!
Are you trying to create a buffer overrun?