struct NamesAtt
{
	char student_Surname[20];
	char student_givenName[50];
	char gender[100];
}Summary;

...


void Students_Absence_Report()
{
	system("cls");
	printf("Student Presence/Absence Report\n");
	printf("===============================\n");
	printf(" Name \t\t%%Presence   %%Absence\n");

	for(i=0; i<student; i++)
		printf("(%d) %s %s\t %.2f\t %.2f\n", i+1, Summary.student_Surname[i], Summary.student_givenName[i], Presence_percentage[i], 100.00-Presence_percentage[i]);
	printf("\n");
	printf("------- END OF ATTENDANCE REPORT -------\n\n");
	system("pause");
	system("cls");
}

void Barred_Students_List()
{
	system("cls");
	printf("Students BARRED from the Examination\n");
	printf("====================================\n");

	for(i=0; i<student; i++)
	{
		if(Presence_percentage[i] < 70.00)
		{
			if(Summary.gender[i] == 'M')
			{
				printf("Mr. %s", Summary.student_Surname[i]);
				printf(" %s\n", Summary.student_givenName[i]);
			}

			else if(Summary.gender[i] == 'F')
			{
				printf("Ms. %s", Summary.student_Surname[i]);
				printf(" %s\n", Summary.student_givenName[i]);
			}
		}
		Number_Barred++;
	}
	printf("Total Number of Students Barred = %d\n", Number_Barred);
	printf("\n");
	printf("------- END OF BAR LIST -------\n\n");
	system("pause");
	system("cls");
}

what happen to this two functions ??
i'll get error when i execute it.. if i have enter values..
and i also got tried i dont enter any values in.. it will execute completely..

Recommended Answers

All 23 Replies

I can't see anything wrong with those two functions (well, nothing logically wrong anyway). You'll need to provide some more context.

How is the student list getting populated and how are these functions being called?

I can't see anything wrong with those two functions (well, nothing logically wrong anyway). You'll need to provide some more context.

How is the student list getting populated and how are these functions being called?

i copy all of the codes to let you see..

#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>

struct NamesAtt
{
	char student_Surname[20];
	char student_givenName[50];
	char gender[100];
}Summary;

int Attendance[100][100] = {0};
int studPresence[100] = {0}, weekPresence[100] = {0};
int i, x;
int Number_Barred = 0;
int week, student;
double Presence_percentage[100];
char SubCode[30];

void InputFunction();
void OutputFunction();
void Report_on_Attendance_for_Each_Week();
void Report_on_Attendance_for_Each_Student();
void Students_Absence_Report();
void Barred_Students_List();

void main()
{
	int answer;

	system("cls");
	while(answer != 5 || answer > 5)
	{
		printf("(1) To Input Student's Information\n");
		printf("(2) To Process Student Attendance\n");
		printf("(3) To Process Attendance Summary\n");
		printf("(4) To View Simple Student Attendance (Table Form)\n");
		printf("(5) Quit\n");
		printf("Please Type And Enter\n");
		scanf("%d", &answer);
		

		if(answer == 1)
			InputFunction();

		else if(answer == 2)
		{
			system("cls");
			while(answer != 0 || answer > 3)
			{
				printf("(1) To View Student Attendance\n");
				printf("(2) To View Week Attendance\n");
				printf("(3) Back\n");
				printf("Please Type And Enter\n");
				scanf("%d", &answer);
				
				if(answer == 1)
					Report_on_Attendance_for_Each_Week();

				else if(answer == 2)
					Report_on_Attendance_for_Each_Student();
				else if(answer == 3)
					break;
				else
				{
					printf("You Have Enter Out of Range\n");
					printf("Please Re-enter Again\n");
				}
			}
		}
		
		else if(answer == 3)
		{
			system("cls");
			while(answer != 0 || answer > 3)
			{
				printf("(1) To View Student Presence/Absence Report\n");
				printf("(2) To View Student Barred List\n");
				printf("(3) Back\n");
				printf("Please Type And Enter\n");
				scanf("%d", &answer);

				if(answer == 1)
					Students_Absence_Report();
				
				else if(answer == 2)
					Barred_Students_List();
				
				else if(answer == 3)
					break;
				
				else
				{
					printf("You Have Enter Out of Range\n");
					printf("Please Re-enter Again\n");
				}
			}
		}

		else if(answer == 4)
			OutputFunction();
		
		else if(answer == 5)
			exit(1);

		else
		{
			printf("You Have Enter Out of Range\n");
			printf("Please Re-enter Again\n");
		}
	}
}

