This question already has an answer here:
I'm getting the error "The variable 'num1' is being used without being initialized". I understand what it means, but in this case, I can't figure out what I did wrong. Wouldn't "result=num1+num2" be initializing it?
Here's my code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Hello user, what do you want to do:" << endl;
cout << "A. Display a Message" << endl;
cout << "B. Perform calculation" << endl;
cout << "C. Exit the program" << endl;
cout << "A, B, or C? ";
string choice;
cin >> choice;
if (choice == "A")
{
cout << "Welcome to C++!" << endl;
}
else if (choice == "B")
{
int num1;
int num2;
int result;
result = num1 + num2;
cout << "Enter first number to calculate: ";
cin >> num1;
cout << "Enter second number to calculate: ";
cin >> num2;
cout << "The sum of " << num1 << " and " << num2 << " is " << result << endl;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire