我运行我的程序时,即使用户"输入数字|| c ++绘画工作

时间:2017-05-06 02:31:02

标签: c++

c ++和我试图找出为什么我的代码在用户输入一些浮点数后从几个语句中返回0&#s; s。我不确定为什么。也许有人可以提供帮助:

这是我在运行方法并回答之前的问题后得到的结果:

  

所需涂料的加仑数为:0加仑

     

所需的劳动时间:0小时

     

在开头的时候也忽略我的#周围的()。我会在线之间加上句点,使它在这个网站上看起来更整洁。

/**
 * A painting company has determined that for every 160 square feet of wall 
        space, one gallon of paint and 3 hours of labor are required.
 *   The company charges the $28.00 per hour for labor.
 *   Design a modular program that allows the user to enter the number of rooms 
     that are to be painted,
 * the approximate square feet of wall space in each room (may differ from room 
   to room), and the price per gallon of paint.
 *    It should then create a report that includes a fancy company header and 
      displays the following information:
 * The number of gallons of paint required: (Rounded up to the next full 
   gallon)

 *      The hours of labor required:
 *      The cost of the paint:
 *      The labor charges:
 *      Total cost of the paint job:
 *    Requirements:
 *      Input validation: The program should not accept a value less than 1 or 
         more than 12 for the number of rooms
 *                        Should not accept a value less than 100 for the square 
                          footage of a room.
 *                        Should not accept a value less than $10.00 or more 
                          than $25.00 for the price of a gallon of paint
 *
 * Lets do this...
 */

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

using namespace std;

float priceOfGallon(float);
float numberOfGallons(float, float);
float totalWallArea(float, float, float);
float laborHours(float, float);
void fancyCompanyHeader();
int main() {
    float area;
    float totalArea;
    float min_labor = 3;
    float number_of_rooms;
    float number_of_gallons;
    float price_of_gallon;
    totalWallArea(area, totalArea, number_of_rooms);
    priceOfGallon(price_of_gallon);
    numberOfGallons(number_of_gallons, totalArea);
    laborHours(number_of_gallons, min_labor);
    fancyCompanyHeader();
    return 0;
}

// function that gets the number of gallons needed for the total area

float numberOfGallons(float number_of_gallons, float totalArea) {
    number_of_gallons = (totalArea / 160.0);
    std::cout << "The number of gallons of paint required is: " << 
                                  number_of_gallons << " gallons" << std::endl;
}


float priceOfGallon(float price_of_gallon){
    std::cout << "Please enter the price per gallon: " << std::endl;
    cin >> price_of_gallon;
    while(price_of_gallon < 10.00 || price_of_gallon > 25.00) {
        std::cout << "The price should be between $10.00 and $25.00. Please try again: " << std::endl;
        cin >> price_of_gallon;
    }
}

float totalWallArea(float area, float totalArea, float  number_of_rooms) {
    std::cout << "Please enter the number of rooms that needs to be painted:" << 
                                  std::endl;
    std::cin >> number_of_rooms;

    while(number_of_rooms < 1)
    {
        cout << "Number of rooms must be at least one. Please try again: " << 
                                  std::endl;
        cin >> number_of_rooms;
    }

    for(float i = 1; i <= number_of_rooms; i++)
    {
        cout << "Please enter the square feet of wall space needed for Room " << 
                                  i << std::endl;
        cin >> area;
        while(area < 100)
        {
            std::cout << "The area should be 100 or greater. Please try again: " 
                                  << std::endl;
            cin >> area;
        }

        totalArea += area;
    }
}

// I will finish this method later
float laborHours(float number_of_gallons, float min_labor) {

    min_labor = number_of_gallons * 28.00;
    std::cout << "Hours of labor that is required: " << min_labor << " hours " 
                                  << std::endl;

return min_labor;
}

1 个答案:

答案 0 :(得分:0)

您需要将所有正在修改的变量设为全局变量(在int main()之外声明)。在C ++中,当你给一个函数一个变量时,它只会将变量的内容复制到函数的变量中:传入的原始变量保持不变。因此,未初始化的float默认为0,并且不会被任何函数更改,因此当它们被赋予laborHours函数或numberOfHours函数时,{{ 1}}被传递到每个。

比你的代码更好的做法的例子(没关系,每个人都从编写残暴的代码开始):

0

这仍然不是写这个的最佳方式,但它仍然是你想要做的完全相同的事情。现在尝试用这种风格重写你想要做的事情!

TLDR /快速修复:将变量定义设为全局,从函数中删除参数。