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)
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;
}

Edited 3 Years Ago by happygeek: fixed formatting

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

int main()
{
   int numDrawers;
   getDrawers(numDrawers);
   // ...

(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:

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

#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...

--------------------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 &'

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.

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

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;
   }


}

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:

int numDrawers;
   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!

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:

int numDrawers;
   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?

int numDrawers;
   getDrawers(numDrawers);
   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?

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?

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!

#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.

This article has been dead for over six months. Start a new discussion instead.