请使用函数变量解决/回答C ++程序问题

时间:2010-07-22 01:26:26

标签: c++ variables parameters function pass-by-reference

请解决/回答问题以使程序正常运行。我并没有完全理解通过引用或值传递的变量,我认为这就是使这个变得如此困难的原因。所以,如果你能解决这个问题。过去两天我一直在这里。我已经包含了我的完整代码。

paxdiablo建议这一点,我正在尝试做他们所说的

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

最重要的是,您可以从主函数调用getUserData,然后再从doEstimate调用。然后你再次在showReport中调用它们。这就是为什么要问四次。只需调用getUserData一次。既然是作业,我会让你弄清楚在哪里,但这里有一个暗示。如果你在main(nudge,nudge,wink,wink)中执行它,你也必须将变量传递给doEstimate,而不是在该函数中创建同名的新变量,并且神奇地期望编译器将它们与原稿。 “

当我输入1个房间,110平方英尺,15.00的测试数据时。我在报告功能中获得了正确的房间号码,但其他所有内容都是0

 #include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>

using namespace std;

// Function prototypes
void showMenu();
void getUserData(int &, int &, double &);
void doEstimate(int &, int &, double &, int &, double &, int &, double &, double &);
void showReport(int &, int &, double &, int &, double &, int &, double &, double &);

int main()
{
    int choice = 0;
    int calcGallonsOfPaint = 0, rooms = 0, totalsqrtfeet = 0;
    double calcCostOfPaint = 0, costOfPaint = 0;
    int calcHoursOfLabor = 0;
    double calcLaborCost = 0;
    double calcPaintJobCost = 0;

   // 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)
      {

        //User enters information
        getUserData(rooms, totalsqrtfeet, costOfPaint);

        //Information from getUserData is used to make calculations
        doEstimate(rooms, totalsqrtfeet, costOfPaint, calcGallonsOfPaint, calcCostOfPaint, calcHoursOfLabor, calcLaborCost, calcPaintJobCost);

        //Report is generated from user input and calculations
        showReport(rooms, totalsqrtfeet, costOfPaint, calcGallonsOfPaint, calcCostOfPaint, calcHoursOfLabor, calcLaborCost, calcPaintJobCost);

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

//*****************************************************************
// Definition of function showMenu which displays the menu.       *
//*****************************************************************

void showMenu()
{
   cout << "\n\t\tPaint Job Estimator Menu\n\n";
   cout << "1. Get Paint Job Estimate\n";
   cout << "2. Quit the Program\n\n";
   cout << "Enter your choice: ";
}

/*
After the paint job estimate is displayed, the menu should be displayed again. 
The number of rooms must be at least 1, the price of the paint per gallon must be at least $15.00, 
and the area for the wall space of each room must be greater than 10 square feet. 
All input validation must be performed with a loop.
*/

void getUserData(int &rooms, int &totalsqrtfeet, double &costOfPaint)
{
    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 room 1: ";
    cin >> sqrtfeet;

    for (count = 2; 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;
}

void doEstimate(int &rooms, int &totalsqrtfeet, double &costOfPaint, int &calcGallonsOfPaint, double &calcCostOfPaint, int &calcHoursOfLabor, double &calcLaborCost, double &calcPaintJobCost)
{


    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;     


}

void showReport(int &rooms, int &totalsqrtfeet, double &costOfPaint, int &calcGallonsOfPaint, double &calcCostOfPaint, int &calcHoursOfLabor, double &calcLaborCost, double &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");
}

5 个答案:

答案 0 :(得分:1)

这是错误的

if (choice == 1)
{
    getUserData();
    doEstimate();
    showReport();
}

你的原型是

void getUserData(int &rooms, double &costOfPaint, int &totalsqrtfeet);
void doEstimate(int &calcGallonsOfPaint, double &calcCostOfPaint, int &calcHoursOfLabor, double &calcLaborCost, double &calcPaintJobCost);
void showReport();

你需要

int rooms;
double costOfPaint;
int totalsqrtfeet;

getUserData(rooms, costOfPaint, totalsqrtfeet);

答案 1 :(得分:1)

你的问题在这里:

getUserData();
doEstimate();
showReport();

这些函数都是参数。你没有给他们任何。解决方案是为他们提供他们需要的论据。

他们似乎正在使用用作out-parameters的引用参数。你可以这样称呼它们:

void foo(int& out_param) 
{
   out_param = 1;
}

int main() 
{
   int x = 0;      // Create a variable of the appropriate type to pass 
   foo(x);         // Give it as a parameter to foo
   std::cout << x; // x is now 1, so this should print 1

   return 0;
}

我没有为您的特定问题提供直接的解决方案,因为这显然是家庭作业,但这应该会让您走上正确的轨道。

答案 2 :(得分:1)

不确定这是否与您的问题有关(您没有真正解释这是什么),但在函数doEstimate中您有:

getUserData (double &costOfPaint, int &totalsqrtfeet);

我不确定你在这里尝试做什么,但正如所写,它是一个函数原型声明,没有任何用处。可能你有其他意图吗?如果你想调用这个函数你应该这样做,其中var1var2var3是一些必须在此调用之前声明的变量:

getUserData(var1, var2, var3);

稍后你有:

calcGallonsOfPaint: 1 * (totalsqrtfeet/110);

这应该是=而不是:

答案 3 :(得分:1)

我建议使用(源代码级)调试器逐步执行您的程序,以查看每行代码发生的情况。它是一个不可或缺的工具,可以非常容易地追踪程序中的意外行为,并为您节省大量时间。

只需为您正在使用的编程环境/ IDE执行调试器教程。但通常在使用任何调试器时,您需要在代码中设置断点,然后逐步执行每一行并查看程序的当前状态,检查变量等。

答案 4 :(得分:0)

在课堂上最多有30名学生。每个学生都通过姓氏,姓名和平均数来确定。它要求学生展示:

  1. 按环境降序排列;
  2. 按字母顺序排列。