C ++函数和传递变量

时间:2010-07-22 03:12:30

标签: c++ function parameters

  

可能重复:
  C++ passing variables in from one Function to the Next.

程序正在运行但是当涉及到getUserData时,它会要求相同的信息4次,然后显示带负数的结果。我使用房间数量的测试数字1,11表示房间的平方英尺,15.00表示涂料成本。

 //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");
}

2 个答案:

答案 0 :(得分:1)

您应该做的一件事是在主函数中将totalsqrtfeet初始化为零。这是因为您只是将每个房间的大小添加到它,并且它以随机值开头:junk + a + b + c + d仍为junk: - )

最重要的是,您从主要功能调用getUserData,然后从doEstimate再次 。然后在showReport中再次将它们称为 。这就是为什么要问四次。只需拨打getUserData一次即可。既然是作业,我会让你弄清楚在哪里,但这里有一个暗示。如果你在main中执行它(轻推,轻推,眨眼,眨眼),你也必须将变量传递给doEstimate,而不是在其中创建同名的 new 变量函数并且神奇地期望编译器将它们与原始文件相关联。

答案 1 :(得分:0)

要注意paxdiablo所说的内容,你在嵌套的while循环中调用了getUserData,但我不明白在数据为getUserData(rooms, costOfPaint, totalsqrtfeet);之前调用doEstimate(...)的目的在getUserData函数内部再次调用doEstimate(...)之前,不会使用或传递给doEstimate(...)。您应该将会议室 costOfPaint totalsqrtfeet 变量的值传递给doEstimate(...),或者将它们设为全球,因为您只有一个{ {1}}无论如何都有功能。如果您有某种OO解决方案,那么我不建议将它们作为全局而是类的一部分。

当传递到doEstimate(...)时,所有这些变量都是零或NULL:

calcGallonsOfPaint,calcCostOfPaint,calcHoursOfLabor,calcLaborCost,calcPaintJobCost

如果要输出它们,则需要通过引用main()来传递它们,然后当它到达doEstimate(...)时,它将足以打印正确的值。这就是它为零的原因。

底线是你需要一个函数来调用其他函数。那将是目前最简单的计划,例如:

cout