Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2006, 9:37 PM   #11
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
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().
grumpy is offline   Reply With Quote
Old Apr 9th, 2006, 10:01 PM   #12
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
int time=getStartTime();
int length=getLengthofCall();
double gross=calcGross();
double net=calcNet();
char input=0;
@nindoja: This sort of declaration and assignment combined (particularly with the assigned value being the return of a function) should not be used with C unless one is absolutely sure the compiler will tolerate it.

@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
DaWei is offline   Reply With Quote
Old Apr 9th, 2006, 10:09 PM   #13
Barb
Newbie
 
Join Date: Apr 2006
Posts: 6
Rep Power: 0 Barb is on a distinguished road
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
Barb is offline   Reply With Quote
Old Apr 9th, 2006, 10:57 PM   #14
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by Barb
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.
Yup. And, as I explained in my last post, you throught wrong.

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:
Originally Posted by Barb
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
People in this thread have already tried to make suggestions. The basic problem is that you don't have enough understanding of C to realise that.

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 */
}
This only works for providing data to a function. The change of "value" inside Function does not make 2 equal to 42, and it does not change x (inside main) to have a value 42.

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.
grumpy is offline   Reply With Quote
Old Apr 10th, 2006, 8:15 PM   #15
nindoja
Programmer
 
Join Date: Jun 2005
Posts: 92
Rep Power: 4 nindoja is on a distinguished road
Quote:
Originally Posted by DaWei
int time=getStartTime();
int length=getLengthofCall();
double gross=calcGross();
double net=calcNet();
char input=0;
@nindoja: This sort of declaration and assignment combined (particularly with the assigned value being the return of a function) should not be used with C unless one is absolutely sure the compiler will tolerate it.
Dawei is right, I forgot that in C one must declare all variables first, then use them in code.
nindoja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC