Solved

Posted on 2004-09-10

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

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 78659508567518402000000000

=================

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

Adecold

3 Comments

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

Question has a verified solution.

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

Title | # Comments | Views | Activity |
---|---|---|---|

How do I open an Access Data base with a password set using C++ | 2 | 110 | |

Parsing XML instructions in Java/any other language.... | 3 | 104 | |

Would like to move button in a function | 3 | 73 | |

Task manager indicates my c++ program memory consumption is growing? | 12 | 100 |

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

Connect with top rated Experts

**25** Experts available now in Live!