Simple C Program - Evaluating the mean of exam results
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Simple C Program - Evaluating the mean of exam results
Dear all,
I am having a problem with a simple C program I have made (I am still very much a beginner), and it would be really kind of you if you could help me trace the error! Here is the code:
Code:
#include <stdio.h>
/* a program to work out the mean of your exam results */
main()
{
int total, exams_taken, number, quit;
printf ("End typing in your results by pressing the \"x\" key\n\n");
total = 0;
exams_taken = 0;
number = 0;
quit = "x";
while (number != quit) {
printf ("%d. Please input your exam result (out of 100):\n", exams_taken + 1);
scanf ("%d", &number);
total = total + number;
exams_taken = exams_taken + 1;
}
printf ("Your mean mark was: %d", (total / exams_taken)*100);
}
I have realized that actually the while statement has a serious fault - that when the end character (x) is typed, it will try adding that to the int "Total", which just wouldn't work. Anyway, the - current - problem is different: once the 'x' character is entered, the input disappears but the output of "Please input your exam result (out of 100):" continues on the loop. This shouldn't happen, but I can't see the error I've made....could anyone please help me? Thanks!
Your problem is scanf with "%d" is never going to except a char.
Your going to want to read in the numbers as strings. You can use strtol to convert the string to a long int for math purposes. You can then use strcmp to compare two strings (like the input string with "x").
Thanks JT! I've tried putting it together though, and it still seems to fail... I converted the 'number' int to a string, and I read up on strtol, but my usage of it must be wrong, because I get a compilation error. It tells me that strtol wants "const signed char *", which I'm pretty sure has something to do with pointers. Also, 'number' would need to be a variable, not a constant, for the program to work... Am I trying to run before I can walk? Anyhoo, could you please lead me step by step through the while statement? I'd really like to see how I could get this program up and running...
#include <stdio.h>
#include <stdlib.h>
/* a program to work out the mean of your exam results */
int main()
{
int total, exams_taken, number, quit;
char *tempstr = malloc(sizeof(char) * 10);
printf ("End typing in your results by pressing the \"x\" key\n\n");
total = 0;
exams_taken = 0;
number = 0;
quit = 0;
while (!quit) {
printf ("%d. Please input your exam result (out of 100):\n", exams_taken + 1);
scanf ("%10s", tempstr);
if (tempstr[0] == 'x') {
quit = 1;
} else {
number = strtol(tempstr,NULL,10);
total = total + number;
exams_taken = exams_taken + 1;
}
}
if (exams_taken > 0) {
printf ("Your mean mark was: %d", (total / exams_taken));
} else {
printf ("There were no scores\n");
}
free(tempstr);
return 0;
}
Ok, I didn't test this very extensively, and it is by no means bullet proof.
First off, I changed the meaning of quit. Consider it a boolean, if it is 0 we aren't going to quit, if it is 1 we will.
The read statement reads in at most a 10 character string (should be plenty big enough). If the first element of the string equals the character 'x' we set quit to 1 and that causes us to quit the loop. If it isn't, we convert it to a number with strtol an go back through the loop again.
After the loop exits, there is a check to make sure some scores were entered (don't want to divide by 0) and if so we do the final calculation and display it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.