473,378 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

variables in functions

5
if someone could check this out... i've been working on this function, try to focus on 'InstCarpTot' as a main one, i keep getting errors that it is being used without being initialized and i don't know what to do to fix it. i'm pretty new to functions and am not familiar with how they work. thanks


Expand|Select|Wrap|Line Numbers
  1. //declaring functions
  2. void GetDimensions(int &Length, int &Width);
  3. int CalculateArea(int Length, int Width);
  4. void GetDiscount(double &Discount);
  5. void GetUnit_Price(double &Unit_Price);
  6. double CalInstCarpTot(int Area, double Unit_Price);
  7. double Subtotal(double InstCarpTot, double Discount);
  8. double Total(double Subtot);
  9. void PrintMeasurements(int Length, int Width);
  10. void PrintCharges(int Area, double Discount);
  11.  
  12.  
  13. int main()
  14. {
  15.     int Length, Width, Area;
  16.     double Discount, Unit_Price, InstCarpTot, Subtot;
  17.  
  18.  
  19.  
  20.  
  21. //print functions
  22.     GetDimensions (Length, Width);
  23.     Area=CalculateArea(Length, Width);
  24.  
  25.     cout << Area << endl;
  26.     GetDiscount(Discount);
  27.  
  28.     GetUnit_Price(Unit_Price);
  29.     CalInstCarpTot(Area, Unit_Price);
  30.     Subtotal(InstCarpTot, Discount);
  31.     Total(Subtot);
  32.     PrintMeasurements(Length, Width);
  33.     PrintCharges( Area, Discount);
  34.  
  35.     return 0;
  36. }
  37.  
  38. //get the total cost for installation 
  39. double CalInstCarpTot(int Area, double Unit_Price)
  40. {
  41.     double TotLabor, InstCarpTot, TotUnit_Price;
  42.  
  43.     const double Labor=0.42;
  44.     TotLabor=Labor*Area;
  45.     TotUnit_Price=Unit_Price*Area;
  46.     InstCarpTot=TotUnit_Price+TotLabor;
  47.  
  48.     return InstCarpTot;
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55. //print the costs using variables entered by user
  56. void PrintCharges(int Area, double Discount)
  57. {
  58.     double InstCarpTot;
  59.     cout << setw(13) <<left <<"Carpet w/ Labor" << setw(26) << right<< InstCarpTot <<endl;
Oct 15 '07 #1
6 1431
Shashi Sadasivan
1,435 Expert 1GB
Hi,
that should give a u a compile time warning, (not an error)
Just set default values to your variables.

cheers
Oct 15 '07 #2
mng2nf
5
Hi,
that should give a u a compile time warning, (not an error)
Just set default values to your variables.

cheers
thanks but i need the program to work with variables inputted by the user, and the program gives warnings but it doesn't run when it hits the 'unitinialized' variables
Oct 15 '07 #3
Shashi Sadasivan
1,435 Expert 1GB
Hi,
What i meant was, change your code from:
Expand|Select|Wrap|Line Numbers
  1. int Length, Width, Area;
  2. double Discount, Unit_Price, InstCarpTot, Subtot;
to
Expand|Select|Wrap|Line Numbers
  1. int Length=0, Width=0, Area=0;
  2. double Discount=0, Unit_Price=0, InstCarpTot=0, Subtot=0;
And you should be allright.

cheers
Oct 15 '07 #4
mng2nf
5
Hi,
What i meant was, change your code from:
Expand|Select|Wrap|Line Numbers
  1. int Length, Width, Area;
  2. double Discount, Unit_Price, InstCarpTot, Subtot;
to
Expand|Select|Wrap|Line Numbers
  1. int Length=0, Width=0, Area=0;
  2. double Discount=0, Unit_Price=0, InstCarpTot=0, Subtot=0;
And you should be allright.

cheers
:/ when i do that it makes all of my variables into 0 instead of what was calculated by the program
Oct 15 '07 #5
Shashi Sadasivan
1,435 Expert 1GB
Well, the variables have just been initialised at this line,
How come it already got calculated before being initialized? (ding ding)

EDITED:
Hi,
Its been a long time since ive seen C/C++ code (im into C#)
but isint
Expand|Select|Wrap|Line Numbers
  1. GetDimensions (Length, Width);
supposed to be
Expand|Select|Wrap|Line Numbers
  1. GetDimensions (*Length, *Width);
??
cheers
Oct 15 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
thanks but i need the program to work with variables inputted by the user, and the program gives warnings but it doesn't run when it hits the 'unitinialized' variables
That's fine. But using variables that have never neen initialized os a major source of error. Major compilers, like Visual Studio.NET give warnings about using uninitialized variables. That is, to make a function call using one of those variables require that a copy be made. You cannpt reliably copy an uninitialized variable. There's not guarantee the function will intialize it making a crash not farr away.

Just provide a default value, then go ahead and ask for user input.
Oct 15 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Angelos | last post by:
hello there... I would like to ask if there is any way of using variables which are outside functions in the functions without passing them when I call the function... Complicated eehh ? I have...
2
by: Paul M | last post by:
Hi, This is on an AS/400 which can be a little strange but I think the basic question is portable. I have a (non-C) program that needs to make series of calls to some C programs/functions....
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
3
by: darrel | last post by:
This is something I should know, but I don't. Say I have this: Function dim variable1 dim variable2 do stuff with the variables
12
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
10
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.