views:

103

answers:

2

Possible Duplicate:
C++ passing variables in from one Function to the Next.

The Program is working but when it comes to getUserData it asks for the same information 4 times and then displays the results with negative numbers. I used test numbers for number of rooms 1, 110 for sqrt feet in the room, 15.00 for cost of paint.

 //Problems with this not working
   void showMenu();
void getUserData(int &, double &, int &);
void doEstimate(int &, double &, int &, double &, double &);
void showReport();

    int main()
{
 int choice;
//I am not sure why I have to do this but someone suggested to do it and the program complied when I did this int calc ect

int calcGallonsOfPaint, rooms, totalsqrtfeet;
 double calcCostOfPaint, costOfPaint;
 int calcHoursOfLabor;
 double calcLaborCost;
 double calcPaintJobCost;

   // Set up numeric output formatting.
   cout << fixed << showpoint << setprecision(2);

   do
   {
      // Display the menu and get the user's choice.
      showMenu();
      cin >> choice;

      // Validate the menu selection.
      while (choice < 1 || choice > 2)
      {
         cout << "Please enter 1 or 2: ";
         cin >> choice;
      }

      if (choice == 1)
      {
    //for some reason it just keeps repeating the function getUserData
 getUserData(rooms, costOfPaint, totalsqrtfeet);
 doEstimate(calcGallonsOfPaint, calcCostOfPaint, calcHoursOfLabor, calcLaborCost, calcPaintJobCost);
 showReport();


    }
   } while (choice != 2);
   return 0;
}


    void getUserData(int &rooms, double &costOfPaint, int &totalsqrtfeet)
{
 int sqrtfeet;
 int count = 0;

 cout << "Please enter the number of rooms to be painted: ";
 cin >> rooms;

 cout << "Please enter square feet of wall space in each room: ";
 cin >> sqrtfeet;

 for (count = 1; count <= rooms; count++)
  { 
   cout << "Please eneter square feet of wall space in room " << count << ": ";
   cin >> sqrtfeet;
   totalsqrtfeet += sqrtfeet;
  } 

 cout << "What is the cost of the paint: ";
 cin >> costOfPaint;

 system("cls");
 system("pause");
}

void doEstimate(int &calcGallonsOfPaint, double &calcCostOfPaint, int &calcHoursOfLabor, double &calcLaborCost, double &calcPaintJobCost)
{
//I am not sure why I have to do this but someone suggested to do it and the program complied when I did this: puting int rooms ect 
int rooms, totalsqrtfeet;
 double costOfPaint;

 getUserData(rooms, costOfPaint, totalsqrtfeet);

 calcGallonsOfPaint = 1 * (totalsqrtfeet/110);   //Calculates the number of whole gallons of paint required.

 calcCostOfPaint = calcGallonsOfPaint  * costOfPaint; //Calculates the cost of the paint required.

 calcHoursOfLabor = calcGallonsOfPaint * 6;    //Calculates the number of whole hours of labor required.

 calcLaborCost = calcHoursOfLabor * 15.00;    //Calculates the labor charges.

 //Calculates the cost of the paint job. This is the sum of the labor charges and the cost of the paint required.
 calcPaintJobCost = calcLaborCost + calcCostOfPaint;  

/*110 square feet of wall space
one gallon of paint
six hours of labor
$15.00 per hour for labor
*/

}

void showReport()
{

//I am not sure why I have to do this but someone suggested to do it and the program complied when I did this 
int calcGallonsOfPaint, rooms, totalsqrtfeet;
 double calcCostOfPaint, costOfPaint;
 int calcHoursOfLabor;
 double calcLaborCost;
 double calcPaintJobCost;

 getUserData(rooms, costOfPaint, totalsqrtfeet);
 doEstimate(calcGallonsOfPaint, calcCostOfPaint, calcHoursOfLabor, calcLaborCost, calcPaintJobCost);

 cout << "The number of rooms to be painted: " << rooms << endl;
 cout << "The number of whole gallons of paint required: " << calcGallonsOfPaint << endl;
 cout << "The hours of labor required: " << calcHoursOfLabor << endl;
 cout << "The cost of the paint: " << calcCostOfPaint << endl;
 cout << "The labor charges: " << calcLaborCost << endl;
 cout << "The total cost of the paint job: " << calcPaintJobCost << endl;

 system("pause");
 system("cls");
}
+1  A: 

One thing that you should do is initialise totalsqrtfeet to zero in your main function. That's because you're just adding the size of each room to it and it starts out with a random value: junk + a + b + c + d is still junk :-)

On top of that, you call getUserData from your main function and then again from doEstimate. And then you call them both again in showReport. That's why it's asking four times. Just call getUserData once. Since it's homework, I'll leave you to figure out where but here's a hint. If you do it in main (nudge, nudge, wink, wink), you'll have to pass he variables into doEstimate as well, not create new variables of the same name within that function and magically expect the compiler to associate them with the originals.

paxdiablo
Can you just tell me this is killing me...i've been stuck on this for the past day
Razon
@Razon, if you want the answer instead of help with your homework, your best bet is to go back to the original question and say you want the answer. You'll just have to be prepared for failing if your educators discover that you've copied it verbatim from the web. Most people will help and guide you (rather than solve the problem) if you ask a homework question since that's better for you in the long term.
paxdiablo
I'm trying to do what you suggested which is great, but If you could go to the original question and and help. I got a new problem from using your advise.
Razon
A: 

To caveat on what paxdiablo said, you are calling getUserData in your nested while loop, but I don't understand the purpose of calling getUserData(rooms, costOfPaint, totalsqrtfeet); prior to doEstimate(...) when the data isn't used nor passed to doEstimate(...) until you call getUserData again while inside the doEstimate(...) function. You should pass by value the rooms, costOfPaint, and totalsqrtfeet variables to doEstimate(...) or make them global since you only have one main() function anyways. If you had some sort of OO solution, then I wouldn't recommend making them global and rather part of a class.

Also all these variables are ZERO or NULL when they are passed into doEstimate(...):

calcGallonsOfPaint, calcCostOfPaint, calcHoursOfLabor, calcLaborCost, calcPaintJobCost

If you want to output them, then you need to pass them by reference to doEstimate(...) and then when it gets to the cout then it will sufficiently print the correct values. That is why it is zero.

The bottom line is though you need one function to call the other functions. That would be the simplest plan at this point, such as:

GetDataAndPrint(...) {

   // Get some data

   // Do estimate, pass in values by reference

   // Print Results from calculated values

   // return
}
0A0D
Mate, if our technical writers find you, you're in deep trouble. Caveat is a noun, not a verb :-)
paxdiablo