Variables

Pages: 12
i've done that :)
please tell me where to declare the variables and what to do.. i need to use them in the whole programme and my C++ textbook provides very limited knowledge about that...
@aish96
Apologies, didn't mean to offend you.

I'd like to repeat the importance of context here. If I can't see where/how your variables are declared in relation to how/where they're being used, then I can't offer meaningful help as to why they're suddenly unavailable.

From your original statement:
"i use code::blocks. I am not able to use a variable that i've declared globally in the function definition.. "
and this code fragment:
double ba_sal, HRA, g_sal, sal;

It appears you've defined your variables within another function, this makes them local to that function and therefore not available globally.

Also, as mentioned above, you don't have to rely on global variables. You can indeed pass and return in many different ways. A better understanding of the required scope and lifetime of these variables would yield the most helpful suggestions on how to accomplish this.
ohh.. that's where i made a mistake. sorry.. i meant to say that i've declared variables globally and i'm not able to use them in the function CreRe's definition..
i need to use all the variables in another function, and code blocks does not allow me to globally defined variables and use them in functions..

i've defined the variable no1 globally and used it in the definition of the function CreRe...this is the error i get: 'no1' was not declared in this scope
Here's a complete file, showing a contrived and trivial example and using global variables. Mind you I'm still not advocating this ideology, I'm not trying to be patronising either, I really hope this helps:

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
#include <iostream>

using namespace std;

// global variables
int g_one;
int g_two;

// function that uses the global variables
void func1()
{
	// use the global variables
	g_one *= 2;
	g_two *= 4;
}

// function that uses the global variables
void func2()
{
	// use the global variables
	g_one -= 10;
	g_two -= 20;
}

// function that uses the global variables
void dump()
{
	// use the global variables
	cout << "g_one = " << g_one << endl;
	cout << "g_two = " << g_two << endl;
}

int main()
{
	// initialise the global variables (ha ha)
	g_one = 14;
	g_two = 15;
	
	// call functions that will use the global variables
	func1();
	dump();
	func2();
	dump();
	func1();
	dump();
	func2();
	dump();
	
	return 0;
}
the user has to input the values of the variables. and that is exactly how my programme is, except for the fact that the body of int main() comes first and then the function definitions...
If your code is structured like the example from tipaye, then it should work. There's clearly something you are not telling us.
idk what more is there to tell.. i'm still not getting the fault of the program... :/
as i'm just a beginner, i don't know what could affect the scope of a variable. if someone could tell me what, apart from it being declared in a function (as i've declared in the body of int main()), could affect the scope, i'd be exhilarated
Last edited on
Well, you've posted various fragments of code, but not enough to illustrate the problem. I appreciate that the full code might be quite long, but that might be the only way to get to the bottom of the problem.

Or how about you post the first 50 lines of the program, unedited - that might be enough to give some clues.
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
#include<iostream>
#include<fstream>
#include<conio.h>
#include<string>
int main()
{
    double ba_sal, HRA, g_sal, sal;
    int DA, TA, PF, IT, no, no1, no2, no3;
    std::string str, 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);


Take a closer look at the previous responses.

If you declare a variable in main() it is not global.

A global variable is declared outside all functions.
1
2
3
4
5
6
7
8
#include<iostream>
#include<fstream>
#include<conio.h>
#include<string>
using namespace std;
double ba_sal, HRA, g_sal, sal;
int DA, TA, PF, IT, no, no3;
std::string str, age;


am i supposed to declare it like this? because if it is a yes, the same error is given.. please forgive my ignorance but i am new. in fact, i didn't know that main() was a function :P i noticed that only when you mentioned it.. as i mentioned earlier, my c++ text book provides very limited information..
Can you paste the actual error message verbatim, in it's entirety?
F:\Aishwarya\sdxfch\main.cpp||In function 'void CreRe()':|
F:\Aishwarya\sdxfch\main.cpp|39|error: 'no1' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp|45|error: 'no2' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp||In function 'void ViewRe()':|
F:\Aishwarya\sdxfch\main.cpp|160|error: 'no1' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp|165|error: 'no2' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp|167|error: 'no2' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp|169|error: 'no2' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp|171|error: 'no2' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp|173|error: 'no2' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp|175|error: 'no2' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp|177|error: 'no2' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp|179|error: 'no2' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp|181|error: 'no2' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp|183|error: 'no2' was not declared in this scope|
F:\Aishwarya\sdxfch\main.cpp||In function 'void EditSal()':|
F:\Aishwarya\sdxfch\main.cpp|210|error: 'no2' was not declared in this scope|
||=== Build finished: 14 errors, 0 warnings (0 minutes, 0 seconds) ===|
Last edited on
In the fragment you posted before this, you didn't actually declare no1 or no2, you only did no and no3.
Also, if your textbook is that bad, consider a better one. There are loads of C++ books, tutorials, wikis, etc. available for free on-line, there's even a tutorial on this very site (http://www.cplusplus.com/doc/tutorial/).

I remember reading Bruce Eckel's book many many years ago, it was a cracking good read, and excellent both as a tutorial and a reference and is distributed freely on-line (google thinking in C++)
ohhh oh God!!!! sorry!!! my bad! i'm really lazy, so sorry.. :P and sorry for troubling all of you

i can't thank you enough :D THANKS THANKS THANKS :D
actually, that is my school text book :P so i really can't do that.
Hi aish96,

Tipaye is doing great here, but you seem to have written a lot of code then tried to build it, and then gotten a bit stuck so trying to walk you through fixing it is quite difficult.

perhaps you could begin again and use your code here as a reference, since functionally your code seems sensible, but structurally it is just a bit muddled up.

Firstly you should really write your main and get it to print something simple and make sure everything works:

1
2
3
4
5
6
7
8
9
#include<iostream>
#include<fstream>
#include<conio.h>
#include<string>

int main()
{
 std::cout<<"Test";
 }


Then your global vars and your function declarations need to be declared before the main and you define your functions pretty much where ever you want as long as they are in correct scope note(not inside another function):

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
#include<iostream>
#include<fstream>
#include<conio.h>
#include<string>

void CreRe();
void ViewRe();
void YrSal();
void EditSal();

double ba_sal, HRA, g_sal, sal;
int DA, TA, PF, IT, no, no1, no2, no3;
std::string str, age;

int main()
{
 std::cout<<"Test";
 }

void CreRe()
{
}

void ViewRe()
{
}

void YrSal()
{
}

void EditSal()
{
}


if you build this and it works, then you can migrate your code into it, piece by piece, rebuilding and testing as you go. .

Thanks -NGangst





Last edited on
i've almost finished it :)
Topic archived. No new replies allowed.
Pages: 12