Help with C

00Jones

Banned
Jul 15, 2001
800
0
0
I am having trouble, I have to show an average of four numbers you input. The numbers have to shown as an int and as a float. This is what I have so far:


#include <stdio.h>

int main()

{
float moo, poo, koo, loo;
float foo;
int hoo;


printf("Enter 4 numbers to be averaged: ");
scanf("%f %f %f %f", &moo, &poo, &koo, &loo);




foo = (moo + poo + koo + loo) /4;
hoo = foo;

printf("Your average as a Float: %f\n", foo);
printf("Your average as an Integer: %d\n", hoo);




return 0;

}


I get and error but it seems to run fine, what do you guys think of the code?
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
Uh, what's the error?

I don't see anything wrong with it, aside from your awful choice of variable names.
 

00Jones

Banned
Jul 15, 2001
800
0
0
Originally posted by: PrincessGuard
Uh, what's the error?

I don't see anything wrong with it, aside from your awful choice of variable names.

Nothing wrong with those names! :D

Sorry, I should have said warning, The warning wants me to turn the float into an int, but I can't.
Should I let that slide? I am thinking my code is really messed up. When I try to add a printf statement so that it lists the numbers you type in, it gives me 0's and some long number.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
cast it explicitly to get rid of the warning. the warning is there so if you mistakenly assign a float to an int the compiler will let you know

hoo = (int)foo;
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
"I get an error" and "the warning wants me to turn a float into an int" are not very helpful at all. Paste the errors/warnings.
 

00Jones

Banned
Jul 15, 2001
800
0
0
Originally posted by: dighn
cast it explicitly to get rid of the warning. the warning is there so if you mistakenly assign a float to an int the compiler will let you know

hoo = (int)foo;

That helped with the warning, thanks
Know I just can't get the printf statement I want to work.
This is what I have:

#include <stdio.h>

int main()

{
float moo, poo, koo, loo;
float foo;
int hoo;


printf("Enter 4 numbers to be averaged: ");
scanf("%d %d %d %d", &moo, &poo, &koo, &loo);

printf("%d %d %d %d\n", moo, poo, koo, loo);

foo = (moo + poo + koo + loo) /4;
hoo = (int)foo;

printf("Your average as a Float: %f\n", foo);
printf("Your average as an Integer: %d\n", hoo);




return 0;

}
 

00Jones

Banned
Jul 15, 2001
800
0
0
I got it, Thanks for you help:


#include <stdio.h>

int main()

{
int moo, poo, koo, loo;
float joo;
int foo;


printf("Enter 4 numbers to be averaged: ");
scanf("%d %d %d %d", &moo, &poo, &koo, &loo);

printf("%d %d %d %d\n", moo, poo, koo, loo);

foo = (moo + poo + koo + loo) / 4;
joo = (float)foo;

printf("%d\n", foo);
printf("%f", joo);





return 0;

}
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
joo will just be identical to foo (a float but showing the integer part only). are you sure that's what you want?


and you really should choose more descriptive names like num1, num2, avgf, avgi or something
 

00Jones

Banned
Jul 15, 2001
800
0
0
I noticed that, I type in 10 15 20 25 and I don't get 17.5, blah, and I thought I was done. SHould I just rewrite it?
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: 00Jones
I noticed that, I type in 10 15 20 25 and I don't get 17.5, blah, and I thought I was done. SHould I just rewrite it?

to keep the result in float, you must do everything in float. then cast the float to an int in the final step

are the 4 input numbers always integers?
 

00Jones

Banned
Jul 15, 2001
800
0
0
When I put everything as a float, the int doesn't work and the when it print's the number you type in, they are all wrong.
 

00Jones

Banned
Jul 15, 2001
800
0
0
#include <stdio.h>

int main()

{
float moo, poo, koo, loo;
float joo;
int foo;
float goo;


printf("Enter 4 numbers to be averaged: ");
scanf("%f %f %f %f", &moo, &poo, &koo, &loo);

printf("You entered the numbers %d %d %d %d\n", moo, poo, koo, loo);

goo = (moo + poo + koo + loo) / 4;
foo = (int)goo;
joo = goo;

printf("Your number as an integer is %d\n", foo);
printf("Your number as a float is %f", joo);





return 0;

}


got everything to work except the printing out the numbers, I just get weird numbers
 

BCYL

Diamond Member
Jun 7, 2000
7,803
0
71
moo, poo, koo, loo are all floats... why are u trying to print them as integers? use '%f' in your printf statement...
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
a few ways to overcome that:

- you can do something like %.2f so it'll only display 2 digits
- you can use %d but convert the floats to ints in the printf fucntion eg printf("%d", (int)moo);
- or use ints for the inputs but force floating piont division by doing something like goo = (moo + poo + koo + loo) / 4.0; (goo is still a float)
 

00Jones

Banned
Jul 15, 2001
800
0
0
Originally posted by: dighn
a few ways to overcome that:


- you can use %d but convert the floats to ints in the printf fucntion eg printf("%d", (int)moo);

THANKS! That did it!

final code:

#include <stdio.h>

int main()

{
float moo, poo, koo, loo;
float joo;
int foo;
float goo;


printf("Enter 4 numbers to be averaged: ");
scanf("%f %f %f %f", &moo, &poo, &koo, &loo);

printf("You entered the numbers %d %d %d %d\n", (int)moo, (int)poo, (int)koo, (int)loo);

goo = (moo + poo + koo + loo) / 4;
foo = (int)goo;
joo = goo;

printf("Your number as an integer is %d\n", foo);
printf("Your number as a float is %f", joo);





return 0;

}