当我运行我的程序时,即使"user"输入数字||C++ 绘画作业

I keep getting 0's when I run my program even though the "user" inputs numbers || c++ painting job

本文关键字:输入 user 数字 C++ 作业 绘画 即使 运行 我的 程序      更新时间:2023-10-16

c ,我正在尝试弄清为什么我的代码在用户输入一些浮点数后从几个语句返回0。我不确定为什么。也许有人可以提供帮助:

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

所需油漆的加仑数为: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;
}

您需要制作所有要修改全局的变量(在int main()之外声明(。在C 中,当您将函数一个变量提供时,它将仅将变量的内容复制到函数的变量中:传递的原始变量传递给了恒定。因此,您的非初始化的float s默认为0,并且任何功能都不会更改,因此当将它们赋予laborHours函数或numberOfHours函数时,0 S将传递到每个功能中。

示例的实践比您的代码更好得多(没关系,每个人都从编写残酷的代码开始(:

#include <iostream>
int walls,rooms,total_walls; //It only makes sense if all of these are integers.
//These are global declarations, they can be modified by any function in the program
void input_walls() { 
    /* Functions are notated as so in C++: 
       {return type} {function_name}({input},{input},...)
       It's pointless to annotate functions that don't return anything
       with a declaration of a float return type (they'll just return 0 */
    std::cin >> walls;
    while(walls < 0) {
        std::cout << "Please input a positive amount of walls per room.";
        std::cin >> walls;
    }
}
void input_rooms() {
    std::cin >> rooms;
    while(rooms < 0) {
         std::cout << "Please input a positive amount of rooms";
         std::cin >> rooms;
    }
}
void calculate_result() {
    total_walls = walls*rooms;
}
void output_result() {
    std::cout << "I'll need to paint " << total_walls << " walls!" << std::endl;
}
int main() {
    input_walls();
    input_rooms();
    calculate_result();
    output_result();
}

这仍然不是写这篇文章的最佳方法,但这仍然是您要做的完全相同的事情。现在尝试重写您要以这种样式进行的操作!

tldr/快速修复:使变量定义全局,从函数中删除参数。