Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

I Made A Calculator In C++, Need Help With Decimals!?

Ok, so I made a calculator, and tried using double values for the calculator, but it didn't work too well... Say, for example, if I did 9 divided by 7, it would give me 1. Everything EXCEPT decimals work. If I add 3.7 and 1.3 I get 4. Here is the source:

#include <iostream>

#include <string>

using namespace std;

int add(int, int);

int subtract(int, int);

int multiply(int, int);

int divide(int, int);

int main()

{

long long a, b, c, d, e, f, g;

string h, i, j, k, l, m, n, o, p;

h = "add";

i = "subtract";

j = "multiply";

k = "divide";

hell:

cout << "Hello, please enter what you want to do.\n";

somewhereelse:

cin >> l;

if(l==h)

{

cout << "Please enter the first number.\n";

cin >> a;

cout << "Please enter the second number.\n";

cin >> b;

c = add(a,b);

cout << "The answer is " << c << ".\n";

}

if(l==i)

{

cout << "Please enter the first number.\n";

cin >> a;

cout << "Please enter the second number.\n";

cin >> b;

c = subtract(a,b);

cout << "The answer is " << c << ".\n";

}

if(l==j)

{

cout << "Please enter the first number.\n";

cin >> a;

cout << "Please enter the second number.\n";

cin >> b;

c = multiply(a,b);

cout << "The answer is " << c << ".\n";

}

if(l==k)

{

cout << "Please enter the first number.\n";

cin >> a;

cout << "Please enter the second number.\n";

cin >> b;

c = divide(a,b);

cout << "The answer is " << c << ".\n";

}

if(l!=h && l!=i && l!=j && l!=k)

{

cout << "Please try again.\n";

goto somewhereelse;

}

aplacewhereyoulearntotype:

cout << "Would you like to go again?\n";

cin >> m; n = "yes"; o = "no";

if(m==n)

{

goto hell;

}

if(m==o)

{

cout << "Too bad, maybe next time, eh?\n";

}

if(m!=n && m!=o)

{

cout << "Sorry, please enter \"yes\", or \"no\". Try again.\n";

goto aplacewhereyoulearntotype;

}

return 0;

}

int add(int a, int b)

{

int c =(a+b);

return (c);

}

int subtract(int a, int b)

{

int c =(a-b);

return (c);

}

int multiply(int a, int b)

{

int c =(a*b);

return (c);

}

int divide(int a, int b)

{

int c =(a/b);

return (c);

}

Can you help? Thanks.

Update:

Oops, I gave you guys the original! Haha, I did change the long long to double. Should I change the functions too?

Update 2:

I fixed it! Thanks, and I'll replace the labels! I never thought of function calls, labels were just the first thing that came to mind...

2 Answers

Relevance
  • 9 years ago
    Favorite Answer

    As Bill stated you should use doubles or floats. change this line:

    long long a, b, c, d, e, f, g;

    to

    float a, b, c, d, e, f, g;

    also i would advice against using goto, it is seen as bad programming. use function calls instead.

    Source(s): 5 years as a software engineer.
  • 9 years ago

    The problem is that you are using ints, not doubles. Those numbers can't store fractions. Replace everything with doubles and it should work properly.

Still have questions? Get your answers by asking now.