## Nabeel24

``````#include <iostream>
using namespace std;
#include <iomanip>
#include <string>
int main()
{
string name;
string number;
double bonusContributed;
const int FW = 10;
double const ticketPrice = 5;
int ticketsSold;
double revenuePercentage;
double moneyDistributed;
double revenueGenerated, adminDeduction, balanceRaised;

cout << "How many tickets were sold? ";
cin >> ticketsSold;
cin.ignore();
cout << "What percentage of the ticket revenue goes to administrative costs? ";
cin >> revenuePercentage;
getline (cin, number);
cout << "How much total money is distributed in prizes? ";
cin >> moneyDistributed;
cin.ignore();
cout << "What is the name of the charity? ";
getline (cin, name);

revenuePercentage = revenuePercentage /100;
revenueGenerated = ticketsSold * ticketPrice;
adminDeduction = revenueGenerated * revenuePercentage;
balanceRaised = revenueGenerated - (adminDeduction + moneyDistributed);

cout << fixed << showpoint << setprecision(2);

cout << "Charity:                              " << setw(FW) << "\"" << name << "\"" << endl;
cout << "Revenue Generated from ticket sales:           \$   " << setw(FW) << revenueGenerated << endl;
cout << "Amount deducted for administrative overhead:   \$   " << setw(FW) << adminDeduction << endl;
cout << "Amount deducted for prize movey:               \$   " << setw(FW) << moneyDistributed << endl;
cout << "Bonus contribution:                            \$   " << setw(FW) << bonusContributed << endl;
cout << "Balance raised for charitable fund:            \$   " << setw(FW) << balanceRaised << endl;

if (ticketsSold >= 10,000)

{
if (ticketsSold >= 100,000)
bonusContributed = 25,000;

else if (ticketsSold >= 50,000)
bonusContributed = 15,000;
else if (ticketsSold >= 25,000)
bonusContributed = 8,000;
else if (ticketsSold >= 10,000)
bonusContributed = 3,000;
}

return 0;
}``````

>>> Got a warning but not sure how to fix it: "warning C4700: uninitialized local variable 'bonusContributed' used"

