Hello All, I am trying to write my first C++ program which is to be a simple 2 number calculator. The code works fine until I get to entering the numbers, Either
the input is not being read as I liked or they are not being assigned to my variables as I would want, I'm a bit stuck.All suggestions greatly appreciated.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
int add(int x, int y) { // Addition function for adding two numbers.
return x + y;
};
int sub() { // Subtraction function for adding two numbers.
cout << "Please enter the first number and hit enter, Then do the same with the 2nd number" << endl;
int x;
int y;
cin >> x;
cin >> y;
return x - y;
};
int mult() { // Multiplication function for adding two numbers.
cout << "Please enter the first number and hit enter, Then do the same with the 2nd number" << endl;
int x;
int y;
cin >> x;
cin >> y;
return x * y;
};
int div() { // Division function for adding two numbers.
cout << "Please enter the first number and hit enter, Then do the same with the 2nd number" << endl;
int x;
int y;
cin >> x;
cin >> y;
return x / y;
};
int main() {
string input = ""; // Store users input
cout << "Welcome to the CMD Calculator" << endl; // Welcome message to begin program.
cout << "Currently this calculator only supports two variables" << endl; //2nd line after begining program.
cout << "Please choose from Addition, Subtraction, Multiplication or Division" << endl; // Choose Mode
getline(cin, input); // Get input and store
if (input == "Addition") { // If addition chosen run addition
cout << "Please enter the first number and hit enter,Then do the same with the 2nd number" << endl;
int x;
cin >> x;
cin.ignore();
cout << "Please enter 2nd number" << endl;
int y;
cin >> y;
add(x, y);
} elseif (input == "Subtraction") { // If subtraction chosen run subtraction
sub();
} elseif (input == "Multiplication") { // If multiplication chosen run multiplication
mult();
} elseif (input == "Division") { // If division chosen run division
div();
} else {
cout << "Please enter a appropraite response" << endl;
cout << " " << endl;
main();
};
}