variable not declared iin this scope

My program isn't working because it say's variables aren't declared in the correct way. I was wondering if anyone could help me.
#include <iostream>
using namespace std;
#include <iomanip>

void printDescription();
float getSquareFeet();
float cost();
float labor(double sFeet);
float total(float paint, float laborCost);
void printValue(float sFeet, float paint, float laborCost, float totalCost);

int main()
{
float squareFeet;
float paintCost;
float labCost;
float totCost;
printDescription();
squareFeet = getSquareFeet();
paintCost= cost();
labCost= labor(sFeet);
totCost= total(paint, laborCost);
printValue(sFeet, paint, laborCost, total);
}


void printDescription()
{

cout<< "**********************************************************" << endl;
cout<< "Welcome to the paint calculator" << endl;
cout<< "Input: square feet, cost of paint per gallon" << endl;
cout<< "output: paint cost, labor cost ($18 per hour), total cost" << endl;
cout<< "**********************************************************" << endl;
}

float getSquareFeet()
{
float sFeet;
cout << "How muany square feet needs to be painted?" << endl;
cin >> sFeet;
return sFeet;
}

float cost(float sFeet)
{
double gallon;
float paint;
cout<<"What is the cost of paint per galloon?"<< endl;
cin>> gallon;
if(gallon<10.00)
{
cout<<"invalid input"<< endl;
}
else if(gallon>=10.00)
{
paint=(sFeet/115)*gallon;
}
return paint;
}
float labor(float sFeet)
{
float laborCost;
laborCost=(sFeet/8)*18;
return laborCost;
}

float total(float paint, float laborCost)
{
float totalCost;
totalCost=paint+laborCost;
return totalCost;
}

void printValue(float sFeet, float paint, float laborCost, float totalCost)
{
cout<< "Square feet: "<< setprecision(8) << sFeet << endl;
cout<< "Paint cost: " << setprecision(9)<< "$" << paint << endl;
cout<< "Labor cost: " << setprecision(9)<< "$" << laborCost << endl;
cout<< "Total cost: " << setprecision(9)<< "$" << total << endl;
}

painting_TimSpano.cpp: In function ‘int main()’:
painting_TimSpano.cpp:32: error: ‘sFeet’ was not declared in this scope
painting_TimSpano.cpp:33: error: ‘paint’ was not declared in this scope
painting_TimSpano.cpp:33: error: ‘laborCost’ was not declared in this scope
The error is reffering to the two lines in int main()
float labor(double sFeet);
float total(float paint, float laborCost);
Please post the actual error messages, including the line numbers.

EDIT:

I'm guessing the error messages are actually for these two lines:

1
2
3
labCost= labor(sFeet);
totCost= total(paint, laborCost);
printValue(sFeet, paint, laborCost, total);


I have to congratulate you. Not bothering to tell us what lines the errors are on is quite a usual occurrence, but you went better than that, and actually told us the wrong lines. Spectacular!

Anyway, what the compiler is telling you is that you haven't declared the variables sFeet, paint, laborCost or total anywhere within the scope of main.

Oh, and please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Last edited on
Hylian, I apologize for MikeyBoy's incredible rudeness. Most people here are a lot nicer. On the other hand, he DID provide a valuable answer.

Just to elaborate, your program has sFeet, paint and laborCost declared as parameters to functions, but parameters only exist inside their functions. That's why they aren't visible inside main.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
using namespace std;
#include <iomanip>

void printDescription();
float getSquareFeet();
float cost();
float labor(double sFeet);
float total(float paint, float laborCost);
void printValue(float sFeet, float paint, float laborCost, float totalCost);

int main()
{
float squareFeet;
float paintCost;
float labCost;
float totCost;
printDescription();
squareFeet = getSquareFeet();
paintCost= cost();
labCost= labor(sFeet);
totCost= total(paint, laborCost);
printValue(sFeet, paint, laborCost, total);
}


void printDescription()
{

cout<< "**********************************************************" << endl;
cout<< "Welcome to the paint calculator" << endl;
cout<< "Input: square feet, cost of paint per gallon" << endl;
cout<< "output: paint cost, labor cost ($18 per hour), total cost" << endl;
cout<< "**********************************************************" << endl;
}

float getSquareFeet()
{
float sFeet;
cout << "How muany square feet needs to be painted?" << endl;
cin >> sFeet;
return sFeet;
}

float cost(float sFeet)
{
double gallon;
float paint;
cout<<"What is the cost of paint per galloon?"<< endl;
cin>> gallon;
if(gallon<10.00)
{
cout<<"invalid input"<< endl;
}
else if(gallon>=10.00)
{
paint=(sFeet/115)*gallon;
}
return paint;
}
float labor(float sFeet)
{
float laborCost;
laborCost=(sFeet/8)*18;
return laborCost;
}

