2016-10-01 1 views
-2

저는 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; 
} 
+0

어디에서 초기화 했습니까? –

답변

4

캐릭터 yV을 비교하려면 V == 'y' (작은 따옴표 사이에 기록 된 문자 리터럴)를 작성해야해야한다.

선언 char y;은 어떤 문자도 저장할 수있는 y이라는 변수를 만듭니다.

+0

제안 : 'v =='y ''를 뜻할 때 실수로'v = 'y''라고 쓰는 것은 정말로 일반적인 버그입니다. 유효한 C 또는 C++이지만 부작용이 있습니다. 그러나 항상'=='의 * 왼쪽에 상수를 쓰는 습관을 가지면''y '= v'는 항상 컴파일되지 않습니다. 이렇게하면 많은 문제를 줄일 수 있습니다. – Davislor

+0

@Lorehead 유효한 C/C++이지만 정상적인 컴파일러라면 경고합니다. 또한 심각한 프로그래밍에서는 "경고를 오류로 처리"해야하므로 "요다 조건부"가 절실히 필요합니다. – Angew

+0

일반적인 경우에는 동의하지만, 지금은 아무 것도 밝지 않은 골동품 컴파일러로 기존 시스템을 유지해야 할 때가 있습니다. 또한 코드가 "작동 했으므로"수정이 허용되지 않는 경고의 경고를 방출하는 일부 작업을 수행했습니다. 추천 : 당신이이 장소들 중 하나에서 자신을 발견하면 오래 머무르지 마십시오. 그리고 추가 요금. – user4581301

0

헤이 테스트 조건의 그게 잘못 된 방법.

if (V == Y || V == y) { //Wrong Way 

if (V == 'Y' || V == 'y') { 
+0

당신은 왜 이것을 계속 쓰고 있습니까? –

+0

@ LightnessRacesinOrbit 자본주의, 아마. – user4581301

관련 문제