tags:

views:

210

answers:

4

My assignment is to fix the code. I have my edited code below and the original code below that. I figure I still have a few errors in here. My error checking doesnt seem to work, and I am not sure if my getchar() function is written or working properly.

Please assume I know nothing becasue that is fairly accurate.

The code compiles, but the answer is always 2. I am about 4 hours into this piece of code with 3 more to work after this.

My code

#include <stdio.h> 

double get_number(double num);

main () {
  double n1,n2,n3;
  double average;

  printf("\nCompute the average of 3 integers\n");
  printf("--------------------------------\n");
  n1 = get_number(1);
  n2 = get_number(2);
  n3 = get_number(3);
  average = (n1 + n2 + n3)/3;
  printf("The average is %0.2f\n",average);
}

double get_number(double num) { 
  double value = 0;
  char c;
  int i;
  printf("Please input number %d: ", num);

  while (c = getchar != '\n') { 
    if ( (c>9) || (c<0) ) { 
      printf("Incorrect character entered as a number - %c\n",c);
      return(0);
    }
    else {
    value = num;
    }
  }
  return(value);
}

Original code

#include <stdio.h> 

main () {
  double n1,n2,n3;
  double average;

  printf("\nCompute the average of 3 integers\n");
  printf("--------------------------------\n");
  n1 = get_number(1);
  n2 = get_number(2);
  n3 = get_number(3);
  average = (n1 + n2 + n3)/3;
  printf("The average is %0.2f\n",average);
}

double get_number(int num) { 
  double value = 0;
  char c;
  printf("Please input number %d: ", num);
  while (c = getchar() != '\n') { 
    if ( (c<=9) && (c>=0) ) { 
      printf("Incorrect character entered as a number - %c\n",c);
      exit(-1);
    }
    else {
      value = 10*value + c - '0';
    }
  }
  return(value);
}
+3  A: 

A few issues:

1. You should be using '9' and '0', since you want the ASCII values for digit '9' (0x39) and '0' (0x30), not 0x9 and 0x0.

if ( (c>'9') || (c<'0') ) { 

2. != has higher precedence than =, so you need parens. Learn operator precedence, and if you're in doubt, use parens:

3. getchar is a function not a variable.

while ((c = getchar()) != '\n') { 

4.  You use the wrong conversion. num is a double, so you would need %f. Or, you could make num a int.

printf("Please input number %f: ", num);

5. You never actually use c in any way (except error checking). You always return 0 or num (see your else clause), which makes no sense. The else body of the original is correct.

Matthew Flaschen
To make this more explicit, Matthew means use `'0'` (value of ASCII character '0') instead of the number `0`.
DarkDust
ok, i understand that
pisfire
The code with `(c = getchar != '\n')` compiles because that spelling of `getchar` is not an invocation of the `getchar()` macro in the `<stdio.h>` header (because it isn't followed by an open parenthesis), so it must be the name of the function that is also required, and the expression as a whole is checking whether the pointer to function is equal to a newline (it isn't!), and assigning the boolean value of the comparison to `c`. The compiler should be having fits about it if warnings are enabled - but it 'compiles'.
Jonathan Leffler
+1  A: 

You got the floating point parsing all wrong and shouldn't be doing it yourself. There's an easier way:

double get_number(double num) { 
  double value = 0.0;
  printf("Please input number %lf: ", num);
  scanf("%lf", &value);
  return(value);
}
DarkDust
Note that this is an exercise and that the original code eats one char at a time. He may be expected to do it by hand...
dmckee
That occurred to me as well when I saw Matthew's answer...
DarkDust
ok, thank everyone for their help, I am going to work on all of these things and more importantly try to understand them, and then repost my reedited if I have more problems
pisfire
+1  A: 

The issues with the original program are:

  1. getchar() returns an ASCII code, and the condition was wrong. When checking for boundaries, use ((c<'0') || (c>'9')).
  2. For the exit function you need to include "stdlib.h".
  3. For the main function to understand what is get_number, you need to either move the function to be before the main function, or you can use a forward declaration.
  4. The assignment operator (=) has lower precedence than the inequality operator (!=), so you need to use parenthesis, like: ((c = getchar()) != '\n')

You have actually created several more issues, so I wouldn't rely on your code.

In any case, in general - it is advised you study how to use a debugger. Once your code is compiling (and for that you'll need to get accustomed to the compilation error messages), you need to start debugging your code. I suggest you take some time to learn how to set breakpoints, set watches, and go step by step into your code. That skill is absolutely essential for a good developer.

Eldad Mor
thank you for breaking it down for me. I wondered if I had installed the wrong version of something and thats why I could not use the exit function. and Ok I will take your advice and learn to use the debugger.
pisfire
+1  A: 

Here's how I'd go about correcting the code ...

  1. http://ideone.com/a0UMm -- compilation errors
  2. http://ideone.com/ljUg1 -- warnings, but it works now
  3. http://ideone.com/Qd0gp -- no errors, no warnings, test run ok

For 1. I used the "original code" as posted by you.

For 2. I used int main(void), declared the function get_number before defining main, added parenthesis in line 20, and added #include <stdlib.h>

For 3. I added return 0; before the final } of main. I also removed the extra output that messed up the ideone interface (it looks better on an interactive interface)


Edit more tests needed to complete the task of correcting the original code

pmg
Note that reading characters with getchar() should include a test for EOF - but that requires you to define `c` as an `int` and not as a `char`. However, the code will not behave well if there is less input than three numbers each on one line. (Applies to original as well as your final version.)
Jonathan Leffler