I am relatively new to C++ and I am building a calculator. I want to be able to add, subtract, multiply, and divide decimals but for some reason, I cannot. For example, if I did 1.23 + 4.56, I am given 5 instead of 5.79. Any help is appreciated!
#include <iostream>
#include "main.h"
int firstNumber()
{
usingnamespace std;
cout << "Welcome to my calculator! " << endl;// A short intro for the user
cout << "What is your first number? ";// Prompts user to put their first number for the problem in
double a = 0;//defines and resets variable that will be used to store the first number for the problem
cin >> a;//Intakes the first number for the calculator
return a;// Returns the number to the call in main.
}
int secondNumber()
{
usingnamespace std;
cout << "What is your second number? ";// Prompts user to put their second number for the problem in.
double b = 0;//defines and resets variable that will be used to store the second number for the problem
cin >> b;//Intakes the second number for the calculator
return b;// Returns the number to the call in main.
}
int solving()
{
usingnamespace std;
double a = firstNumber();
double b = secondNumber();
cout << "What would you like to do with these numbers? (1 = +, 2 = -, 3 = *, 4 = /) ";// Prompts user to see what they will do with these two numbers
double c = 0;//Defines and resets variable that will be used to translate the operator to an if statement.
cin >> c;//Intakes number that will be translated into a operator.
cout << " " << endl;// adding blankspace.
if (c == 1) {//if statement that translates the number to a operator.
cout << "Your answer is " << a + b;
}
if (c == 2) {//if statement that translates the number to a operator.
cout << "Your answer is " << a - b;
}
if (c == 3) {//if statement that translates the number to a operator.
cout << "Your answer is " << a * b;
}
if (c == 4) {//if statement that translates the number to a operator.
cout << "Your answer is " << a / b;
}
}
int main()
{
solving();
return 0;// the return
}