float total(float paint, float laborCost)
{
float totalCost;
totalCost=paint+laborCost;
return totalCost;
}

void printValue(float sFeet, float paint, float laborCost, float totalCost)
{
cout<< "Square feet: "<< setprecision(8) << sFeet << endl;
cout<< "Paint cost: " << setprecision(9)<< "$" << paint << endl;
cout<< "Labor cost: " << setprecision(9)<< "$" << laborCost << endl;
cout<< "Total cost: " << setprecision(9)<< "$" << total << endl;
}

In function 'int main()': 21:16: error: 'sFeet' was not declared in this scope
 22:16: error: 'paint' was not declared in this scope
 22:23: error: 'laborCost' was not declared in this scope In function 'void printValue(float, float, float, float)': 
80:51: warning: the address of 'float total(float, float)' will always evaluate as 'true' [-Waddress]


Added this for further, if any, diagnostic purposes only, without further comment other than to exceed allegedly usual 2 word self-imposed limit. :]

( PS the diagnosis is grimmer than just fixing the first couple of bloopers. )
Last edited on
Hylian, I apologize for MikeyBoy's incredible rudeness.

Meh. I'm sick of lazy, rude people coming in here, making absolutely minimal effort to give us the information that would be helpful, not even bothering to format their posts to make them readable, and expecting us to do all the legwork of teasing out from them the information they should have supplied in the first place.

That behaviour on the part of people who expect us to give up our time and effort, but won't spend any of their own, is the real rudeness here, not a gentle reprimand from me about providing misleading information.
Last edited on
Meh. I'm sick of lazy, rude people coming in here, making absolutely minimal effort to give us the information that would be helpful

This was a first time poster. He posted the code and the error messages. Yes, he left off code tags, but so do half of the other first time posters. It's just human nature. The compiler message line numbers didn't match the code that he posted, but at least he tried, and the text of the messages gave the problem.

That behaviour [...] is the real rudeness here, not a gentle reprimand from me

Re-read what you posted:
I have to congratulate you. Not bothering to tell us what lines the errors are on is quite a usual occurrence, but you went better than that, and actually told us the wrong lines. Spectacular!
That's not a "gentle repremand," it's a slap in the face of someone who came asking for help.

I get this this person pissed you off, but as the old saying goes, "if you can't say something nice, then don't say anything at all." Please don't discourage first time posters by insulting them. You may discourage a promising programmer from entering the field. If you don't like their post, just don't respond.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
float squareFeet;
float paintCost;
float labCost;
float totCost;
printDescription();
squareFeet = getSquareFeet();
paintCost= cost();
labCost= labor(sFeet);
totCost= total(paint, laborCost); //What is "paint" - should you be using "paintCost"?
printValue(sFeet, paint, laborCost, total); //what is "sFeet"?  Should you be using "squareFeet"?  Same goes for "paint" and "total"
}


Because you're executing in main, your program will look for those variables in main. sFeet and paint and total weren't declared in main, so your program can't find them. Those variables exist inside the other functions you wrote, but only within the scope of those functions. It looks like you should be using paintCost, squareFeet and totCost in place of paint, sFeet and total inside of your main when you are calling labor(...), total(...) and printValue(...)

Just remember that variables are within scope only in the area they are declared. Unless they are global, variables will be visible only within the functions they are originally declared in. Moreover, variables declared within loops inside of a function will only be visible within that loop.

In other news, don't let the rudeness of the other commenter get to you. It's people like that who almost drove me away from programming. I'd ask a question, get called "stupid" and "lazy" and told to f off. Most of us aren't like that. You posted your code, and you posted the errors, which a lot of people here don't even bother to do. But, we usually do prefer it if you use the "code" tags. There is a "<>" button in the formatting options, and it will format the text in-between to look like code. It just makes it much nicer to look at.

I have to congratulate you. Not bothering to tell us what lines the errors are on is quite a usual occurrence, but you went better than that, and actually told us the wrong lines. Spectacular!


I don't think I've seen a comment that rude in quite a while. If you're pissed about a post, ignore it. There's absolutely no need to come in here and bash the original poster.

Meh. I'm sick of lazy, rude people coming in here, making absolutely minimal effort to give us the information that would be helpful

OP DID give the information. The error codes included what was wrong on the lines. We, as programmers, should be able to figure out the location even if the line numbers don't match up.

I apologize for MikeyBoy's rudeness. That was far from a "gently reprimand". Most of us aren't like that.

We're always happy to help. Feel free to post your questions. The more information you give us, like error codes, error descriptions, line numbers, etc, the easier it will be for us to help you debug your code.

Happy programming!
Topic archived. No new replies allowed.