Reputation: 27
Okay so I'm really new to programming and C++ and I've been trying to expand on an in-class assignment by using boolean statements to give the user multiple paths for the program. I was wondering how I get the value defined within the boolean statement to be used outside in the lower //processing area.
The problem: In the "interestRate = interestRate / 12;" problem, Xcode tells me "Variable 'interestRate' may be uninitialized when used here"
cout << "To figure out the interest rate on your deposit, please type whether your money is in a 'CD', 'Savings', 'Checking' or 'IRA' account." << endl;
char CD;
char Savings;
char Checking;
char IRA;
if (cin.get (CD))
{
interestRate = 0.01;
}
if (cin.get(Savings))
{
interestRate = 0.01;
}
if (cin.get(Checking))
{
interestRate = 0.08;
}
if (cin.get(IRA))
{
interestRate = 0.08;
}
//Processing
interestRate = interestRate / 12;
time = years * 12;
amountSaved = deposit *((pow(1 + interestRate, time) - 1) / interestRate);
interestRate = (interestRate * 100) * 12;
Upvotes: 0
Views: 75
Reputation: 61970
To directly answer your question, you presumably have interestRate
defined similar to:
double interestRate;
When not given a value, the default for built-in types like double
is not being initialized at all. If none of your if conditions are true, interestRate
will never be set. Then you use its value when calculating interestRate / 12
. That's undefined behaviour and it's no good. The compiler is being really helpful.
To prevent this, all possible code paths need to give interestRate
a value. For example, if you want invalid inputs to be ignored and use a default value, you can just initialize it and structure your checks similar to what you have now.
With that out of the way, your code isn't doing what you think it is. You seem to want to actually input a string and check its value against several options. What you're doing now is making the user input one character for each check you have and then setting the interest rate if the read succeeds (which it always should for a single character under normal circumstances).
What you need is probably a single string and an if-else chain:
std::string accountType;
if (!(std::cin >> accountType)) {
//unsuccessful read
}
if (accountType == "CD" || accountType == "Savings") {
interestRate = 0.01;
} else if (accountType == "Checking" || accountType == "IRA") {
interestRate = 0.08;
} else {
//invalid input
}
Thomas Matthews also makes a good point in that it is expected from the user's point of view that entering "ira" will do the same thing as entering "IRA", so converting the input string to one case will let you compare against just the lowercase or uppercase string.
Upvotes: 3
Reputation: 57749
You could also use a lookup table to find the interest rates:
struct Entry
{
std::string account_name;
double interest_rate;
};
Entry Interest_Rates[] =
{
{"CD", 0.01},
{"Savings", 0.01},
{"Checking", 0.08},
{"IRA", 0.08},
};
static const unsigned int number_of_account_types =
sizeof(Interest_Rates) / sizeof(Interest_Rates[0]);
//...
std::string account_name;
cin >> account_name;
double interest_rate = 0.0005;
for (unsigned int i = 0; i < number_of_account_types; ++i)
{
if (account_name == Interest_Rates[i].account_name)
{
interest_rate = Interest_Rates[i].interest_rate;
break;
}
}
Upvotes: 1
Reputation: 318
You get this warning since there is no default value for interestRate
. If no if condition evaluates to true, then the variable is indeed uninitialized. Try to give the variable a default value when declaring it, or changing the if statements structure to one that handles the case I mentioned (none is true). This should fix the warning issue.
Upvotes: 3