void InputFunction()
{
	system("cls");
	fflush(stdin);
	printf("What is your Course Code? ");
	scanf("%s", SubCode);
	fflush(stdin);

	printf("How Many Weeks? ");
	scanf("%d", &week);
	while(isalpha(week))
	{
		printf("You Have Enter Out of Range\n");
		printf("Please Re-enter Again\n");
		printf("How Many Weeks? ");
		scanf("%d", &week);
	}

	printf("How Many Students In This Class? ");
	scanf("%d", &student);
	while(isalpha(student))
	{
		printf("You Have Enter Out of Range\n");
		printf("Please Re-enter Again\n");
		printf("How Many Students In This Class? ");
		scanf("%d", &student);
	}

	for(i=0; i<student; i++)
	{
		system("cls");
		printf("STUDENT %d\n", i+1);
		printf("==========\n");

		printf("Student SurName: ");
		scanf("%s", &Summary.student_Surname[i]);
		fflush(stdin);

		while(!isalpha(Summary.student_Surname[i]))
		{
			printf("You Have Enter Out of Range\n");
			printf("Please Re-enter Again\n");
			printf("Student SurName: ");
			scanf("%s", &Summary.student_Surname[i]);
		}
		fflush(stdin);

		printf("Student GivenName: ");
		scanf("%s", &Summary.student_givenName[i]);

		while(!isalpha(Summary.student_givenName[i]))
		{
			printf("You Have Enter Out of Range\n");
			printf("Please Re-enter Again\n");
			printf("Student GivenName: ");
			scanf("%s", &Summary.student_givenName[i]);
		}

		fflush(stdin);
		printf("Student Gender <M = Male; F = Female>: ");
		scanf("%c", &Summary.gender[i]);
		fflush(stdin);

		while(Summary.gender[i] != 'M' && Summary.gender[i] != 'F')
		{
			printf("You Have Enter Out of Range\n");
			printf("Please Re-enter Again\n");
			printf("Student Gender <M = Male; F = Female>: ");
			scanf(" %c", &Summary.gender[i]);
		}

		for(x=0; x<week; x++)
		{	
			
			printf("Present in Week %d? <1 = Yes; 0 = No> ",x+1);
			scanf("%d", &Attendance[x][i]);
			while(Attendance[x][i] != 1 && Attendance[x][i] != 0)
			{
				printf("You Have Enter Out of Range\n");
				printf("Please Re-enter Again\n");
				printf("Present in Week %d? <1 = Yes; 0 = No> ",x+1);
				scanf("%d", &Attendance[x][i]);
			}
		}
	}

	for(i=0; i<student; i++)
	{
		studPresence[i] = 0;	
		for(x=0; x<week; x++)
		{
			studPresence[i] += Attendance[x][i];
			Presence_percentage[i] = studPresence[i] / week * 100;
		}
	}

	for(x=0; x<week; x++)
	{
		weekPresence[x] = 0;
		for(i=0; i<student; i++)
			weekPresence[x] += Attendance[x][i];
	}

	printf("\n");

}

void Report_on_Attendance_for_Each_Week()
{
	system("cls");
	printf("Attendance for each Week\n");
	printf("========================\n");

	for(x=0; x<week; x++)
		printf("Week No %d : %d students present\n", x+1, weekPresence[x]);
	printf("---- END OF REPORT -----\n\n");
	system("pause");
	system("cls");
}

void Report_on_Attendance_for_Each_Student()
{
	system("cls");
	printf("Student Attendance - %s\n", SubCode);
	printf("=============================\n");

	for(i=0; i<student; i++)
		printf("Student %d : %d days present\n", i+1, studPresence[i]);
	printf("------- END OF REPORT -------\n\n");
	system("pause");
	system("cls");
}

void OutputFunction()
{
	system("cls");
	printf("\t\tOutput Display In Table Form\n");
	printf("\t\t============================\n");
	printf("\t\tRow = Weeks\tColumn = Student\n\n");

	printf("\t\t\t\tWeek Presence\n");
	for(x=0; x<week; x++)
	{
		printf("\t\t| ");
		for(i=0; i<student; i++)
			printf("%d |", Attendance[x][i]);
		printf("\t| %d |\n", weekPresence[x]);
	}
	
	printf("\n");
	printf("Student Presence| ");
	for(i=0; i<student; i++)
		printf("%d |", studPresence[i]);
	printf("\n\n");

	system("pause");
	system("cls");

}

