Любая помощь? Когда я вставляю два числа, он говорит, что ответ не инициализируется ...?Ответ переменной используется без инициализации?
#include <iostream>
using namespace std;
int main()
{
int num;
int num2;
int working;
int answer;
int uChoice;
int work(int one, int two, int todo);
cout << "Welcome to my Basic Mini-Calculator!" << endl;
do
{
cout << endl << "What do you want to do?" << endl;
cout << "1) Add" << endl;
cout << "2) Subtract" << endl;
cout << "3) Multiply" << endl;
cout << "4) Divide" << endl;
cout << endl << "Waiting for input... (enter a number): ";
cin >> uChoice;
cout << endl;
} while(uChoice != 1 && uChoice != 2 && uChoice != 3 && uChoice != 4);
switch (uChoice)
{
case 1:
cout << endl << "You chose addition." << endl;
cout << "Enter a number: ";
cin >> num;
cout << "Enter another number: ";
cin >> num2;
working = num + num2;
cout << "Your answer is: " << answer;
break;
case 2:
cout << endl << "You chose subtraction." << endl;
cout << "Enter a number: ";
cin >> num;
cout << "Enter another number: ";
cin >> num2;
working = num - num2;
cout << "Your answer is: " << answer;
break;
case 3:
cout << endl << "You chose multiplication." << endl;
cout << "Enter a number: ";
cin >> num;
cout << "Enter another number: ";
cin >> num2;
working = num * num2;
cout << "Your answer is: " << answer;
break;
case 4:
cout << endl << "You chose division." << endl;
cout << "Enter a number: ";
cin >> num;
cout << "Enter another number: ";
cin >> num2;
working = num/num2;
cout << "Your answer is: " << answer;
break;
return 0;
}
}
Возможно, вы имеете в виду «работающий», а не «ответ» ... –
@MatsPetersson Nice catch! – Plynx
Ваш код никогда не присваивает переменной 'answer' перед ее печатью. Это неопределенное поведение. – dasblinkenlight