**News Alert:**Experts Exchange Confirmed as Safe in Cloudbleed Leak Read More

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 |
---|---|---|---|

Move constructor only called if marked noexcept? | 6 | 104 | |

Setting nameservers after res_init fails doing res_query | 2 | 100 | |

C++ question | 3 | 66 | |

Copy output image from TWindowsMediaPlayer | 6 | 42 |

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