Well, I've been reading your code and there are several problems. Here are the ones that I've found out:
- Return type of 'main' is not 'int'.
- In main: Unused variable 'count'
- Missing spacing and camel notation for function
- You're assuming to read string, but you are actually reading a char (e.g. the name, the creation of gender string, and so on).
- You're trying to insert a string, but reading a char, you pollute all your input buffer.
- You're using fflush(...). Please check here why you shouldn't use it: Using fflush(stdin)
- In order to use strings (or array of char) while reading dynamically names and assigning chars to an array of char, you need dynamic memory or at least VLA (in the following code, you will find the implementation with Dynamic Memory).
- In the 'percentage calculator' function you are assuming to take multiple values for each subject. Actually, you are taking only one mark for each subject.
- You are going wrong while reading a string. Here's how to properly do that:
How to read string from keyboard using C?
- You are trying to print a char instead of a string in the final print.
- Please do a better naming of your variables and your functions.
I'm gonna attach here a working code for your question:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define stringSize 256
char * readGender() {
fseek(stdin, 0, SEEK_END);
char * gender = (char *) malloc(stringSize);
char g;
if (!gender) exit(EXIT_FAILURE);
printf("\n >>> Enter your gender (M/F/T): ");
g = getchar();
if (g == 'M') {
strcpy(gender, "Male");
}else if(g == 'F') {
strcpy(gender, "Female");
}else {
strcpy(gender, "Transgender");
}
gender = (char *) realloc(gender, strlen(gender) + 1);
if (!gender) exit(EXIT_FAILURE);
return gender;
}
float percentageCalculator() {
float math, physics, chemistry, english, other, percent;
printf("\n >>> Enter math mark: ");
scanf("%f", &math);
printf("\n >>> Enter english mark: ");
scanf("%f", &english);
printf("\n >>> Enter physics mark: ");
scanf("%f", &physics);
printf("\n >>> Enter chemistry mark: ");
scanf("%f", &chemistry);
printf("\n >>> Enter additional subject mark: ");
scanf("%f", &other);
percent = ((math + english + physics + chemistry + other) / 500) * 100;
return percent;
}
int main() {
char * gender;
int age;
float percentage;
char *name = (char *) malloc(stringSize);
if (!name) exit(EXIT_FAILURE);
printf(">>> Enter your name: ");
fgets(name, sizeof(stringSize - 1), stdin);
name = (char *) realloc(name, strlen(name));
if (!name) exit(EXIT_FAILURE);
fseek(stdin, 0, SEEK_END);
printf("\n >>> Enter your age: ");
scanf("%d", &age);
gender = readGender();
percentage = percentageCalculator();
if (percentage < 33) {
printf("\n name : %s \n age : %d \n gender : %s \n Percentage : %f \n Status : Failed\n", name, age, gender, percentage);
} else if (percentage >= 33) {
printf("\n name : %s \n age : %d \n gender : %s \n Percentage : %f \n Status : Passed\n", name, age, gender, percentage);
} else {
printf("\n Error");
}
return 0;
}
Note that I've used fseek(...) as suggested here: How to clear input buffer in C? in order to read correctly char and integers together.
fseek(...) works on some systems; if not, then it is not surprising as nothing guarantees that it will work when standard input is an interactive device (or a non-seekable device like a pipe or a socket or a FIFO, to name but a few other ways in which it can fail).
If you need that it has to be portable, then check the link that I've placed before. Hope that it was helpful.
Next steps:
- Adding user input error handling
- Use a switch in 'readGender(...)' instead of the final triple if
Cheers, Denny