LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2005, 12:51 PM   #1
J_K9
Member
 
Registered: Nov 2004
Distribution: Slackware 11, Ubuntu 6.06 LTS
Posts: 700

Rep: Reputation: 30
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!

M.
 
Old 09-28-2005, 01:12 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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").
 
Old 09-28-2005, 01:25 PM   #3
J_K9
Member
 
Registered: Nov 2004
Distribution: Slackware 11, Ubuntu 6.06 LTS
Posts: 700

Original Poster
Rep: Reputation: 30
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...

Thanks again!

M.

Last edited by J_K9; 09-28-2005 at 01:28 PM.
 
Old 09-28-2005, 04:15 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Code:
#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.

Last edited by jtshaw; 09-28-2005 at 04:19 PM.
 
Old 09-29-2005, 02:30 AM   #5
J_K9
Member
 
Registered: Nov 2004
Distribution: Slackware 11, Ubuntu 6.06 LTS
Posts: 700

Original Poster
Rep: Reputation: 30
Thank you so much, jtshaw! That was really helpful!!

M.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is dual core worth it on regular apps? Simple SMP test results SteveSch Linux - Hardware 7 09-12-2005 12:36 AM
Exam results Boffy General 15 08-20-2004 01:13 PM
simple c program liguorir Linux - Software 4 05-29-2004 06:22 AM
conditional not evaluating correctly, help adelatorre204 Programming 4 04-11-2004 10:31 AM
Exam Results! Ahhhhhhhhhh! CragStar General 2 08-25-2001 06:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration