请使用函数变量解决/回答C ++程序问题 [英] Please Solve/Answer C++ Program problems with Functions Variables

查看:119
本文介绍了请使用函数变量解决/回答C ++程序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解决/回答问题以使程序正常工作。我不完全理解通过引用或价值传递的变量,我认为这是如此艰难。所以如果你可以修复这个程序。过去两天我一直在这里。我已经包含了我的完整代码。



paxdiablo提出了这个建议,我正在试着做他们所说的话。 你应该做的一件事是在你的主函数中初始化totalsqrtfeet为零,这是因为你只是将每个房间的大小加到它上面,并且它以一个随机值开始:junk + a + b + c + d是仍然是垃圾: - )



最重要的是,您可以从主函数调用getUserData,然后再从doEstimate中调用getUserData,然后在showReport中再次调用它们,这就是为什么它要求四次,只需调用getUserData一次,因为它是作业,所以我会让你找出在哪里,但这里有一个提示,如果你在main(微调,微调,眨眼,眨眼)中做,你必须通过他也会变成doEstimate,而不是在该函数中创建同名的新变量,并且神奇地期望编译器将它们与原始关联。



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

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

使用namespace std;

//函数原型
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;

//设置数字输出格式。
cout<<固定的<<展示点<< setprecision(2);


{
//显示菜单并获得用户的选择。
showMenu();
cin>>选择;

//验证菜单选择。 (选择< 1 ||选择> 2)
{
cout< 请输入1或2:;
cin>>选择;
}

if(choice == 1)
{

//用户输入信息
getUserData(rooms,totalsqrtfeet,costOfPaint) ;

// getUserData的信息用于计算
doEstimate(rooms,totalsqrtfeet,costOfPaint,calcGallonsOfPaint,calcCostOfPaint,calcHoursOfLabor,calcLaborCost,calcPaintJobCost);

//从用户输入和计算生成报告
showReport(rooms,totalsqrtfeet,costOfPaint,calcGallonsOfPaint,calcCostOfPaint,calcHoursOfLabor,calcLaborCost,calcPaintJobCost);


} while(choice!= 2);
返回0;
}

// *********************************** ******************************
//定义显示菜单的showMenu函数。 *
// ******************************************* **********************

void showMenu()
{
cout<< \\\\\\\\\\\;
cout<< 1.获取油漆作业评估。
cout<< 2.退出程序\\\
\\\
;
cout<< 输入你的选择:;
}

/ *
在显示绘画作业估计后,应该再次显示菜单。
房间的数量必须至少为1,每加仑油漆的价格必须至少为$ 15.00,
,并且每个房间的墙面面积必须大于10平方英尺。
所有输入验证都必须用循环执行。
* /

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

cout<< 请输入要涂漆的房间数量:;
cin>>房间;

cout<< 请在房间1输入墙面的平方英尺:;
cin>> sqrtfeet; (计数= 2;计数<=房间;计数++)
{
cout<< 请在房间内留出方形的墙壁空间<<计数<< :;
cin>> sqrtfeet;
totalsqrtfeet + = sqrtfeet;
}

cout<< 油漆的成本是多少?;
cin>> costOfPaint;
}

void doEstimate(int& rooms,int& totalsqrtfeet,double& costOfPaint,int& calcGallonsOfPaint,double& calcCostOfPaint,int& CalcHoursOfLabor,double& CalcLaborCost ,double& calcPaintJobCost)
{


calcGallonsOfPaint = 1 *(totalsqrtfeet / 110); //计算所需油漆的总加仑数。

calcCostOfPaint = calcGallonsOfPaint * costOfPaint; //计算所需油漆的成本。

calcHoursOfLabor = calcGallonsOfPaint * 6; //计算所需的全部劳动小时数。

calcLaborCost = calcHoursOfLabor * 15.00; //计算人工费用。

//计算绘画作业的成本。这是劳务费和所需涂料成本的总和。
calcPaintJobCost = calcLaborCost + calcCostOfPaint;



$ b void showReport(int& rooms,int& totalsqrtfeet,double& costOfPaint,int& calcGallonsOfPaint,double& calcCostOfPaint,int & calcHoursOfLabor,double& calcLaborCost,double& CalcPaintJobCost)
{


cout< 要涂漆的房间数量:<<房间<< ENDL;
cout<< 所需的全部油漆的数量:<< calcGallonsOfPaint<< ENDL;
cout<< 所需的劳动时间:<< calcHoursOfLabor<< ENDL;
cout<< 油漆的成本:<< calcCostOfPaint<< ENDL;
cout<< 劳务费:<< calcLaborCost<< ENDL;
cout<< 油漆作业的总成本:<< calcPaintJobCost<< ENDL;

系统(暂停);
系统(cls);
}


解决方案

程序使用(源级)调试器来查看每行代码中发生了什么。这是一个不可或缺的工具,可以非常方便地跟踪程序中的意外行为,并节省大量时间。



只需在Google上进行调试教程对于您正在使用的编程环境/ IDE。但通常在使用任何调试器时,你想在你的代码中设置一个断点,然后遍历每一行,看看你的程序的当前状态,检查变量等。


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

解决方案

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.

这篇关于请使用函数变量解决/回答C ++程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