Unused Variable

closed account (4ybDGNh0)
Hi can someone tell me why I am getting the "unused variable" error for the variable g? I am trying to make a code that you enter your age and gender and it gives you a ticket price for a show. It's for an assignment for one of my classes and the rubric wants a void function (boxOffice) with two parameters (age and gender). Any suggestions would be much appreciated. Thanks in advance!


#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

void boxOffice ();


int main ()
{
char g= '\0';
int a =0;

cout <<"Welcome to Pinnochio's Marionette Theater! Please enter patron's age: ";
cin>> a;

cout <<"Please enter patron's gender: ";
cin>> g;

boxOffice ();

return 0;
}

void boxOffice ()
{
char g ='\0';
int a =0;


if (char g = 'm')
{
if (a<=12)
{
cout<<"Ticket Price: $5.00";
}
else if (a<62)
{
cout<<"Ticket Price: $8.00";
}
else if (a>=62)
{
cout<<"Ticket Price: $5.00";
}
else if (char g = 'f')
{
if (a<=12)
{
cout<<"Ticket Price: $0.00";
}
else if (a<62)
{
cout<<"Ticket Price: $8.00";
}
else if (a>=62)
{
cout<<"Ticket Price: $5.00";
}
}
cout<<endl;
}



}
Hi,
if (char g = 'm')

Firstly, you can't declare a variable inside an if-condition.

==>
if (g == 'm')
Also this :
else if (char g = 'f')

==>
else if (g == 'f')
Does that help? :)
As they pointed out you don't declare variables in if statements. I also took the liberty of condensing the code slightly from what you have done. Assuming this isn't homework, which seems too simplistic to be a homework problem (could be wrong). The unused variable error is fairly self explanatory, it is just a simple warning provided by compilers to let you know that you have declared a variable in your code, but haven't used it beyond declaration. I would get the warning if I uncommented #include <string> and then added string movie_title; in main() without ever using it past that declaration.

Surprised it gave you that warning though as it should have gave you an error about missing primary expression before 'char' in the if statements.

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
#include <iostream>
//#include <cstdlib>  // why are these here? don't appear to be using them right now
//#include <string>

using namespace std;

void boxOffice(int iAge, char chGender);  // have to give function parameters to pass gender and age to

int main()
{
	char gender = 0;  // gave it more descriptive name than g
	int age = 0;   // gave it a more descriptive name than a
	
	cout << "Welcome to Pinnochio's Marionette Theater! Please enter patron's age: ";
	cin >> age;
	
	cout << "Please enter patron's gender: ";
	cin >> gender;
	
	boxOffice(age, gender);
	
	return 0;
}

void boxOffice(int iAge, char chGender) // added parameter list 
{
	switch(chGender){		// changed if/else for male/female check to switch statement
		case 'm':
		case 'M':
			if (iAge <= 12 || iAge >= 62){     // condensed age checks 
				cout << "Ticket Price: $5.00\n";
			}else{
				cout << "Ticket Price: $8.00\n";
			}
		break;
		case 'f':
		case 'F':
			if (iAge <= 12 || iAge >= 62){
				cout << "Ticket Price: $5.00\n";
			}else{
				cout << "Ticket Price: $8.00\n";
			}
		break;
		default:
			cout << chGender << " is not a gender. Please enter m or f.\n";
		break;
	}
}	
Last edited on
> you can't declare a variable inside an if-condition.

The condition in if( condition ) can be

either a. an expression that can be contextually converted to bool

or b. a declaration of a (non-array) variable with a brace-or-equals initializer.
(The value of the condition is the value of the declared variable contextually converted to bool.)

For instance, this construct is canonical:
1
2
3
4
5
6
7
8
9
10
if( std::ifstream file{ path_to_flle } ) // canonical
{
    // use file
    // eg. std::cout << file.rdbuf() ;
}

else
{
    std::cerr << "could not open file '" << path_to_flle << "' for input\n" ;
}
Topic archived. No new replies allowed.