void Students_Absence_Report()
{
	system("cls");
	printf("Student Presence/Absence Report\n");
	printf("===============================\n");
	printf(" Name \t\t%%Presence   %%Absence\n");

	for(i=0; i<student; i++)
		printf("(%d) %s %s\t %.2f\t %.2f\n", i+1, Summary.student_Surname[i], Summary.student_givenName[i], Presence_percentage[i], 100.00-Presence_percentage[i]);
	printf("\n");
	printf("------- END OF ATTENDANCE REPORT -------\n\n");
	system("pause");
	system("cls");
}

void Barred_Students_List()
{
	system("cls");
	printf("Students BARRED from the Examination\n");
	printf("====================================\n");

	for(i=0; i<student; i++)
	{
		if(Presence_percentage[i] < 70.00)
		{
			if(Summary.gender[i] == 'M')
			{
				printf("Mr. %s", Summary.student_Surname[i]);
				printf(" %s\n", Summary.student_givenName[i]);
			}

			else if(Summary.gender[i] == 'F')
			{
				printf("Ms. %s", Summary.student_Surname[i]);
				printf(" %s\n", Summary.student_givenName[i]);
			}
		}
		Number_Barred++;
	}
	printf("Total Number of Students Barred = %d\n", Number_Barred);
	printf("\n");
	printf("------- END OF BAR LIST -------\n\n");
	system("pause");
	system("cls");
}
void main()

Should be:

int main(void)

and you need to return 0 at the end.

while(answer != 5 || answer > 5)

Why the second condition? If it's > 5 then it's != 5.

The same goes for the while loops within the following if/else block.

void main()

Should be:

int main(void)

and you need to return 0 at the end.

while(answer != 5 || answer > 5)

Why the second condition? If it's > 5 then it's != 5.

The same goes for the while loops within the following if/else block.

dude..
that is the same result if i change the void main into int main(void);
and also the condition.. if i never put != 5, it can't show me the choices...

dude..
i solve the problem about the two function already..
which is i put something wrong at the input and also the output..
since if

char x[10]

is consider as a string type.. is not a Array type..
so i change my Summary to Summary[100], make it as array type..

now i got another problem which is my error message problem...

printf("Student SurName: ");
		scanf("%s", &Summary.student_Surname[i]);
		fflush(stdin);

		while(!isalpha(Summary.student_Surname[i]))
		{
			printf("You Have Enter Out of Range\n");
			printf("Please Re-enter Again\n");
			printf("Student SurName: ");
			scanf("%s", &Summary.student_Surname[i]);
		}
		fflush(stdin);

		printf("Student GivenName: ");
		scanf("%s", &Summary.student_givenName[i]);

		while(!isalpha(Summary.student_givenName[i]))
		{
			printf("You Have Enter Out of Range\n");
			printf("Please Re-enter Again\n");
			printf("Student GivenName: ");
			scanf("%s", &Summary.student_givenName[i]);
		}

how can i display the error message when the user input non-alphabets.?
i tried to put isalpha(ch) statement, but it keep show me the error message whatever i have type.
and also this

printf("Student Gender <M = Male; F = Female>: ");
		scanf("%c", &Summary.gender[i]);
		fflush(stdin);

		while(Summary.gender[i] != 'M' && Summary.gender[i] != 'F')
		{
			printf("You Have Enter Out of Range\n");
			printf("Please Re-enter Again\n");
			printf("Student Gender <M = Male; F = Female>: ");
			scanf(" %c", &Summary.gender[i]);
		}

do you have any idea can make this error appear when the word is more than 1.
i also have tried use strlen(ch), but it have errors..
by the way.. thanks first.:)

scanf("%s", p) reads a string int p, not a character.
If you want to check the surname for non-alphabetic characters, read the whole string into Summary.student_Surname, then loop over it character by character looking for non-alphabetic ones.

Leaving aside why gender is 100 characters long, you want to read gender into Summary.gender[0] if you just want 1 character. As it is, you are reading it into Summary.gender, which could be anywhere because the i variable is used all over the place.

scanf("%s", p) reads a string int p, not a character.
If you want to check the surname for non-alphabetic characters, read the whole string into Summary.student_Surname, then loop over it character by character looking for non-alphabetic ones.

