Help referencing variables

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 4
Reputation: Lorita is an unknown quantity at this point 
Solved Threads: 0
Lorita Lorita is offline Offline
Newbie Poster

Help referencing variables

 
0
  #1
Feb 12th, 2005
Hi, I need help changing this code to reference the variables rather than using the return statement. This is as far as I have gotten and my brain is mush...Please help. Thanks!

#include <iostream.h>
#include <conio.h>

void main()
{

int numDrawers;
int x=0;
double total;

//int getDrawers();
//int getWood();
double getTotal(int &numDrawers,int &x);
void PrintPrice(double &total);

numDrawers = getDrawers();

x = getWood();

total = getTotal(numDrawers,x);

PrintPrice(total);

}

int getDrawers()
{
int numDrawers;
cout <<"Enter the number of drawers you would like."<<endl;
cin >> numDrawers;
//return numDrawers*30;
}

int getWood()
{
char x;
int i=0;
int j=0;
char woods[3] = {'M', 'O', 'P'};
int cost[3] = { 180, 140, 100};

cout<<"Please choose the wood you would like for your desk;"<<endl;
cout<<"M for Mahogany, O for Oak, P for Pine."<<endl;
cin >> x;

for (i=0; i<=3; i++)
{ if (x==woods[i])
j=i;
}

//return cost[j];
}


double getTotal(int &numDrawers, int &x)
{
double c=0;

c=numDrawers + x;

//return c;
}

