Solved

money(currency) calculations (newbie question)

Posted on 2004-09-10
3
230 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 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now