bluthunder bluthunder - 2 months ago 6
C++ Question

Why am I being prompted with an error message stating that Y may be used uninitialized when I initialized it?

I just started writing C++ not too long ago. For my class, we were asked to write a program that allows the user to input data and then print out the data respectively. I wanted to verify that the user inputted their data incorrectly so I added an if-else statement. However, when I run the program it says warning: 'Y' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (V == Y || V == y) {

It says this for Y, y, N, and n. What am I doing wrong? I thought I initialized these variables in the char assignment statement... Here is my code:

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

Answer

Hey Thats Wrong Way Of Testing The Condition.

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

It Should Be

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