-4

有什么帮助吗?当我输入这两个数字时,它说答案没有被初始化......?

#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;
    }
}
4

2 回答 2

3

正是这样。您声明答案:

int answer;

然后您多次使用它而不对其进行初始化或为其分配任何值:

cout << "Your answer is: " << answer;
于 2013-02-10T23:46:43.197 回答
0

您使用answer时无需为其分配值,就像警告状态一样:-)

语句部分可能如下:

working = num + num2;
cout << "Your answer is: " << answer;

实际上应该有:

answer = num + num2;

反而。

在这种情况下,您可能可以完全摆脱working

于 2013-02-10T23:48:29.377 回答