6

I'm new to C++ I don't understand why I'm getting this error. Out of 5 statements that are similar 3 mark error but the other two are okay. The error is in the main function.

    #include <iostream>
using namespace std;

// Function declaration
void getGallons(int wall);
void getHours(int gallons);
void getCostpaint(int gallons, int pricePaint);
void getLaborcharges(int hours);
void getTotalcost(int costPaint, int laborCharges);

// Function definition
void getGallons(int wall)
{
    int gallons;

    gallons = wall / 112;

    cout << "Number of gallons of paint required: " << gallons << endl;


}

// Function definition
void getHours(int gallons)
{
    int hours;

    hours = gallons * 8;

    cout << "Hours of labor required: " << hours << endl;


}

// Function definition
void getCostpaint(int gallons, int pricePaint)
{
    int costPaint;

    costPaint = gallons * pricePaint;

    cout << "The cost of paint: " << costPaint << endl;
}

// Function definition
void getLaborcharges(int hours)
{
    int laborCharges;

    laborCharges = hours * 35;

    cout << "The labor charge: " << laborCharges << endl;
}

// Funtion definition
void getTotalcost(int costPaint, int laborCharges)
{
    int totalCost;

    totalCost = costPaint + laborCharges;

    cout << "The total cost of the job: " << totalCost << endl;
}

// The main method
int main()
{
    int wall;
    int pricePaint;

    cout << "Enter square feet of wall: ";
    cin >> wall;

    cout << "Enter price of paint per gallon: ";
    cin >> pricePaint;

    getGallons(wall);

    getHours(gallons); // error here

    getCostpaint(gallons, pricePaint);

    getLaborcharges(hours); // error here

    getTotalcost(costPaint, laborCharges); //error here

    return 0;

}

This lesson focused on using functions and passing parameters in the code. I'm not supposed to use global variables. If you have a better way to do this please share.

hcas
  • 69
  • 1
  • 1
  • 6
  • You haven't defined `gallons`, `hours`, `costPain`, or `laborCharges`. I can see what you are trying to do. There are other problems in your code too. May I suggest that you take some time to learn the basics of the language from a [good textbook](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R Sahu Oct 03 '19 at 04:53
  • You haven't declared the variables gallons, hours, costPaint, laborCharges. In C++ variables have to be declared before they can be used. – jignatius Oct 03 '19 at 04:57
  • Side note: about [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Oct 03 '19 at 05:45

3 Answers3

9

Reducing to three lines (the other errors are analogous):

int wall;    
getGallons(wall);
getHours(gallons); // error here

While wall is defined, gallons is not. And where do you want to get gallons from anyway? The result is hidden deep inside another function. How do you want to get it out from there?

Well, you need a return value:

  int getGallons(int wall)
//^^^ !
{
     int gallons = wall / 112;
     // ...
     return gallons; // !
}

This way, you can use your function like this:

int gallons = getGallons(wall);
// now gallons is defined and you can use it:
getHours(gallons);

Analogously for the other functions and variables.

Usually, it is not a good idea to mix logic (calculations) and output in the same function. So I'd rather move writing to console into main function:

int getGallons(int wall) { return wall / 112; }
int getHours(int gallons) { return gallons * 8; }

int wall;
std::cin >> wall;
int gallons = getGallons(int wall);
std::cout << ...;
int hours = getHours(gallons);
std::cout << ...;

Notice? All input/output now is at the same level...

Side note: It is not necessary to declare functions before defining them if you don't use them before definition:

//void f(); // CAN be ommitted
void f() { };
void g() { f(); }

Counter-example:

void f();
void g() { f(); } // now using f before it is defined, thus you NEED do declare it
void f() { };

If you still want to keep the declarations is rather a matter of style (but will get important when managing code in different compilation units, as you'd then have the declarations in header files – you'll encounter soon in next lessons).

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • @hcas. the above-described method is good. Although if you plan to add more functionality to your code and want to keep it flexible, I would suggest you to create class and add those variables as private and methods as public. – RC0993 Oct 03 '19 at 05:11
  • @RC0993 Depends. Having a class just for the sake of a class is not a good thing. If you have stateless utility functions only, as the ones in the question, it *can* be more appropriate to keep them as free functions (but place them in a namespace). Have a look at standard library, e. g. [](https://en.cppreference.com/w/cpp/header/algorithm), how many free-standing functions will you find? In any case, this goes beyond the scope of the question, author is still starting with functions, classes coming later... – Aconcagua Oct 03 '19 at 05:29
  • @RC0993 Sidenote: The C++ standard doesn't know about 'methods', what you mean by this term is referred to as `member functions` there. To stay consistent with standard, you should prefer the latter term over the former one... – Aconcagua Oct 03 '19 at 05:33
2

Reason is variables are not defined before they are used. Following changes added to the code.

  • since you have named functions as "getSomeValue()" better to use a return type instead of void.
  • its better to use double instead of int, because there are divisions in the calculation
  • also used nested function calls to reduce number of lines of code.

Fixed Code:

#include <iostream>
using namespace std;

// Function declaration
int getGallons(int wall);
int getHours(int gallons);
int getCostpaint(int gallons, int pricePaint);
int getLaborcharges(int hours);
int getTotalcost(int costPaint, int laborCharges);

// Function definition
int getGallons(int wall)
{
    int gallons;

    gallons = wall / 112;

    cout << "Number of gallons of paint required: " << gallons << endl;
    return gallons;
}

// Function definition
int getHours(int gallons)
{
    int hours;

    hours = gallons * 8;

    cout << "Hours of labor required: " << hours << endl;
    return hours;
}

// Function definition
int getCostpaint(int gallons, int pricePaint)
{
    int costPaint;

    costPaint = gallons * pricePaint;

    cout << "The cost of paint: " << costPaint << endl;
    return costPaint;
}

// Function definition
int getLaborcharges(int hours)
{
    int laborCharges;

    laborCharges = hours * 35;

    cout << "The labor charge: " << laborCharges << endl;
    return laborCharges;
}

// Funtion definition
int getTotalcost(int costPaint, int laborCharges)
{
    int totalCost;

    totalCost = costPaint + laborCharges;

    cout << "The total cost of the job: " << totalCost << endl;
    return totalCost;
}

// The main method
int main()
{
    int wall;
    int pricePaint;

    cout << "Enter square feet of wall: ";
    cin >> wall;

    cout << "Enter price of paint per gallon: ";
    cin >> pricePaint;

    int costPaint = getCostpaint(getGallons(wall), pricePaint);
    int laborCharges = getLaborcharges(getHours(getGallons(wall)));
    getTotalcost(costPaint, laborCharges);

    return 0;

}
Ruchira Nawarathna
  • 1,137
  • 17
  • 30
  • Be aware, though, that `double` is a double-edged sword. Consider such simple numbers as 0.1, even these cannot be represented exactly in double (0.1 is periodic in binary!). It might do the job in *given* case, but if you rely on *exact* math, it is most likely the wrong tool for the job (in some cases, fixed comma arithmetics based on large enough integers is superior, e. g. doing calculations in cents instead of euros/dollars). – Aconcagua Oct 03 '19 at 07:01
0

Here are a few errors/issues

  1. You have function declarations which are redundant. You only need them if you plan on calling the function before the definition.

  2. In your main method, you don't declare gallons

  3. In your main method, you don't give values for wall and pricepaint.

  4. In your functions, you operate via side effects, meaning you print to the console, rather than returning anything.

Neel Sandell
  • 429
  • 5
  • 12