![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Sigh.
You are incorrectly assuming that simply declaring a variable is enough to be able to use it wherever you want it. For example, you have a variable named "gross" in multiple functions. The problem is, you are not passing its value between those functions. CalcGross() calculates a value, and even returns it, but the main() program does nothing with the value returned. The variable named gross in CalcNet() is only visible within CalcNet(), and is a TOTALLY different variable from the variable named gross that is used in CalcGross(). |
|
|
|
|
|
#12 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
int time=getStartTime(); int length=getLengthofCall(); double gross=calcGross(); double net=calcNet(); char input=0; @barb: You need to review some basics. You don't need help with errors, so much, as you need to learn some simple things about the language.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#13 |
|
Newbie
Join Date: Apr 2006
Posts: 6
Rep Power: 0
![]() |
I've changed my code and no errors. I am really new at C, so please just bear with me. I thought that as long as you declared something that it could be used anywhere in the program. Since I am using gross from CalcGross() to get a calculation of gross=length*rate*discounta, that the total of that could be used in the net for CalcNet() of net = gross*tax+gross - if this is not true, then how can I fix it. Any suggestions is greatly appreciated - Thanks for all of the help
|
|
|
|
|
|
#14 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
As Dawei said, you are misunderstanding one of the absolute fundamentals of how C works. You need to go back and, if you are using a textbook, read the first couple of chapters repeatedly until you actually understand what they say. Quote:
A basic rule in C is that variables declared within a function are local to that function --- they are not generally accessible outside it. There are four broad approaches if you want to pass data between functions. 1) Pass values as arguments by value to the function. For example a function of the form; void Function(int value)
{
/* do something with value */
value = 42;
}
int main()
{
int x = 5;
Function(2); /* Function will receive a value of 2 */
Function(x); /* Function will receive a value of 5 */
}2) Pass a pointer as an argument to a function; void Function2(int *value)
{
int x = *value; /* x now has the value who's address was passed */
*value = 42; /*
}
int main()
{
int x = 5;
Function2(&x); /* Function will receive a value of 5 */
/* x will now have a value of 42 */
}3) Return a value from a function; int Function3()
{
int x = 10;
return x;
}
int main()
{
int x = 20;
int y = 30;
y = Function3();
/* x will have a value of 20 and y will have a value of 10 */
}4) The fourth option (using global variables) is one I will not describe for you until you have shown better understanding. Global variables offer a solution to your problem but an extremely poor one. And, because of your current lack of understanding, it is the solution you would latch onto. |
||
|
|
|
|
|
#15 | |
|
Programmer
Join Date: Jun 2005
Posts: 92
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|