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

C++ 请解决/回答C++;函数变量的程序问题,c++,variables,parameters,function,pass-by-reference,C++,Variables,Parameters,Function,Pass By Reference,请解决/回答问题以使程序正常运行。我没有完全理解通过引用或值传递变量,我认为这就是为什么这么难。如果你能修好这个程序。过去两天我一直在做这件事。我已经包括了我的全部代码 帕斯迪亚布洛提出了这个建议,我正试着按照他们说的去做 “您应该做的一件事是在主函数中将totalsqrtfeet初始化为零。这是因为您只需将每个房间的大小添加到其中,它以一个随机值开始:junk+a+b+c+d仍然是junk:-) 最重要的是,从主函数调用getUserData,然后从doEstimate再次调用。然后在show

请解决/回答问题以使程序正常运行。我没有完全理解通过引用或值传递变量,我认为这就是为什么这么难。如果你能修好这个程序。过去两天我一直在做这件事。我已经包括了我的全部代码

帕斯迪亚布洛提出了这个建议,我正试着按照他们说的去做

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

最重要的是,从主函数调用getUserData,然后从doEstimate再次调用。然后在showReport中再次调用这两个函数。这就是为什么它要问四次。只需调用getUserData一次。因为这是家庭作业,我会让你去弄清楚在哪里,但这里有一个提示。如果你在主函数中调用(轻推,轻推,眨眼,眨眼),您还必须将变量传递到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");
}
#包括
#包括
#包括
#包括
使用名称空间std;
//功能原型
void showMenu();
void getUserData(int&,int&,double&);
void doEstimate(int&,int&,double&,int&,double&,int&,double&,double&);
无效显示报告(int&,int&,double&,int&,double&,int&,double&,double&);
int main()
{
int-choice=0;
int calcGallonsOfPaint=0,房间=0,总平方英尺=0;
双钙涂料=0,涂料成本=0;
int calcHoursOfLabor=0;
双重成本=0;
双重calcPaintJobCost=0;
//设置数字输出格式。
这是错误的

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);
你的问题是:

getUserData();
doEstimate();
showReport();
这些函数都接受参数。你没有给它们任何参数。解决方法是给它们所需的参数

它们似乎使用了用作out参数的引用参数。您可以这样调用它们:

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;
}
void foo(int&out_参数)
{
out_param=1;
}
int main()
{
int x=0;//创建要传递的适当类型的变量
foo(x);//将其作为参数赋给foo

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

getUserData (double &costOfPaint, int &totalsqrtfeet);
calcGallonsOfPaint: 1 * (totalsqrtfeet/110);
我不确定您在这里试图做什么,但正如所写的那样,它是一个没有任何用处的函数原型声明。可能您还有其他意图?如果您想调用函数,您应该这样做,其中
var1
var2
var3
是在此之前必须声明的一些变量电话:

getUserData(var1, var2, var3);
稍后你会:

getUserData (double &costOfPaint, int &totalsqrtfeet);
calcGallonsOfPaint: 1 * (totalsqrtfeet/110);

这应该是一个
=
而不是

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


只需在google上搜索您正在使用的编程环境/IDE的调试器教程。但一般来说,在使用任何调试器时,您都希望在代码中设置一个断点,然后逐行查看程序的当前状态、检查变量等。

在一个类中,最多有30名学生。每个学生都是可识别的按姓氏、姓名和平均数排序。要求学生显示:

  • 按环境的降序排列
  • 按字母顺序

  • 在哪里?您可以只显示您遇到问题的几行吗?您可以给出实际结果和预期结果吗?另外,您没有向函数传递任何内容:if(choice==1){getUserData();doEstimate();showReport();}getUserData和doEstimate都有几个参数,您没有提供任何参数。请尝试以您认为应该的方式清楚地解释您想要做的事情、您尝试过的事情以及哪些事情不起作用。此外,请尝试减少代码以省去不必要的部分。只需使用代码来说明您的问题。我已经修复了代码并减少了一些代码我现在遇到的问题是,从getUserData到在下一个函数中进行计算,再到最终在“编程”中显示结果,都不是这样标签在这个网站上有点多余,我想。我试过了。我不使用全局变量。所以我必须通过引用或类似的方式传递变量。这些不是全局变量。它们是通过引用传递的。问题是,这段代码太多了,无法学习。从小一点开始——一个main()还有一个函数。从main调用该函数。我更改了代码。我在上面发布了它。我更改了原型和您建议的内容。它可以工作,但它会重复getUserData函数。我需要它从getUserData获取信息,将变量放入doEstimate并运行计算。以showReport结束获取所有变量并显示它们。在最终显示结果之前,它要求4倍于getUserData的输入。在最终显示结果之前,它要求4倍于getUserData的输入。请帮助。我最终让它开始工作,但它不断重复getUserData 4次,然后以负数显示结果。I use表示房间数1,然后对于每个房间的平方英尺,我输入110,对于油漆成本,我输入15.00。当结果为负数时,它会一遍又一遍地询问相同的问题,但在结果中显示的是正确的房间数。在最终显示之前,它会询问getUserData输入的4倍这个