Variables

Pages: 12
i use code::blocks. I am not able to use a variable that i've declared globally in the function definition..
1
2
3
4
5
6
7
8
9
10
11
int var1, var2;
switch(var1)
{
case 2 : func1;
    :
    :
    :
}
func1
{
std::cin>>var2;

the error i am getting is:'var2' has not been declared
Last edited on
That cannot be your whole code and therefore we cannot know.
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
double ba_sal, HRA, g_sal, sal;
    int DA, TA, PF, IT, no2, no, no3;
    std::string str, no1, age;
    void CreRe();
    void ViewRe();
    void YrSal();
    void EditSal();
    do
    {std::cout<<"Menu \n 1. Create Record \n 2. View All Records \n 3. Yearly Salary \n 4. Edit Salary \n 5. Exit \n Please enter the number:";
    std::cin>>no;
switch(no)
{
    case 1 : CreRe();
             break;
    case 2 : ViewRe();
             break;
    case 3 : YrSal();
             break;
    case 4 : EditSal();
             break;
    std::cout<<"CREATE RECORDS \n";
    std::cout<<"Enter Record Number: ";
    std::cin>>no1;
    std::cout<<"\n Enter name: ";
    std::cin>>str;
    std::cout<<"\n Enter age:";
    std::cin>>age;
    std::cout<<"\n 1. Managing Director \n 2. Director \n 3. General Manager \n 4. Deputy General Manager \n 5. Chief Manager \n 6. Senior Executive \n 7. Executive \n 8. Officer \n 9. Clerk \n 10. Sub-Staff \n Enter designation number : ";
    std::cin>>no2;
    ba_sal=0;
    do
    {
    switch(no2)
    {
    case 1 :     std::cout<<"\n MANAGING DIRECTOR";
                   ba_sal=800000;
                   break;
    case 2 :     std::cout<<"\n DIRECTOR";
                   ba_sal=500000;
                   break;
    case 3 :     std::cout<<"\n GENERAL MANAGER";
                   ba_sal=300000;
                   break;
    case 4 :     std::cout<<"\n DEPUTY GENERAL MANAGER";
                   ba_sal=150000;
                   break;
    case 5 :     std::cout<<"\n CHIEF MANAGER";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<80000||ba_sal>100000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 6 :     std::cout<<"\n SENIOR EXECUTIVE";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<65000||ba_sal>85000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 7 :     std::cout<<"\n EXECUTIVE";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<50000||ba_sal>80000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 8 :     std::cout<<"\n OFFICER";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<40000||ba_sal>75000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 9 :     std::cout<<"\n CLERK";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<25000||ba_sal>40000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 10:     std::cout<<"\n SUB-STAFF";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<15000||ba_sal>25000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    default:    std::cout<<"Please Check Number.";
                ba_sal=0;
    }
    }while(ba_sal==0);
what has this code to do with your original question?

lines 20/21 can impossibly be right
Last edited on
oh! gimme a min.. i think i deleted a part of it by mistake..
double ba_sal, HRA, g_sal, sal;
int DA, TA, PF, IT, no2, no, no3;
std::string str, no1, age;
void CreRe();
void ViewRe();
void YrSal();
void EditSal();
do
{
do
{std::cout<<"Menu \n 1. Create Record \n 2. View All Records \n 3. Yearly Salary \n 4. Edit Salary \n 5. Exit \n Please enter the number:";
std::cin>>no;
switch(no)
{
case 1 : CreRe();
break;
case 2 : ViewRe();
break;
case 3 : YrSal();
break;
case 4 : EditSal();
break;
default: std::cout<<"Please Check Number Entered";
no=0;
}while(no==0);
getch();
return 0;
}
void CreRe();
{
std::cout<<"CREATE RECORDS \n";
std::cout<<"Enter Record Number: ";
std::cin>>no1;
std::cout<<"\n Enter name: ";
std::cin>>str;
std::cout<<"\n Enter age:";
std::cin>>age;
std::cout<<"\n 1. Managing Director \n 2. Director \n 3. General Manager \n 4. Deputy General Manager \n 5. Chief Manager \n 6. Senior Executive \n 7. Executive \n 8. Officer \n 9. Clerk \n 10. Sub-Staff \n Enter designation number : ";
std::cin>>no2;
ba_sal=0;
do
{
switch(no2)
{
case 1 : std::cout<<"\n MANAGING DIRECTOR";
ba_sal=800000;
break;
case 2 : std::cout<<"\n DIRECTOR";
ba_sal=500000;
break;
case 3 : std::cout<<"\n GENERAL MANAGER";
ba_sal=300000;
break;
case 4 : std::cout<<"\n DEPUTY GENERAL MANAGER";
ba_sal=150000;
break;
case 5 : std::cout<<"\n CHIEF MANAGER";
std::cout<<"\n Enter Pay:";
std::cin>>ba_sal;
if(ba_sal<80000||ba_sal>100000)
{
ba_sal=0;
std::cout<<"\n CHECK SALARY";
}
break;
case 6 : std::cout<<"\n SENIOR EXECUTIVE";
std::cout<<"\n Enter Pay:";
std::cin>>ba_sal;
if(ba_sal<65000||ba_sal>85000)
{
ba_sal=0;
std::cout<<"\n CHECK SALARY";
}
break;
case 7 : std::cout<<"\n EXECUTIVE";
std::cout<<"\n Enter Pay:";
std::cin>>ba_sal;
if(ba_sal<50000||ba_sal>80000)
{
ba_sal=0;
std::cout<<"\n CHECK SALARY";
}
break;
case 8 : std::cout<<"\n OFFICER";
std::cout<<"\n Enter Pay:";
std::cin>>ba_sal;
if(ba_sal<40000||ba_sal>75000)
{
ba_sal=0;
std::cout<<"\n CHECK SALARY";
}
break;
case 9 : std::cout<<"\n CLERK";
std::cout<<"\n Enter Pay:";
std::cin>>ba_sal;
if(ba_sal<25000||ba_sal>40000)
{
ba_sal=0;
std::cout<<"\n CHECK SALARY";
}
break;
case 10: std::cout<<"\n SUB-STAFF";
std::cout<<"\n Enter Pay:";
std::cin>>ba_sal;
if(ba_sal<15000||ba_sal>25000)
{
ba_sal=0;
std::cout<<"\n CHECK SALARY";
}
break;
default: std::cout<<"Please Check Number.";
ba_sal=0;
}
}while(ba_sal==0);
:
:
:
}
So you just started a do block, right there outside all functions?

Try and give a more realistic code sample, explain what this has to do with your question and use the code format tags...
i do it because i need it... you want me to post the whole code?

and also, it's just that i'm not able to use the globally declared variables in the function.. that is all. i don't think it has anything to do with the do-while loops.. :/
@coder777 asked:
what has this code to do with your original question?

You still haven't made that clear.

"and also, it's just that i'm not able to use the globally declared variables in the function.. that is all. i don't think it has anything to do with the do-while loops.. :/"

If indeed your problem is with scope, that is the more reason to show your code in context.

You can't have that do block there outside a function, with all the problems in the code you've posted, we can't see where you've gone wrong or which of the problems you're complaining about.

If indeed this is a fragment from inside a function, then you are defining functions within functions and you've got some thing like:
1
2
3
4
5
6
7
8
9
do
{
    do
    {
        siwtch()
        {
        } while
    return;
}


Please post meaningful and formatted code.
There is no switch/while construct in C++, so if this is indeed representative of your code, how can the do-while loops not be a contributing factor?
i have to make a project on mass management and i decided to make it on how salary is calculated here in India..
i could post the whole code if you want it.. i have a lot of errors.. but it's just tooooooo long and i don't want to bother you people.. if you give a thumbs up, i shall do that :)
For starters, just show a complete function, and say what the problem is, in relation to the function you've posted.
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
double ba_sal, HRA, g_sal, sal;
    int DA, TA, PF, IT, no2, no, no3;
    std::string str, no1, age;
    void CreRe();
    void ViewRe();
    void YrSal();
    void EditSal();
    do
        {std::cout<<"Menu \n 1. Create Record \n 2. View All Records \n 3. Yearly Salary \n 4. Edit Salary \n 5. Exit \n Please enter the number:";
    std::cin>>no;
switch(no)
{
    case 1 : CreRe();
             break;
    case 2 : ViewRe();
             break;
    case 3 : YrSal();
             break;
    case 4 : EditSal();
             break;
    default: std::cout<<"Please Check Number Entered";
             no=0;
}
    }while(no==0);
    getch();
    return 0;
        }
    void CreRe();
    {
    std::cout<<"CREATE RECORDS \n";
    std::cout<<"Enter Record Number: ";
    std::cin>>no1;
    std::cout<<"\n Enter name: ";
    std::cin>>str;
    std::cout<<"\n Enter age:";
    std::cin>>age;
    std::cout<<"\n 1. Managing Director \n 2. Director \n 3. General Manager \n 4. Deputy General Manager \n 5. Chief Manager \n 6. Senior Executive \n 7. Executive \n 8. Officer \n 9. Clerk \n 10. Sub-Staff \n Enter designation number : ";
    std::cin>>no2;
    ba_sal=0;
    do
    {
    switch(no2)
    {
    case 1 :     std::cout<<"\n MANAGING DIRECTOR";
                   ba_sal=800000;
                   break;
    case 2 :     std::cout<<"\n DIRECTOR";
                   ba_sal=500000;
                   break;
    case 3 :     std::cout<<"\n GENERAL MANAGER";
                   ba_sal=300000;
                   break;
    case 4 :     std::cout<<"\n DEPUTY GENERAL MANAGER";
                   ba_sal=150000;
                   break;
    case 5 :     std::cout<<"\n CHIEF MANAGER";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<80000||ba_sal>100000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 6 :     std::cout<<"\n SENIOR EXECUTIVE";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<65000||ba_sal>85000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 7 :     std::cout<<"\n EXECUTIVE";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<50000||ba_sal>80000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 8 :     std::cout<<"\n OFFICER";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<40000||ba_sal>75000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 9 :     std::cout<<"\n CLERK";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<25000||ba_sal>40000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 10:     std::cout<<"\n SUB-STAFF";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<15000||ba_sal>25000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    default:    std::cout<<"Please Check Number.";
                ba_sal=0;
    }
    }while(ba_sal==0);
if(no2>4&&no2<11)
{
   DA=ba_sal/10;
   HRA=ba_sal/4;
   TA=3000;
   PF=ba_sal/20;
}
else
{
    DA=0;
    HRA=0;
    TA=0;
    PF=ba_sal/20;
}
g_sal=ba_sal+DA+HRA+TA;
{
if((g_sal*12)-100000>800000)
{
IT=g_sal/3.3;
}
else
if((g_sal*12)-100000>500000&&(g_sal*12)-100000<800001)
{
IT=g_sal/5;
}
else
if((g_sal*12)-100000>200000&&(g_sal*12)-100000<500001)
{
IT=g_sal/10;
}
else
IT=0;
}
std::cout<<"\n Deductions = Rs."<<PF+IT;
sal=g_sal-PF-IT;
std::cout<<"\n Take-away salary = Rs."<<sal;
}


this is the code along with only one function.. i'm not able to use the variables like g_sal, PF and IT in the function's body... it says that the variable has not been declared in that scope...
This guy is trolling me right?
i think so.
just checked the calendar and it's definitely not April 1st..
Lets just say that for whatever reasons the communication is nigh impossible.

@aish: What is the name of the function that you have shown us in your latest post?
first of all.. i'm not a "guy" and second, i'm definitely not trolling -.- i'm just a beginner...a very recent one, i mean

the name of the function is CreRe...
if the function CreRe is all that you want, it's:
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
123
    void CreRe();
    {
    std::cout<<"CREATE RECORDS \n";
    std::cout<<"Enter Record Number: ";
    std::cin>>no1;
    std::cout<<"\n Enter name: ";
    std::cin>>str;
    std::cout<<"\n Enter age:";
    std::cin>>age;
    std::cout<<"\n 1. Managing Director \n 2. Director \n 3. General Manager \n 4. Deputy General Manager \n 5. Chief Manager \n 6. Senior Executive \n 7. Executive \n 8. Officer \n 9. Clerk \n 10. Sub-Staff \n Enter designation number : ";
    std::cin>>no2;
    ba_sal=0;
    do
    {
    switch(no2)
    {
    case 1 :     std::cout<<"\n MANAGING DIRECTOR";
                   ba_sal=800000;
                   break;
    case 2 :     std::cout<<"\n DIRECTOR";
                   ba_sal=500000;
                   break;
    case 3 :     std::cout<<"\n GENERAL MANAGER";
                   ba_sal=300000;
                   break;
    case 4 :     std::cout<<"\n DEPUTY GENERAL MANAGER";
                   ba_sal=150000;
                   break;
    case 5 :     std::cout<<"\n CHIEF MANAGER";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<80000||ba_sal>100000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 6 :     std::cout<<"\n SENIOR EXECUTIVE";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<65000||ba_sal>85000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 7 :     std::cout<<"\n EXECUTIVE";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<50000||ba_sal>80000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 8 :     std::cout<<"\n OFFICER";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<40000||ba_sal>75000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 9 :     std::cout<<"\n CLERK";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<25000||ba_sal>40000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    case 10:     std::cout<<"\n SUB-STAFF";
                   std::cout<<"\n Enter Pay:";
                   std::cin>>ba_sal;
                   if(ba_sal<15000||ba_sal>25000)
                   {
                       ba_sal=0;
                       std::cout<<"\n CHECK SALARY";
                   }
                   break;
    default:    std::cout<<"Please Check Number.";
                ba_sal=0;
    }
    }while(ba_sal==0);
if(no2>4&&no2<11)
{
   DA=ba_sal/10;
   HRA=ba_sal/4;
   TA=3000;
   PF=ba_sal/20;
}
else
{
    DA=0;
    HRA=0;
    TA=0;
    PF=ba_sal/20;
}
g_sal=ba_sal+DA+HRA+TA;
{
if((g_sal*12)-100000>800000)
{
IT=g_sal/3.3;
}
else
if((g_sal*12)-100000>500000&&(g_sal*12)-100000<800001)
{
IT=g_sal/5;
}
else
if((g_sal*12)-100000>200000&&(g_sal*12)-100000<500001)
{
IT=g_sal/10;
}
else
IT=0;
}
std::cout<<"\n Deductions = Rs."<<PF+IT;
sal=g_sal-PF-IT;
std::cout<<"\n Take-away salary = Rs."<<sal;
}


i just thought you'd want to see the variable declaration also..
- line 28: remove the semi-colon
- lines 32, 34, 36: need to include string:
#include <string>
at the top of the file.


- no2 declared as a string. dont you want that as an integer?

- dont use global variables.
- if you havent already, provide implementation for these:
1
2
3
void ViewRe();
    void YrSal();
    void EditSal();


or else your linker's gonna shout.




i need the variables to retain their value for the other functions too... how do i do that? return the values from the function CreRe?
i have included string.
and i have provided implementation for the functions before itself...
Last edited on
Remove semicolon from line 1.

See function reference parameters.
Pages: 12