void PrintPrice(double &total)
{

cout << "The price of this desk is $"<<total<<endl;
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help referencing variables

 
0
  #2
Feb 12th, 2005
  1. void getDrawers(int &numDrawers)
  2. {
  3. cout <<"Enter the number of drawers you would like."<<endl;
  4. cin >> numDrawers;
  5. }
  6.  
  7. int main()
  8. {
  9. int numDrawers;
  10. getDrawers(numDrawers);
  11. // ...
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Help referencing variables

 
0
  #3
Feb 12th, 2005
(for something this small, you could simply make the numDrawers global...)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help referencing variables

 
0
  #4
Feb 12th, 2005
Originally Posted by winbatch
(for something this small, you could simply make the numDrawers global...)
Yes -- let's teach bad habits from the get-go, and skip the intent of the assignment. :rolleyes:
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 4
Reputation: Lorita is an unknown quantity at this point 
Solved Threads: 0
Lorita Lorita is offline Offline
Newbie Poster

Re: Help referencing variables

 
0
  #5
Feb 14th, 2005
I still can't get it! I have been messing with this for a week now and just can't figure it out. I've looked through my C++ book a zillion times, I've checked tons of code on the web, I don't know what I'm doing wrong. Does anyone know this kind of stuff? Thanks, L

  1.  
  2. #include <iostream.h>
  3. #include <conio.h>
  4.  
  5. void main()
  6. {
  7.  
  8. int numDrawers;
  9. int x=0;
  10. double total;
  11.  
  12. int getDrawers();
  13. int getWood();
  14. int getTotal(int &numDrawers,char &x);
  15. void PrintPrice(int &total);
  16.  
  17. x = getWood();
  18.  
  19. total = getTotal(numDrawers,x);
  20.  
  21. PrintPrice(total);
  22.  
  23. }
  24.  
  25. void getDrawers(int &numDrawers)
  26. {
  27. cout <<"Enter the number of drawers you would like."<<endl;
  28. cin >> numDrawers;
  29. }
  30.  
  31. void main()
  32. {
  33. int numDrawers;
  34. getDrawers(numDrawers*30);
  35.  
  36. }
  37.  
  38. void getWood(char &x, int &i, int &j, char &woods, int &cost)
  39. {
  40. cout<<"Please choose the wood you would like for your desk;"<<endl;
  41. cout<<"M for Mahogany, O for Oak, P for Pine."<<endl;
  42. cin >> x;
  43.  
  44. char x;
  45. int i=0;
  46. int j=0;
  47. char woods[3] = {'M', 'O', 'P'};
  48. int cost[3] = { 180, 140, 100};
  49.  
  50.  
  51.  
  52. for (i=0; i<=3; i++)
  53. { if (x==woods[i])
  54. j=i;
  55. }
  56.  
  57. void getTotal(int &numDrawers, int &x)
  58. {
  59. double c=0;
  60.  
  61. c=numDrawers + x;
  62.  
  63. }
  64.  
  65. void PrintPrice(double &total)
  66. {
  67.  
  68. cout << "The price of this desk is $"<<total<<endl;
  69. }
  70.  
  71.  
  72. }

Here are the latest errors...
  1. --------------------Configuration: CalcTwo - Win32 Debug--------------------
  2. Compiling...
  3. CalcTwo.cpp
  4. C:\Programming C++\Chapter04\CalcTwo.cpp(20) : error C2664: 'getTotal' : cannot convert parameter 2 from 'int' to 'char &'
  5. A reference that is not to 'const' cannot be bound to a non-lvalue
  6. C:\Programming C++\Chapter04\CalcTwo.cpp(22) : error C2664: 'PrintPrice' : cannot convert parameter 1 from 'double' to 'int &'
  7. A reference that is not to 'const' cannot be bound to a non-lvalue
  8. C:\Programming C++\Chapter04\CalcTwo.cpp(33) : error C2084: function 'void __cdecl main(void)' already has a body
  9. C:\Programming C++\Chapter04\CalcTwo.cpp(35) : error C2664: 'getDrawers' : cannot convert parameter 1 from 'int' to 'int &'
  10. A reference that is not to 'const' cannot be bound to a non-lvalue
  11. C:\Programming C++\Chapter04\CalcTwo.cpp(45) : error C2082: redefinition of formal parameter 'x'
  12. C:\Programming C++\Chapter04\CalcTwo.cpp(46) : error C2082: redefinition of formal parameter 'i'
  13. C:\Programming C++\Chapter04\CalcTwo.cpp(47) : error C2082: redefinition of formal parameter 'j'
  14. C:\Programming C++\Chapter04\CalcTwo.cpp(48) : error C2082: redefinition of formal parameter 'woods'
  15. C:\Programming C++\Chapter04\CalcTwo.cpp(49) : error C2082: redefinition of formal parameter 'cost'
  16. C:\Programming C++\Chapter04\CalcTwo.cpp(54) : error C2109: subscript requires array or pointer type
  17. C:\Programming C++\Chapter04\CalcTwo.cpp(59) : error C2601: 'getTotal' : local function definitions are illegal
  18. C:\Programming C++\Chapter04\CalcTwo.cpp(67) : error C2601: 'PrintPrice' : local function definitions are illegal
  19. Error executing cl.exe.
  20.  
  21. CalcTwo.obj - 12 error(s), 0 warning(s)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help referencing variables

 
0
  #6
Feb 14th, 2005
C:\Programming C++\Chapter04\CalcTwo.cpp(20) : error C2664: 'getTotal' : cannot convert parameter 2 from 'int' to 'char &'
The compiler is really trying its damnedest to tell you exactly what you did.
int x=0;
double total;

int getDrawers();
int getWood();
int getTotal(int &numDrawers,char &x);
void PrintPrice(int &total);

x = getWood();

total = getTotal(numDrawers,x);
C:\Programming C++\Chapter04\CalcTwo.cpp(22) : error C2664: 'PrintPrice' : cannot convert parameter 1 from 'double' to 'int &'
int numDrawers;
int x=0;
double total;

int getDrawers();
int getWood();
int getTotal(int &numDrawers,char &x);
void PrintPrice(int &total);

x = getWood();

total = getTotal(numDrawers,x);

PrintPrice(total);
C:\Programming C++\Chapter04\CalcTwo.cpp(33) : error C2084: function 'void __cdecl main(void)' already has a body
void main()
{

   int numDrawers;
   int x=0;
   double total;

   int getDrawers();
   int getWood();
   int getTotal(int &numDrawers,char &x);
   void PrintPrice(int &total);

   x = getWood();

   total = getTotal(numDrawers,x);

   PrintPrice(total);

}

void getDrawers(int &numDrawers)
{
   cout <<"Enter the number of drawers you would like."<<endl;
   cin >> numDrawers;
}

void main()
{
   int numDrawers;
   getDrawers(numDrawers*30);

}
C:\Programming C++\Chapter04\CalcTwo.cpp(35) : error C2664: 'getDrawers' : cannot convert parameter 1 from 'int' to 'int &'
This one is a little more difficult to find. By calculating a result, you are passing a value and not the reference.
  1. int numDrawers;
  2. getDrawers(numDrawers*30);
C:\Programming C++\Chapter04\CalcTwo.cpp(45) : error C2082: redefinition of formal parameter 'x'
C:\Programming C++\Chapter04\CalcTwo.cpp(46) : error C2082: redefinition of formal parameter 'i'
C:\Programming C++\Chapter04\CalcTwo.cpp(47) : error C2082: redefinition of formal parameter 'j'
C:\Programming C++\Chapter04\CalcTwo.cpp(48) : error C2082: redefinition of formal parameter 'woods'
C:\Programming C++\Chapter04\CalcTwo.cpp(49) : error C2082: redefinition of formal parameter 'cost'
C:\Programming C++\Chapter04\CalcTwo.cpp(54) : error C2109: subscript requires array or pointer type
void getWood(char &x, int &i, int &j, char &woods, int &cost) 
{
	cout<<"Please choose the wood you would like for your desk;"<<endl;
	cout<<"M for Mahogany, O for Oak, P for Pine."<<endl;
	cin >> x;

	char x;
	int i=0;
	int j=0;
	char woods[3] = {'M', 'O', 'P'};
	int cost[3] = { 180, 140, 100};
C:\Programming C++\Chapter04\CalcTwo.cpp(59) : error C2601: 'getTotal' : local function definitions are illegal
C:\Programming C++\Chapter04\CalcTwo.cpp(67) : error C2601: 'PrintPrice' : local function definitions are illegal
If you learn to indent better, this is quite obvious.
void getWood(char &x, int &i, int &j, char &woods, int &cost)
{
   cout<<"Please choose the wood you would like for your desk;"<<endl;
   cout<<"M for Mahogany, O for Oak, P for Pine."<<endl;
   cin >> x;

   char x;
   int i=0;
   int j=0;
   char woods[3] = {'M', 'O', 'P'};
   int cost[3] = { 180, 140, 100};



   for ( i=0; i<=3; i++ )
   {
      if ( x==woods[i] )
         j=i;
   }

   void getTotal(int &numDrawers, int &x)
   {
      double c=0;

      c=numDrawers + x;

   }

   void PrintPrice(double &total)
   {

      cout << "The price of this desk is $"<<total<<endl;
   }


}
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 4
Reputation: Lorita is an unknown quantity at this point 
Solved Threads: 0
Lorita Lorita is offline Offline
Newbie Poster

Re: Help referencing variables

 
0
  #7
Feb 14th, 2005
I'm new to all of this and none of it is obvious to me. I'm taking this online and have figured out that I can't learn this way. I contacted the tutor but she hasn't written back & I'm freaking out.

I fixed the int, char & double thing I had going on (I think). But from where you had the comment on:
  1. int numDrawers;
  2. getDrawers(numDrawers*30);
I still don't know what you mean. Where do I calculate the value if I can't do it there?

Your last two comments have me lost. You wrote I'd see it if I indented right (but I don't), and you just highlighted the char woods and it's variables. Am I supposed to do something with that area?

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Help referencing variables

 
0
  #8
Feb 14th, 2005
Originally Posted by Lorita
I'm new to all of this and none of it is obvious to me. I'm taking this online and have figured out that I can't learn this way. I contacted the tutor but she hasn't written back & I'm freaking out.

I fixed the int, char & double thing I had going on (I think). But from where you had the comment on:
  1. int numDrawers;
  2. getDrawers(numDrawers*30);
I still don't know what you mean. Where do I calculate the value if I can't do it there?
How about calling getDrawers to get the number of drawers, and then multiplying the result by 30?
  1. int numDrawers;
  2. getDrawers(numDrawers);
  3. numDrawers *= 30;
Originally Posted by Lorita
Your last two comments have me lost. You wrote I'd see it if I indented right (but I don't), and you just highlighted the char woods and it's variables. Am I supposed to do something with that area?
Your comment here is associated wrongly. The indenting business is where you define two functions within another function. Properly indented, this sticks out like a sore thumb.

The stuff above that with the variables highlighted was in response to the "redefinition" errors. Which woods is woods? The function parameter or the local in the function? Which x is x, etc?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 21
Reputation: compshooter is an unknown quantity at this point 
Solved Threads: 0
compshooter's Avatar
compshooter compshooter is offline Offline
Newbie Poster

Re: Help referencing variables

 
0
  #9
Feb 26th, 2005
Hey, I am pretty new to this site and have learned a lot of C++ with the help of these kind folks. I looked at your problem and attempted to solve it myself. Here is what I got. It seems to work for me.

BTW, working on your problem helped my understand of reference variables! It was fun!

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void getDrawers(int *numDrawers);
  6. void getWood(int *woodFactor);
  7. double getTotal(int numDrawers,int woodfactor,double *total);
  8. void PrintPrice(double total);
  9.  
  10. int main()
  11. {
  12. int numDrawers=0, woodFactor=0;
  13. double total=0;
  14. getDrawers(&numDrawers);
  15. getWood(&woodFactor);
  16. getTotal(numDrawers,woodFactor,&total);
  17. PrintPrice(total);
  18. cin.get();
  19. return (0);
  20. };
  21.  
  22. void getDrawers(int *numDrawers)
  23. {
  24. int num;
  25. cout<<"Enter the number of drawers you would like."<<endl;
  26. cin>>num;
  27. *numDrawers = num;
  28.  
  29. }
  30.  
  31. void getWood(int *woodFactor)
  32. {
  33. char x;
  34. int i=0;
  35. char woods[3] = {'M', 'O', 'P'};
  36. int cost[3] = { 180, 140, 100};
  37.  
  38. cout<<"Please choose the wood you would like for your desk;"<<endl;
  39. cout<<"M for Mahogany, O for Oak, P for Pine."<<endl;
  40. cin >> x;
  41.  
  42. switch (x)
  43. {
  44. case 'M':
  45. *woodFactor = 180;
  46. break;
  47. case 'm':
  48. *woodFactor = 180;
  49. break;
  50. case 'O':
  51. *woodFactor = 140;
  52. break;
  53. case 'o':
  54. *woodFactor = 140;
  55. break;
  56. case 'P':
  57. *woodFactor = 100;
  58. break;
  59. case 'p':
  60. *woodFactor = 100;
  61. break;
  62. default:
  63. *woodFactor = 1000;
  64. break;
  65. }
  66. }
  67.  
  68.  
  69. double getTotal(int numDrawers, int woodFactor,double *total)
  70. {
  71. *total = numDrawers + woodFactor;
  72. }
  73.  
  74. void PrintPrice(double total)
  75. {
  76. cout << "The price of this desk is $"<<total<<endl;
  77. }

I hope that helps you.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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