Wednesday, March 7, 2012

Error "variable 'n' is being used without being initialized"?

Im new to computer programming (using C++) and i dont really know what im doing. my professor is of little help (hes a new professor for an intro class, even if i could understand his accent any better). i've read the C++ manual, which of course, is no help and was hoping somebody who knows what they are doing could help me out.



The goal of the program is to have the user enter an account number(which does nothing), a billing code (i,h,or c, which defines what they will be charged for gallons of water used, and the amount of gallons used. i have to calculate the total cost based on their billing code and gallons used. then i have to output all four variables .(any help at all would be much appreciated):this is my code for my homework:



p.s. i know i havent used any iomanip yet but im not too worried about the output format quite yet.











#include%26lt;iostream%26gt;

#include%26lt;iomanip%26gt;

#include%26lt;string%26gt;

using namespace std;



double findCost (char, double, double);



int main ()

{

char billingCode;

int accountNumber;

double gallonsUsed;

double n;



cout%26lt;%26lt;"Enter Account Number"%26lt;%26lt;endl;

cin%26gt;%26gt; accountNumber;

cout%26lt;%26lt;"Enter Billing Code"%26lt;%26lt;endl;

cin%26gt;%26gt;billingCode;

cout%26lt;%26lt;"Enter Gallons Used"%26lt;%26lt;endl;

cin%26gt;%26gt;gallonsUsed;



double totalCost = n;

findCost (n, gallonsUsed, billingCode);









cout%26lt;%26lt;"Account Number"%26lt;%26lt;" "%26lt;%26lt;"Billing Code"%26lt;%26lt;" "%26lt;%26lt;"Gallons Used"%26lt;%26lt;" "%26lt;%26lt;"Total Cost"%26lt;%26lt;endl;

cout%26lt;%26lt;accountNumber%26lt;%26lt;" "%26lt;%26lt;billingCode%26lt;%26lt;" "%26lt;%26lt;gallonsUsed%26lt;%26lt;" "%26lt;%26lt;"$"%26lt;%26lt; totalCost %26lt;%26lt;endl;



system("PAUSE");

return 0;

}





double findCost (char billingCode , double gallonsUsed, double n)



{

if (billingCode == 'c')



{if ( gallonsUsed %26lt;= 4000000)

{ n = 1500;}

else if (gallonsUsed %26gt; 4000000)

{ n = 1500 + (.00025 * gallonsUsed);}

}

else if (billingCode == 'i')

{if (gallonsUsed %26lt;= 4000000)

{n = 1500;}

else if ( gallonsUsed %26gt; 4000000 %26amp;%26amp; gallonsUsed %26lt;= 10000000)

{n = 2500;}

else if (gallonsUsed %26gt; 10000000)

{n = 3500;}

}

else if (billingCode == 'h')

{n = (.0005 * gallonsUsed) + 10;}



return (gallonsUsed, billingCode, n);

}Error "variable 'n' is being used without being initialized"?
The problem is this part:



double n;

...

double totalCost = n;



you don't set n to anything before you use it. I think there's a couple other problems with your code too:



1. whats the point of n? if you actually need it give it a better name than n.



2. you call - findCost (n, gallonsUsed, billingCode); - but the parameters for that method are in a different order, swap n and billingCode



3. your findCost method doesn't return anything... I'm assuming return (gallonsUsed, billingCode, n); wont compile, if it does then I think you'll end up with a stack overflow exception (i.e. findCost won't stop calling itself and eventually your program will collapse and die).Error "variable 'n' is being used without being initialized"?
idk

No comments:

Post a Comment