# money(currency) calculations (newbie question)

I'm trying to write a program that uses money through out. The problem i'm having is with the control strings ( %d, %f, %g etc)

Take for instance
========================================================
// Start Program
#include <stdio.h>
#include <conio.h>
main ()
{
float m,n,o;

o=m+n;

printf("Please enter the value of m :");
scanf("%f",&m);
printf("The value of m is equal to %g\n",m);

printf("Please enter the value of n :");
scanf("%f",&n);
printf("The value of n is equal to %g\n",n);

printf("The value of n is equal to %.2f\n",o);
getch();
}
//End Program
======================================================================

Output
===============
Please enter the value of m :15.65
The value of m is equal to 15.65
Please enter the value of n :20
The value of n is equal to 20
The value of o is equal to 7865950856751840200000000000000000.00
=================

The sum is so ridiculous and i am wondering what i have to do to fix it

LVL 6
###### Who is Participating?

Commented:
Try putting the calculation of o AFTER the input of m and n.
0

Commented:
:-)
0

Commented:
when the calculation has been done the variables all hold garbage values, junk that was previously in that memory location. Like efn said, do the calculation after reading in the values from the keyboard.

A good programming hint, initialise your variables when you declare them, it will eliminate logic errors and hard to find bugs.

so,

in the declaration

float o = 0.0, m = 0.0, n = 0.0;

printf("Please enter the value of m :");
scanf("%f",&m);
printf("The value of m is equal to %g\n",m);

printf("Please enter the value of n :");
scanf("%f",&n);
printf("The value of n is equal to %g\n",n);

o=m+n;

printf("The value of n is equal to %.2f\n",o);
getch();
}
//End Program
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.