Reading Value from user input.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  #include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace 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);

	} else if (input == "Subtraction") { // If subtraction chosen run subtraction
		
		sub();

	} else if (input == "Multiplication") { // If multiplication chosen run multiplication
	
		mult();

	} else if (input == "Division") { // If division chosen run division

		div();
	
	} else  {
		
	cout << "Please enter a appropraite response" << endl;
	cout << " " << endl;
	main();

	};


}

http://www.cplusplus.com/doc/tutorial/functions/

This might help you.

Also having main call itself isn't a good idea if you want to loop look at

http://www.cplusplus.com/doc/tutorial/control/
Thanks, Sorted it!
Topic archived. No new replies allowed.