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

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

multiple DLLs with the same namespace | 5 | 83 | |

C++ assignment question | 7 | 128 | |

Focus not getting shifted out of editbox | 2 | 50 | |

Fast, free, private online code repository / editor | 7 | 82 |

The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

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

Connect with top rated Experts

**15** Experts available now in Live!