为什么我在初始化时会提示错误消息,指出Y可能未初始化时使用?

时间:2016-10-01 17:57:28

标签: c++

我不久前刚开始编写C ++。对于我的班级,我们被要求编写一个程序,允许用户输入数据,然后分别打印出数据。我想验证用户输入的数据不正确,所以我添加了一个if-else语句。但是,当我运行该程序时,它会显示警告:' Y'可以在此函数中使用未初始化[-Wyybe-uninitialized]           if(V == Y || V == y){

它表示Y,Y,N和n。我究竟做错了什么?我以为我在char赋值语句中初始化了这些变量......这是我的代码:

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

2 个答案:

答案 0 :(得分:4)

要将V与字符y进行比较,您应该编写V == 'y'(在单引号之间写入字符文字)。

声明char y;创建一个名为y的变量,可以存储任何字符。

答案 1 :(得分:0)

嘿,这是测试条件的错误方法。

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

应该是

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