'saleFalg' was not declared in this scope

I'm a CS1 student and I'm almost done with my lab. I need help though because code::blocks says that I haven't declared the bolo variable saleFlag within the scope of either my getSaleType() function or my do-while loop. I tried declaring & initializing the variable within the getSaleType() function & the do-while loop, but code::blocks insists that I haven't declared it.

here's the error from the log:
<
|78|warning: variable 'saleFlag' set but not used [-Wunused-but-set-variable]|
|84|error: 'saleFalg' was not declared in this scope|
>

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  // Program id: CSLab05.cpp
// This is our template for CS Lab 05
// REFACTORING PRACTICE PROGRAM
// This program is used for, Department of Computer Science Laboratories
// Goal: refactor main into functions
// examine each portion in main and analyze the code for refactoring
// Next build your function prototype
// Next build your function by copying and pasting the code from main, into your function
// repeat until complete

#include<iostream>
#include <iomanip>
#include <cctype>

using namespace std; // forgive me for being lazy!

const double TAX_RATE = 0.05;

int getPrice();
double getPurchased();
char getSaleType();
double getSaleTotal(char, double, double);
void displayTotal(double, double, double, char);

int main()
{

   double price, quant, total, saleType;


	price = getPrice();
	quant = getPurchased();
	saleType = getSaleType();
	total = getSaleTotal(saleType, price, quant);
	displayTotal(price, quant, total, saleType);
    return 0;
}

int getPrice()
{
	double price;
	cout << "Enter price $: ";
	cin >> price;
	while (price <= 0)
	{
		cout << "Enter a price more than 0 dollars and 0 cents. $: ";
		cin >> price;
	}
	return price;
}

double getPurchased()
{
	double quant;
   cout << "Enter the quantity purchased: ";
   cin >> quant;
   while (quant < 1)
   {
		cout << "Quantity must be greater than one. Enter the quantity purchased: ";
		cin >> quant;
   }
   return quant;
}

char getSaleType()
{
	char saleType;

	do
	{


		cout << "Type W if this is wholesale purchase. \n"
			 << "Type R if this is retail purchase. \n"
			 << "then press return... \n";
		cin.ignore();
		cin.get(saleType);
		bool saleFlag = false;
		if (!(saleType == 'W' && saleType == 'R'))
		{
			saleFlag = true;
		}

	} while (saleFalg); //here's the error


	return saleType;
}

double getSaleTotal(char saleType, double price, double quant)
{
	double subTotal, total;

	if ((saleType == 'W') || (saleType == 'w'))
    {
        total = price * quant;

    }
    else if ((saleType == 'R') || (saleType == 'r'))
    {
        subTotal = price * quant;
        total = subTotal + subTotal * TAX_RATE;
    }
    else
    {
        cout << "Error in the input...";
    }

    return total;

}

void displayTotal(double price, double quant, double total, char saleType )
{

	cout << setprecision(2) << fixed << showpoint << quant << " items at $" << price << endl;
    cout << "Total bill $" << total;
    if ((saleType == 'R') || (saleType == 'r'))
    {
        cout << " includes sales tax.\n";
    }
}
Last edited on
Line 84: Check your spelling.

I corrected the spelling, thanks, but the same errors persist. it still says saleFlag is set but not used and saleFlag was not declared in this scope. I even double checked to make sure it wasn't a spelling problem by double clicking on the declaration on line 72 ( I moved the declaration on line 78 to line 72) and on lines 72, 81, & 84 the word saleFlag is highlighted.
Last edited on
Try moving the declaration of saleFlag to before the beginning of the loop. The loop condition is not within the scope of the body of the loop, so the variables used in the condition part of the loop must be defined prior to the loop.
I tried that but it still didn't work. Does the variable scope for saleFlag look right to you? It seems right to me, but the compiler still says something is wrong.
I tried that but it still didn't work.

Where, show your code.

Does the variable scope for saleFlag look right to you?

No, it's not correct. If you want to use this variable in your while() condition the variable must be declared prior to the loop.
Strange here's the code, it's working now.

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Program id: CSLab05.cpp
// This is our template for CS Lab 05
// REFACTORING PRACTICE PROGRAM
// This program is used for , Department of Computer Science Laboratories
// Goal: refactor main into functions
// examine each portion in main and analyze the code for refactoring
// Next build your function prototype
// Next build your function by copying and pasting the code from main, into your function
// repeat until complete

#include<iostream>
#include <iomanip>
#include <cctype>

using namespace std; // forgive me for being lazy!

const double TAX_RATE = 0.05;

int getPrice();
double getPurchased();
char getSaleType();
double getSaleTotal(char, double, double);
void displayTotal(double, double, double, char);

int main()
{

   double price, quant, total, saleType;


	price = getPrice();
	quant = getPurchased();
	saleType = getSaleType();
	total = getSaleTotal(saleType, price, quant);
	displayTotal(price, quant, total, saleType);
    return 0;
}

int getPrice()
{
	double price;
	cout << "Enter price $: ";
	cin >> price;
	while (price <= 0)
	{
		cout << "Enter a price more than 0 dollars and 0 cents. $: ";
		cin >> price;
	}
	return price;
}

double getPurchased()
{
	double quant;
   cout << "Enter the quantity purchased: ";
   cin >> quant;
   while (quant < 1)
   {
		cout << "Quantity must be greater than one. Enter the quantity purchased: ";
		cin >> quant;
   }
   return quant;
}

char getSaleType()
{
	char saleType;
	bool saleFlag = true;

	do
	{

		cout << "Type W if this is wholesale purchase. \n"
			 << "Type R if this is retail purchase. \n"
			 << "then press return... \n";
		cin.ignore();
		cin.get(saleType);
		if (saleType == 'W' || saleType == 'w' || saleType == 'R' ||  saleType == 'r')
			{
				saleFlag = false;
			}

	} while (saleFlag);


	return saleType;
}

double getSaleTotal(char saleType, double price, double quant)
{
	double subTotal, total;

	if ((saleType == 'W') || (saleType == 'w'))
    {
        total = price * quant;

    }
    else if ((saleType == 'R') || (saleType == 'r'))
    {
        subTotal = price * quant;
        total = subTotal + subTotal * TAX_RATE;
    }
    else
    {
        cout << "Error in the input...";
    }

    return total;

}

void displayTotal(double price, double quant, double total, char saleType )
{

	cout << setprecision(2) << fixed << showpoint << quant << " items at $" << price << endl;
    cout << "Total bill $" << total;
    if ((saleType == 'R') || (saleType == 'r'))
    {
        cout << " includes sales tax.\n";
    }
}
Thanks for you help AbstractionAnon & Jlb.
Topic archived. No new replies allowed.