Basic Calculator won't work?

Hi, I'm new to C++ and new to cplusplus.com. The programs I'm using to make c++ programs is MinGW and Vim.

So my problem is that whenever I try to run my program (it's still incomplete), it will ask for my input, then it will instantly close down without a pause and a "Press ENTER to continue..." phrase.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <limits>

int main()
{
	int operationSelection, n1, n2;
	std::cout << "Type in a for Add to do the math" << std::endl;
	std::cin >> operationSelection;
	if (operationSelection == 'a')
	{
		std::cout << "Type a number" << std::endl;
		std::cin >> n1;
		std::cout << "Type another number" << std::endl;
		std::cin >> n2;
		std::cout << "The sum of " << n1 << " and " << n2 << " is " << n1 + n2 << std::endl;
	}

	std::cout << "Press ENTER to continue...";
	std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

	return 0;
}


Is there something wrong with my code? Oh by the way, whenever I compile and make my executable files through vim, I use the following code on Normal Mode

:!g++ calc.cpp -o calc.exe

Thanks for any help!
The problem is these:

std::cin >> n2;

operator >> leaves junk in the buffer (specifically, a '\n' aka an enter) so when you get to your cin.ignore() it sees that and ignores it, ending the program.
Okay, sorry if I'm a little noobie here but what do I do now?

Oh by the way, I guess I didn't really make my problem sound really clear. So my problem is that whenever I run my program and then I type in a, it will instantly close down.

See, it says Type in a for Add to do the math and then it checks for the letter a to be typed in.
operationselection is int not char
Ahh! Thank you so much qtpan and firedraco! Thats what I was trying to figure out, while I was waiting for a reply, I was thinking that int was only for numbers, so there must be a different variable declarer for words and characters.

There's much to learn for me. :)

Again, thanks a lot guys!
Topic archived. No new replies allowed.