Also sorry if I posted in the wrong way, first time here :(

## jonsca 1,059

Next time please use the code tags. Type:

[code]

//code goes here

[/code]

Since you assign bonusContributed a value in an if/else structure, if your sales are less than 10,000 you won't assign it a value at all. Even if it is >=10000, your if/else if statements in the second tier have no "else" clause so there's some chance (at least conceptually, to the compiler) that your variable won't be assigned at all.

A solution is to either initialize it to zero (or another value) when you declare it or put else clauses with both the `if(ticketsSold >=10000)` and the `if(ticketsSold >=100000)` .

## Nabeel24

Sorry, I'm a little confused on how you explained the second part about the solution. First time learning this and writing it.

## jonsca 1,059

Either set `double bonusContributed = 0` on line 9 (which is probably the quickest) or

``````if (ticketsSold >= 10,000)
{
if (ticketsSold >= 100,000)
bonusContributed = 25,000;
else if (ticketsSold >= 50,000)
bonusContributed = 15,000;
else if (ticketsSold >= 25,000)
bonusContributed = 8,000;
else if (ticketsSold >= 10,000)
bonusContributed = 3,000;
ELSE
bonusContributed = 0 //or another appropriate value
} //even though you've accounted for all the options the compiler thinks
// that something could sneak through without the else condition

ELSE
{
bonusContributed = 0; //or another value
}
//same here, if your value is 9999 then what is bonusContributed
//with the way you had the code previously?``````

That way any path you take through that code, bonusContributed will be set equal to zero.

## Nabeel24

With this way, when I run the program, the bonusContributed says zero since I put in zero. What would I have to put if want it to refer back to the "if" and "else" list?

## jonsca 1,059

At no point do you enter the bonusContributed. It might happen to equal zero if your compiler initialized it to that during debugging since you output the value before it was assigned anyway.

n if What would I have to put iI want it to refer back to the "if" and "else" list?

I really have no idea what you were asking there, sorry.

## Nabeel24

Actually, I understand forget that last comment, I don't know why now when I run the program the bonusContributed doesn't show as the way I wrote the "if" statements.

## Nabeel24

And just in case, I wrote it the way you wrote it.

## jonsca 1,059

Ok. Glad you got it working. As long as you know what a value of zero signifies you can always leave the declaration at the beginning and check for a zero value at the end. It depends on how careful you want/need to be. I just wanted you to see more why the compiler was complaining about it.

## Nabeel24

Oh yea definitely, I get it now. But the problem I'm having now is when I run the program, it works but the bonusContribution always says zero not by the ticketSold, the way I wrote the "if" statements.

## jonsca 1,059

One quick question: how did you get this to compile with the commas in the numbers? I totally missed that before and even copied and pasted them into the post that I made.

Are you assuming that because you have the if statements on the bottom it will somehow fill in the bonusContributed in the cout statement above? You should really have your if statements before the line `cout << fixed << showpoint << setprecision(2);`

## Nabeel24

``````#include <iostream>
using namespace std;
#include <iomanip>
#include <string>
int main()
{
string name;
string number;
double bonusContributed;
const int FW = 10;
double const ticketPrice = 5;
int ticketsSold;
double revenuePercentage;
double moneyDistributed;
double revenueGenerated, adminDeduction, balanceRaised;

cout << "How many tickets were sold? ";
cin >> ticketsSold;
cin.ignore();
cout << "What percentage of the ticket revenue goes to administrative costs? ";
cin >> revenuePercentage;
getline (cin, number);
cout << "How much total money is distributed in prizes? ";
cin >> moneyDistributed;
cin.ignore();
cout << "What is the name of the charity? ";
getline (cin, name);

revenuePercentage = revenuePercentage /100;
revenueGenerated = ticketsSold * ticketPrice;
adminDeduction = revenueGenerated * revenuePercentage;
balanceRaised = revenueGenerated - (adminDeduction + moneyDistributed);

if (ticketsSold >= 10000)

{
if (ticketsSold >= 100000)
bonusContributed = 25000;

else if (ticketsSold >= 50000)
bonusContributed = 15000;
else if (ticketsSold >= 25000)
bonusContributed = 8000;
else if (ticketsSold >= 10000)
bonusContributed = 3000;
else
bonusContributed = 0;
}

else
{
bonusContributed = 0;
}

cout << fixed << showpoint << setprecision(2);

cout << "Charity:                              " << setw(FW) << "\"" << name << "\"" << endl;
cout << "Revenue Generated from ticket sales:           \$   " << setw(FW) << revenueGenerated << endl;
cout << "Amount deducted for administrative overhead:   \$   " << setw(FW) << adminDeduction << endl;
cout << "Amount deducted for prize movey:               \$   " << setw(FW) << moneyDistributed << endl;
cout << "Bonus contribution:                            \$   " << setw(FW) << bonusContributed << endl;
cout << "Balance raised for charitable fund:            \$   " << setw(FW) << balanceRaised << endl;

return 0;
}``````

## jonsca 1,059

I'm not going to look at it until you put code tags (you may have to paste it in again to keep the formatting). And you still have the commas in with the numbers, are you even compiling this at all?

## Nabeel24

Yea lol, Im retarded too, I missed the comma's as well, working now or so it seems so far haha.

## Nabeel24

Sorry about that, coded it now.
See, I dont know why again, I have uninitialized local variable 'bonusContributed'.

This is what I added:

``revenueGenerated = (ticketsSold * ticketPrice) + bonusContributed;``

The colored part is what I added and got the warning again.

## jonsca 1,059

If you're trying to change line 34, bear in mind that you do not set a value for bonusContributed until line 38. There's a linear flow of control, if something is not set, it has no value (or it has whatever junk was in memory at the location where the variable was created). Move line 34 to line 57 and everything should be ok.

## Nabeel24

Don't get why this error came up now.
Line 22 - 25 is the new part I put in. It Builds with no errors but when I run it and I put in a negative ticket number, it says:

"Run-Time Check Failure #3 - The variable 'revenuePercentage' is being used without being initialized."

This is the code I have:

``````#include <iostream>
using namespace std;
#include <iomanip>
#include <string>
int main()
{
string name;
string number;
double bonusContributed;
const int FW = 10;
double const ticketPrice = 5;
int ticketsSold;
double totalP;
char revenuePercentage;
double moneyDistributed;
double revenueGenerated, adminDeduction, balanceRaised;

cout << "How many tickets were sold? ";
cin >> ticketsSold;

if (ticketsSold <= 0)
cout << "Invalid entry: there must be a positive number of tickets sold.\n";

else
{
cout << "What percentage of the ticket revenue goes to administrative costs? ";
cin >> revenuePercentage;
getline (cin, number);
cout << "How much total money is distributed in prizes? ";
cin >> moneyDistributed;
cin.ignore();
cout << "What is the name of the charity? ";
getline (cin, name);
}

if (ticketsSold >= 10000)

{
if (ticketsSold >= 100000)
bonusContributed = 25000;

else if (ticketsSold >= 50000)
bonusContributed = 15000;
else if (ticketsSold >= 25000)
bonusContributed = 8000;
else if (ticketsSold >= 10000)
bonusContributed = 3000;
else
bonusContributed = 0;
}

else
{
bonusContributed = 0;
}

revenuePercentage = revenuePercentage /100;
revenueGenerated = (ticketsSold * ticketPrice);
adminDeduction = revenueGenerated * revenuePercentage;
balanceRaised = revenueGenerated - (adminDeduction + moneyDistributed);
totalP = balanceRaised + bonusContributed;

cout << fixed << showpoint << setprecision(2);
cout << "Charity:                              " << setw(FW) << "\"" << name << "\"" << endl;
cout << "Revenue Generated from ticket sales:           \$   " << setw(FW) << revenueGenerated << endl;
cout << "Amount deducted for administrative overhead:   \$   " << setw(FW) << adminDeduction << endl;
cout << "Amount deducted for prize movey:               \$   " << setw(FW) << moneyDistributed << endl;
cout << "Bonus contribution:                            \$   " << setw(FW) << bonusContributed << endl;
cout << "Balance raised for charitable fund:            \$   " << setw(FW) << totalP << endl;

return 0;
}``````

## jonsca 1,059

Can you find a path through your code in which revenuePercentage doesn't get assigned a value? Think about what you've corrected so far.

## Nabeel24

I thought it was assigned already but, from what i changed before. Do I have to put "revenuePercentage = revenuePercentage /100;" before line 27?

## jonsca 1,059

Do I have to put "revenuePercentage = revenuePercentage /100;" before line 27?

No, but look if your tickets sold is less than zero you don't give the user the opportunity to re-enter and control passes over the else leaving revenuePercentage (and moneyDistributed) undefined. It's definitely not assigned before that. Initialize it to zero and change your if/else(line 22/25) to a while loop where the user is reprompted. That way everything is all set and the program won't accept any negative ticket amounts.

Trace through your program with a pencil and paper by hand and see where different conditions change the flow. Also, it very rarely hurts to initialize these variables to a value when they are declared.

commented: That pencil-paper approach is superb! I speak from experience :P +8
commented: Just for shear persistence in this thread +1

## Nabeel24

So, I have to initialize revenuePercentage and moneyDistributed before line 27? Also didn't understand the part where you say to change the if/else to a while loop :(. Haven't learned that yet or at least not that I know of. Sorry for the confusion, haven't had a program like this yet... only one causing problems....

## jonsca 1,059

So, I have to initialize revenuePercentage and moneyDistributed before line 27?

You can (and should) initialize them when they are declared on lines 15 and 16. Do you see what I meant by my other post, though? What I was saying is that if you have tickets less than zero with the way you have it, it will skip over the else and leave those variables unassigned. If you initialize the variables it will take care of that for you. If you wanted to be extra careful you could set them equal to zero with the if statement on line 22 (you'd have to wrap all the statements in braces like you did for the else) but since you're not going to do any computations with negative tickets there's not much use. That's why I recommended wrapping it all in the else (see below).

Also didn't understand the part where you say to change the if/else to a while loop :(. Haven't learned that yet or at least not that I know of. Sorry for the confusion, haven't had a program like this yet... only one causing problems....

Ok then don't worry about the while loop. Extend your else on line 25 to cover everything from 25 to 71. That way if your tickets are less than zero the program control will bypass all of that code and immediately exit when it hits the return (there are other ways to do that but just letting it fall through is perfectly good in this case).