Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

money(currency) calculations (newbie question)

Posted on 2004-09-10
3
Medium Priority
?
280 Views
Last Modified: 2010-04-01
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

Adecold
0
Comment
Question by:adecold
3 Comments
 
LVL 15

Accepted Solution

by:
efn earned 600 total points
ID: 12033249
Try putting the calculation of o AFTER the input of m and n.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12033553
:-)
0
 
LVL 11

Assisted Solution

by:Jase-Coder
Jase-Coder earned 400 total points
ID: 12034324
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question