local variables

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2004
Posts: 21
Reputation: ze_viru$ is an unknown quantity at this point 
Solved Threads: 0
ze_viru$'s Avatar
ze_viru$ ze_viru$ is offline Offline
Newbie Poster

local variables

 
0
  #1
Aug 12th, 2006
Hi,

below is my unfinished code.what i realy want help on is how do i display or take the values of a and b to main?
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int ReadInput (int,int);
  6.  
  7. float CalcIceCreamCost ();
  8.  
  9. float CalcDeliveryCost ();
  10.  
  11. float DisplayCosts ();
  12.  
  13. int main()
  14. {
  15. int flava,cart,delivery;
  16.  
  17. flava = 0;
  18. cart = 0;
  19.  
  20. cout<<" Mulaudzi Ice-Cream Limited\n"
  21. <<" --------------------------\n";
  22.  
  23. ReadInput(flava,cart);
  24.  
  25. cout<<"Ur flava is : "<<flava<<endl;
  26. cout<<"Ur # of cartoons is : "<<cart<<endl;
  27.  
  28. return 0;
  29.  
  30. }
  31.  
  32. int ReadInput (int a,int b)
  33. {
  34.  
  35. cout<<"Please enter flavour(1=choco,2=caramel,3=mint) : ";
  36. cin>>a;
  37.  
  38. cout<<"Please enter number of cartoons(1-20) : ";
  39. cin>>b;
  40.  
  41. return a,b;
  42.  
  43. }
[i]What makes the world to be a dangerous place is not the people who do evil,but is the people who just sit on their buds. :)
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 489
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: local variables

 
0
  #2
Aug 12th, 2006
You can't return multiple values from a function, you probably want to pass your variables by reference instead. ie
void ReadInput (int& a,int& b)
{
	
	cout<<"Please enter flavour(1=choco,2=caramel,3=mint) : ";
	cin>>a;

	cout<<"Please enter number of cartoons(1-20) : ";
	cin>>b;
}

Notice the ampersands (highlighted in red), to denote 'pass by reference' ... also, the function has no retun statement, since it is modifying variables elsewhere in the prgram and not returning a value, hence the function's return type is now void
Last edited by Bench; Aug 12th, 2006 at 7:49 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: local variables

 
0
  #3
Aug 12th, 2006
Also change the prototype of function to
void ReadInput (int&,int&);
I guess it's pretty obvious but I am still reminding.
Last edited by Grunt; Aug 12th, 2006 at 8:30 am.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: local variables

 
0
  #4
Aug 13th, 2006
My preferred solution:
  1. #include<iostream>
  2. using namespace std;
  3. int ReadInputFlava ();
  4. int ReadInputCart ();
  5.  
  6. int main()
  7. {
  8. int flava,cart,delivery;
  9.  
  10. flava = 0;
  11. cart = 0;
  12.  
  13. cout<<" Mulaudzi Ice-Cream Limited\n"
  14. <<" --------------------------\n";
  15.  
  16. flava = ReadInputFlava();
  17. cart = ReadInputCart();
  18.  
  19. cout<<"Ur flava is : "<<flava<<endl;
  20. cout<<"Ur # of cartoons is : "<<cart<<endl;
  21.  
  22. return 0;
  23.  
  24. }
  25.  
  26. int ReadInputFlava ()
  27. {
  28. int a;
  29. cout<<"Please enter flavour(1=choco,2=caramel,3=mint) : ";
  30. cin>>a;
  31.  
  32. return a;
  33. }
  34.  
  35.  
  36. int ReadInputCart ()
  37. {
  38. int b;
  39. cout<<"Please enter number of cartoons(1-20) : ";
  40. cin>>b;
  41.  
  42. return b;
  43. }
This makes the program a little more modular. And I prefer not to pass values via the parameter list unless it can't be avoided. Personal choice.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,623
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: local variables

 
0
  #5
Aug 14th, 2006
Originally Posted by WaltP
This makes the program a little more modular.
Well forgive me for butting in, but here modularity is not an issue since he is not building a Library module but a small program.

As far as writing normal programs are concerned the coder should always be concerned with the best and the fast implementataion wrt to both memory requirements and speed.

But still this is just me, I thought just wanted to let you know.
Any constructive criticisms appreciated.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 489
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: local variables

 
0
  #6
Aug 15th, 2006
Originally Posted by ~s.o.s~
Well forgive me for butting in, but here modularity is not an issue since he is not building a Library module but a small program.

As far as writing normal programs are concerned the coder should always be concerned with the best and the fast implementataion wrt to both memory requirements and speed.

But still this is just me, I thought just wanted to let you know.
Any constructive criticisms appreciated.
Modularity doesn't mean that its a library module, he means breaking a program down into smaller, more managable chunks. I agree that it doesn't matter so much for a small toy program, but since maintainability and readability are generally the 2 most important goals for any coder to aspire to (usually far more important than optimum speed & memory usage), its a good habit to get into.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,623
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: local variables

 
0
  #7
Aug 15th, 2006
Originally Posted by Bench
Maintainability and readability are generally the 2 most important goals for any coder to aspire to (usually far more important than optimum speed & memory usage), its a good habit to get into.
Actully that is not true in all the cases.
For places or applications where the performance is of peak importance, optimum speed and memory do make a large amount of difference.

I dont mean that modularty is not important or you should go for speed more than modularity, just that it depends on the area of application in which the program is used.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: local variables

 
0
  #8
Aug 15th, 2006
speed is sometimes much more important than modularity or maintainability. I worked several years on a program that had to get data real-time from barcode readers on packages as the packages moved along an assembly line. Then the program had to look-up data in a database whose key was the barcode, format the data, and send it to a large-character printer that would print information on the side of the packages when they moved past the printer. All that had to be done in less than 1/2 second and management thought that was too slow. We even resorted to assembly language to squeeze out every millisecond we could.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,623
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: local variables

 
0
  #9
Aug 15th, 2006
Originally Posted by Ancient Dragon
speed is sometimes much more important than modularity or maintainability. I worked several years on a program that had to get data real-time from barcode readers on packages as the packages moved along an assembly line. Then the program had to look-up data in a database whose key was the barcode, format the data, and send it to a large-character printer that would print information on the side of the packages when they moved past the printer. All that had to be done in less than 1/2 second and management thought that was too slow. We even resorted to assembly language to squeeze out every millisecond we could.
Thats exactly the point i was trying to put forward, but as i belong to the new generation couldnt find a real world example. Thanks, this was the kind of example i was looking for.

*but in the end these ppl will say that nowadays as the computing speed and memory has increased the optimizations dont matter. Well in this case the argument continues...*
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 489
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: local variables

 
0
  #10
Aug 16th, 2006
Originally Posted by ~s.o.s~
Actully that is not true in all the cases.
Which is exactly the point i was making, there are no "always do" or "never do" rules. it depends entirely on the platform you're developing for
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC