Solved

money(currency) calculations (newbie question)

Posted on 2004-09-10
3
269 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 150 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 100 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

636 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