I am a noob to C++, and as a part of homework I had to create a simple calculator with four functions within C++. I have done this and it works, however, I am now trying to loop it so that a user can have infinite attempts at using it, however, I am having struggles. Basically, when I run my program and tell the program which operation I'd like to use, it tells me that my variable "sum" is not being initialized. Im not quite sure what this is, or how to fix it. Any ideas? Here is my code -
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
while (true)
{
int num1, num2, r;
double sum;
cout << "Enter a number\n";
cin >> num1;
cout << "Enter another number\n";
cin >> num2;
cout << "Please enter an operator (+ , * , /, - or End)\n";
cin >> r;
if (r == 'End') break;
if (r == '+') sum = num1 + num2;
if (r == '-') sum = num1 - num2;
if (r == '*') sum = num1 * num2;
if (r == '/') sum = num1 / num2;
cout << r;
cout << "The answer is \n" << sum << endl;
system("pause");
return 0;
}
}