| | |
Help referencing variables
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 4
Reputation:
Solved Threads: 0
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;
}
#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;
}
C++ Syntax (Toggle Plain Text)
void getDrawers(int &numDrawers) { cout <<"Enter the number of drawers you would like."<<endl; cin >> numDrawers; } int main() { int numDrawers; getDrawers(numDrawers); // ...
"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
•
•
•
•
Originally Posted by winbatch
(for something this small, you could simply make the numDrawers global...)
"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
•
•
Join Date: Feb 2005
Posts: 4
Reputation:
Solved Threads: 0
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
Here are the latest errors...
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <conio.h> 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); } 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; } }
Here are the latest errors...
C++ Syntax (Toggle Plain Text)
--------------------Configuration: CalcTwo - Win32 Debug-------------------- Compiling... CalcTwo.cpp C:\Programming C++\Chapter04\CalcTwo.cpp(20) : error C2664: 'getTotal' : cannot convert parameter 2 from 'int' to 'char &' A reference that is not to 'const' cannot be bound to a non-lvalue C:\Programming C++\Chapter04\CalcTwo.cpp(22) : error C2664: 'PrintPrice' : cannot convert parameter 1 from 'double' to 'int &' A reference that is not to 'const' cannot be bound to a non-lvalue C:\Programming C++\Chapter04\CalcTwo.cpp(33) : error C2084: function 'void __cdecl main(void)' already has a body C:\Programming C++\Chapter04\CalcTwo.cpp(35) : error C2664: 'getDrawers' : cannot convert parameter 1 from 'int' to 'int &' A reference that is not to 'const' cannot be bound to a non-lvalue 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 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 Error executing cl.exe. CalcTwo.obj - 12 error(s), 0 warning(s)
•
•
•
•
C:\Programming C++\Chapter04\CalcTwo.cpp(20) : error C2664: 'getTotal' : cannot convert parameter 2 from 'int' to 'char &'
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 &'
C++ Syntax (Toggle Plain Text)
int numDrawers; 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
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
•
•
Join Date: Feb 2005
Posts: 4
Reputation:
Solved Threads: 0
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:
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!
I fixed the int, char & double thing I had going on (I think). But from where you had the comment on:
C++ Syntax (Toggle Plain Text)
int numDrawers; getDrawers(numDrawers*30);
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!
•
•
•
•
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:
I still don't know what you mean. Where do I calculate the value if I can't do it there?C++ Syntax (Toggle Plain Text)
int numDrawers; getDrawers(numDrawers*30);
C++ Syntax (Toggle Plain Text)
int numDrawers; getDrawers(numDrawers); 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?
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
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!
I hope that helps you.
BTW, working on your problem helped my understand of reference variables! It was fun!
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void getDrawers(int *numDrawers); void getWood(int *woodFactor); double getTotal(int numDrawers,int woodfactor,double *total); void PrintPrice(double total); int main() { int numDrawers=0, woodFactor=0; double total=0; getDrawers(&numDrawers); getWood(&woodFactor); getTotal(numDrawers,woodFactor,&total); PrintPrice(total); cin.get(); return (0); }; void getDrawers(int *numDrawers) { int num; cout<<"Enter the number of drawers you would like."<<endl; cin>>num; *numDrawers = num; } void getWood(int *woodFactor) { char x; int i=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; switch (x) { case 'M': *woodFactor = 180; break; case 'm': *woodFactor = 180; break; case 'O': *woodFactor = 140; break; case 'o': *woodFactor = 140; break; case 'P': *woodFactor = 100; break; case 'p': *woodFactor = 100; break; default: *woodFactor = 1000; break; } } double getTotal(int numDrawers, int woodFactor,double *total) { *total = numDrawers + woodFactor; } void PrintPrice(double total) { cout << "The price of this desk is $"<<total<<endl; }
I hope that helps you.
![]() |
Similar Threads
- How to delete a data structure in Builder 6.0? (C++)
- what is the best way to track segmentation fault errors (C++)
Other Threads in the C++ Forum
- Previous Thread: Program with many errors
- Next Thread: A question about file streaming
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






