Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

money(currency) calculations (newbie question)

Posted on 2004-09-10
3
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

722 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