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

发布于 2024-09-11 01:42:00 字数 5285 浏览 6 评论 0原文

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

paxdiablo 建议这样做,我正在尝试按照他们所说的去做

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

最重要的是,您从主函数中调用getUserData,然后再次从doEstimate中调用它们,这就是它询问的原因。只需调用一次 getUserData 即可,因为这是作业,我会让您弄清楚在哪里,但这里有一个提示,如果您在 main 中执行此操作(轻移、轻移、眨眼、眨眼),则必须传递变量。也进入 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");
}

Please solve/answer problems to get program to work. I am not fully understanding the passing variables by reference or value and I think that is what is making this so hard. So if you could fix the program. I've been at this for the last 2 days. I have included my full code.

paxdiablo suggested this and I'm trying to do what they said

"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.
"

When I enter test data of 1 room, 110 sqrt feet, 15.00. I get the right number for rooms in the report function but 0 for everything else

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三五鸿雁 2024-09-18 01:42:00

这是错误的

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);

This is wrong

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

Your prototypes are

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

You need

int rooms;
double costOfPaint;
int totalsqrtfeet;

getUserData(rooms, costOfPaint, totalsqrtfeet);
剩余の解释 2024-09-18 01:42:00

你的问题在这里:

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

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

他们似乎正在采用用作输出参数的参考参数。你可以这样称呼它们:

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;
}

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

Your problem is here:

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

These functions all take arguments. You aren't giving them any. The solution is to give them the arguments they need.

They seem to be taking reference arguments that are used as out-parameters. You would call them like this:

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;
}

I'm not giving a direct solution for your particular problem since this is clearly homework, but that should get you on the right track.

漫雪独思 2024-09-18 01:42:00

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

getUserData (double &costOfPaint, int &totalsqrtfeet);

我不确定您在这里想做什么,但作为它是一个函数原型声明,没有任何用处。或许你另有目的?如果你想调用这个函数,你应该这样做,其中 var1var2var3 是一些必须在之前声明的变量这个调用:

getUserData(var1, var2, var3);

稍后你会发现:

calcGallonsOfPaint: 1 * (totalsqrtfeet/110);

这应该是 = 而不是 :

Not sure if this is related to your problem (you don't really explain what that is), but in function doEstimate you have:

getUserData (double &costOfPaint, int &totalsqrtfeet);

I'm not sure what you are trying to do here, but as written it is a function prototype declaration that does nothing useful. Probably you had some other intention? If you want to call the function you should do it like this, where var1, var2 and var3 are some variables that have to be declared before this call:

getUserData(var1, var2, var3);

Later you have:

calcGallonsOfPaint: 1 * (totalsqrtfeet/110);

This should be a = instead of a :.

jJeQQOZ5 2024-09-18 01:42:00

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

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

I would suggest stepping through your program using a (source-level) debugger to see what is happening at each line of code. It's an indispensable tool that makes it immensely easier to track down unexpected behavior in your program as well as saving you a lot of time.

Just do a google for a debugger tutorial for the programming environment/IDE you're using. But in general when using any debugger you want to set a breakpoint in your code and then step through each line and see the current state of your program, inspecting variables etc.

一身仙ぐ女味 2024-09-18 01:42:00

一个班最多有 30 名学生。每个学生都通过姓氏、姓名和平均成绩来识别。它要求学生展示:

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

In a class there are 30 students maximum. Each student is identified by surname, name and average. It requires students to display:

  1. In descending order of environments;
  2. In alphabetical order.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文