-1

Okay I'm entirely stuck here and I do apologise if this inconviences you guys in any way but I need your help.

I'm currently learning C by myself and slowly getting there, started yesterday. So I thought I would give myself a task on having the user input 3 numbers and the program would have to find the average number between those three.

Here is my code:

#include <stdio.h>

int main() {

    int firstnum, secnum, thirnum, finalnum;

    printf("Enter the first number \n");
    scanf("%f",&firstnum);

    printf("Enter the second number \n");
    scanf("%s",&secnum);

    printf("Enter the third number \n");
    scanf("%t",&thirnum);

    finalnum = (firstnum +secnum+thirnum)/3;
    printf("The average value is: " finalnum);

return finalnum;

}
harre
  • 7,081
  • 2
  • 16
  • 28
  • `"%d"` is the appropriate format specifier to `scanf` or `printf` an `int`. – Klas Lindbäck Dec 15 '15 at 12:58
  • Use `printf("The average value is: %d", finalnum);`, but beware that you're performing integer division (http://mathworld.wolfram.com/IntegerDivision.html). Consider floats. – harre Dec 15 '15 at 12:59
  • So I would put floats in with the 'final num'? – Jordan Savell Dec 15 '15 at 13:00
  • See my answer below. – harre Dec 15 '15 at 13:09
  • 1
    Stack Overflow is not an interactive tutorial nor a substitute for C programming books/studies. You'll have to study the basics mentioned in chapter 1 of your book. Also, when asking a question on SO, clearly state what problems you're having, not just dump some code and say "it's not working". In this case you get compiler errors, so you should have posted them with the question. – Lundin Dec 15 '15 at 14:30

3 Answers3

0

Reading integers or floats: Correct format specifier

For integers you'll need %dand for doubles %lf. Read more about those elsewhere.

E.g.

scanf("%d",&firstnum);

Printing integers or floats

E.g.

printf("The average value is: %d", finalnum);

Avoid integer division: casting or all floats

See http://mathworld.wolfram.com/IntegerDivision.html

E.g.

double finalnum = (firstnum +secnum+thirnum)/3.0;

Or use floats for all types.

double firstnum, secnum, thirnum, finalnum;

Return 0 for success in main

return 0;
harre
  • 7,081
  • 2
  • 16
  • 28
0

I found the fix linking with the comments made by harre and Klas Lindbäck. Thank you both.

I changed my format specifiers to %d as suggested. I then changed my printf line a little bit more.

I then tried to make a float with my int > finalnum but it didn't work. All this did was continously return the average as '0' so I placed finalnum back as an int. This is how the code looks now:

#include <stdio.h>

int main() {

    int firstnum, secnum, thirnum;
    int finalnum;

    printf("Enter the first number \n");
    scanf("%d",&firstnum);

    printf("Enter the second number \n");
    scanf("%d",&secnum);

    printf("Enter the third number \n");
    scanf("%d",&thirnum);

    finalnum = (firstnum + secnum + thirnum)/3;
    printf("The average value is: %d", finalnum);

return finalnum;

}

Thank you all :)

Community
  • 1
  • 1
  • This still isn't what you want. You're still doing integer division here. To do what you want, try the following: 1) Replace all `int` with `float`. 2) Replace all `%d` with `%f`. 3) Replace `3` with `3.0` (though this part isn't strictly necessary but technically correct). The code will then behave as you expect. – Samidamaru Dec 15 '15 at 14:32
  • Yeah float now works for some reason. Lesson learnt - use float when not doing integer division. Thank you – Jordan Savell Dec 20 '15 at 16:16
0

Use float instead of int to get decimal precision otherwise the finalnum would get rounded off

use this for printing float printf("The average value is: %f",finalnum);

for more on printf you can refer this link http://www.tutorialspoint.com/c_standard_library/c_function_printf.htm

Rishi Deorukhkar
  • 179
  • 4
  • 15