저는 C++을 쓰기 시작한 지 얼마되지 않았습니다. 제 수업에서는 사용자가 데이터를 입력 한 다음 데이터를 각각 인쇄 할 수있는 프로그램을 작성하도록 요청 받았습니다. 사용자가 데이터를 잘못 입력했는지 확인하여 if-else 문을 추가했습니다. 그러나 프로그램을 실행할 때 경고 : 'Y'는이 함수에서 초기화되지 않은 상태로 사용될 수 있습니다. (-Wmaybe-uninitialized) if (V == Y || V == y) {초기화했을 때 Y가 초기화되지 않은 상태로 사용될 수 있다는 오류 메시지가 나타나는 이유는 무엇입니까?
Y, Y, N, 및 n. 내가 도대체 뭘 잘못하고있는 겁니까? 나는 여기에 내 코드입니다 ... 나는 문자 할당 문에서 이러한 변수를 초기화 생각 :
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int principle;
double rate, time, interest, initInvest, total;
char V, Y, y, N, n;
cout << "Enter the initial amount of your investment: ";
cin >> initInvest;
cout << "The amount of your initial investment has been set to " << initInvest << "."
<< "\n\nEnter the investment principle as a real number: ";
cin >> principle;
cout << "The investment principle has been set to " << principle << "."
<< "\n\nEnter the amount of time that has passed since the initial deposit: ";
cin >> time;
cout << "The amount of time passed since the initial deposit has been set to " << time << "."
<< "\n\nEnter the investment rate as a decimal number: ";
cin >> rate;
cout << "The investment rate has been set to " << rate << ".\n\n"
<< "Please verify that the above values are correct Y/N: "; //verifies data in case user inputed values incorrectly
cin >> V; //verification value
if (V == Y || V == y) {
interest = principle*rate*time; //calculates interest earned
total = initInvest+interest; //calculates total amount earned
cout << setiosflags(ios::showpoint) << fixed << setprecision(3); //sets output for all floating-point values
cout << "INITIAL INVESTMENT INTEREST EARNED TOTAL AMOUNT EARNED\n" //creates a neat table to
<< "------------------ --- --------------- --- -------------------\n" //display the gathered data
<< setw(18) << initInvest << setw(20) << interest << setw(24) << total << endl;
}
else if (V == N || V == n) { //re-initiates all before stated data
cout << "\nPlease re-enter the data and try again.\n"
<< "Enter the initial amount of your investment: ";
cin >> initInvest;
cout << "The amount of your initial investment has been set to " << initInvest << "."
<< "\n\nEnter the investment principle as a real number: ";
cin >> principle;
cout << "The investment principle has been set to " << principle << "."
<< "\n\nEnter the amount of time that has passed since the initial deposit: ";
cin >> time;
cout << "The amount of time passed since the initial deposit has been set to " << time << "."
<< "\n\nEnter the investment rate as a decimal number: ";
cin >> rate;
cout << "The investment rate has been set to " << rate << ".\n\n"
<< "Please review the output values.\n"
<< "If they are still not correct, restart the program."; //gave the user a second chance to get it right
}
return 0;
}
어디에서 초기화 했습니까? –