Leaving aside why gender is 100 characters long, you want to read gender into Summary.gender[0] if you just want 1 character. As it is, you are reading it into Summary.gender, which could be anywhere because the i variable is used all over the place.

oh..
the summary.gender[100] is before i solve my function problem..
now i have been that problem so it should be summary[100].gender
yeah.. i can check the 1st character.. but i want check another thing which is the gender length.. if the gender length over 1 then display the error message..

OK, so you want

scanf("%s\n", Summary.gender);

Then you can do

strlen(Summary.gender)

It should have been summary.Gender. array of struct to be declared summary[3] or any thing.

OK, so you want

scanf("%s\n", Summary.gender);

Then you can do

strlen(Summary.gender)

yeah..
i tried

while(strlen(Summary[i].gender) > 1);

but it giving me errors, if i combine with my previous code there by using 'OR' operator.

while(Summary[i].gender != 'F' && Summary[i].gender != 'M' || strlen(Summary[i].gender) > 1)

It should have been summary.Gender. array of struct to be declared summary[3] or any thing.

yeah..
i have declared my Summary[100]..
but i just want to check whether the user input is more than 1 alphabet/character or not.
if it is over 1 alphabet i'll show him/her an error message and re-type again.
according to my question of the gender input, i already write there it allow M or F only.. so i have success to check the alphabet of M and F, so now i only left , is it more than 1 alphabet/character or not..

just now i have been tried this way as what martin said..
by using loop to check one by one..

for(count=0; count<<strlen(Summary[i].student_Surname); count++)
		while(!isalpha(Summary[i].student_Surname[count]));
		{
			printf("You Have Enter Out of Range\n");
			printf("Please Re-enter Again\n");
			printf("Student SurName: ");
			scanf("%s", &Summary[i].student_Surname);
		}

it will always show me the error message whatever is the first input..
and i dont understand about my for loop..
if i put only one " < " , it gave me a warning which is "signed/unsigned mismatch"
if i put 2 of " < " only can success to execute.. why? :-O

strlen returns an unsigned integer (because the length of a string can hardly be negative), so you want to use one too.

unsigned int count;

Then use the one <.

strlen returns an unsigned integer (because the length of a string can hardly be negative), so you want to use one too.

unsigned int count;

Then use the one <.

dude..
im sorry that i cannot fully understand what you trying to explain..
but any way.. thanks alot ..
i'll ask my sir.. i think face to face explain is more easier to understand. :idea:
so how about my while-loop?
it always show me the error message in the first input.. no matter what we input in the first time. :X

Your loops look inside-out to me. You want to loop looking for good input, and then within that use a loop to check the string:

unsigned int good = 0;
while (!good) {
    /* Get input */
    /* Check the string */
    if (/* It's not valid */) {
        /* Print error message */
    }
    else {
        good = 1;
    }
}

And before you check the string, print it, just to make sure it is being scanned correctly.

I didnt unterstand one thing gender being a string how can u compare it with char 'f'
"while(Summary.gender != 'F' && Summary.gender != 'M' || strlen(Summary.gender) > 1)" - here
it shld be
while(srtcmp(Summary.gender,"F" > 0)
or
while(Summary.gender != 'F' && Summary.gender != 'M' || strlen(Summary.gender) > 1)

just check this code-

struct str
{
 char gen[100];
};
struct str str1;
void main()
{

  gets(str1.gen);
  if(str1.gen[0]=='m')
  {
   printf("Correct");
  }

}

lolz~TARCian...same assignment with me >.<

all of the problem have been solved by own way already..
thanks for the helping friends. (:

lolz~TARCian...same assignment with me >.<

lol.. @@
who you are ? :-O

DIT1 student..

DIT1 student..

lol
.. i dont know eh.. =x
but now my problem have been solved, so i won't post any reply at here..
any problem go facebook find me or message me at here. :)

> that is the same result if i change the void main into int main(void);

So not to let this point go unchecked, void should never be used as the return of a C program's main function. If you are learning C, and your intention is good programming techniques, don't use it. It may have the same results to you, but the question is more about code portability and good practice. The return value of a void main is undefined yet the operating system requires some kind of return code, and you never want to have "undefined behaviour" in a program you write, it can lead to unexpected consequences, if not to you, to someone else in the future using your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.