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...
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:
#include <iostream>
usingnamespace 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...
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
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.
#include<iostream>
#include<fstream>
#include<conio.h>
#include<string>
usingnamespace 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..
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) ===|
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++)
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):