简体   繁体   中英

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

c++ and I'm trying to figure out why my code returns 0's from a few statements after the user inputs some float numbers. I'm not sure why. Maybe someone can help:

This is what I get after running my method and answering the questions before it:

The number of gallons of paint required is: 0 gallons

Hours of labor that is required: 0 hours

.

Also ignore the () around my # in the beginning. I will put periods between lines to make it look neater on this website.

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

You need to make all of those variables you are modifying global (Declared outside of int main() ). In C++, when you give a function a variable, it will just copy the contents of the variable into the function's variables: the original variable passed in remains constant . Thus, your uninitialized float s default to 0 and are not changed by any of the functions, so when they are given to the laborHours function or numberOfHours function, 0 s are passed into each.

Example with much better practices than in your code (it's ok, everyone starts by writing atrocious code) :

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

This still isn't the best way to write this, but it's still the exact same thing you were trying to do. Now try rewriting what you were trying to do in this style!

TLDR/Quick fix: Make the variable definitions global, cut out the arguments from the functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM