Hi people. I have this code and I am stuck on trying to get a 'call by reference' to work properly. I need to convert Celsius to Fahrenheit and Fahrenheit to Celsius. I am not sure what is wrong with my code.
Remember that I have to use a function to call on another function to do the process.

#include <iostream>		//for basic environment
#include <iomanip>		//used in displayTable functions

using namespace std;	//for basic environment

const char D = char(248);

//function prototypes
void displayMenu ();
void getMenuSelection (char& conversionType);
void getStartEndAndIncrement(double& start, double& end, int& increment); 
void displayCtoFTable(double& start, double& end, int& increment);
double CtoF(double curTemp);
void displayFtoCTable(double& start, double& end, int& increment);
double FtoC(double curTemp);


int main()
{
int increment;
double start, end; 
bool notDone = false;
char	conversionType;	  

	cout << fixed << showpoint << setprecision(1);

	// Outer do.. while loop
   do
   {
    //Interact with user
	displayMenu ();
	getMenuSelection (conversionType);
	
	switch (conversionType)
	{
		case 'c':
		case 'C':	// convert C to F
			cout << "convert C to F \n" << endl;
			break;
		case 'f':
		case 'F':  // convert F to C
			cout << "convert F to C \n" << endl;
			break;
		case 'q':
		case 'Q':  // quit the program
			notDone = false;
			return 0;
			break;
		default:
			cout << conversionType <<"  Type of conversion is not recognized " << endl;
			
	} //end switch body

  }//end body of do
    while(notDone);
	 {		
	  if (conversionType == 'f' || conversionType == 'F')
		{
		  getStartEndAndIncrement(start, end, increment);
			displayFtoCTable(start, end, increment);
		}
		 if (conversionType == 'c' || conversionType == 'C')
		{
		  getStartEndAndIncrement(start, end, increment);
		 	displayCtoFTable(start, end, increment);
		}	
	}

cout << "leaving temperature conversion program. Press Enter";
cin.ignore (2);
return 0;
 
}//end main()



void displayMenu(void)
{
	cout << "Enter in the Temperature Converter you wish to use" << endl;
}

void getMenuSelection(char& selection)
{
	cin >> selection;
}

void getStartEndAndIncrement(double& start, double& end, int& increment)
{
	cout << "Enter your Starting temperature" << endl;
	cin >> start;
	cout << "Enter your Ending temperature" << endl;
	cin >> end;
	cout << "Enter your desired increment" << endl;
	cin >> increment;
}
double CtoF(double curTemp)
{	
	double FTemp;  // I need to use this in the function below

	FTemp = (1.8 * curTemp + 32);
	return (FTemp);
}
void displayCtoFTable(double& start, double& end, int& increment)
{
	double CtoF(double FTemp);	
	double FTemp, curTemp;

    start;

    do
	{
    end;
	}	
	while(end <= start);

	do
	{
    increment;
    cout << "\n";
	}	
	while(increment <= 0);
		curTemp = start;
		cout << "Celsius\t\tFahrenheit" << endl;
		cout << "\n";
    while(curTemp <= end)
	{
		cout << curTemp << D << "\t\t"
			 << CtoF(FTemp) << D << endl;
		curTemp += increment;   //could also use 'curCelsius = increment++'
	}
}
double FtoC(double curTemp)
{	double CTemp;
	
	CTemp = (curTemp-32)* 1.8;
	return(CTemp);
}
void displayFtoCTable(double& start, double& end, int& increment)
{
	double FtoC(double CTemp);	
	double CTemp, curTemp;

    start; //'Start' variable will be rcognized here

    do
	{
    end; 
	}	
	while(end <= start);

	do
	{
    increment;
    cout << "\n";
	}	
	while(increment <= 0);
		curTemp = start;
		cout << "Fahrenheit\t\tCelsius" << endl;
		cout << "\n";
    while(curTemp <= end)
	{	
		cout << curTemp << D << "\t\t"
			 << FtoC(CTemp) << D << endl;
		curTemp += increment;   //could also use 'curCelsius = increment++'
	}
}

Any suggestions would be greatly appreciated.

Member Avatar for jencas

Sorry, but read a good C++ book before starting C++ programming. You don't even cope with the basics of a do loop. Just a little hint: performing a loop with start, end and increment is a good job for a for loop!!!

Alright, after some indenting of your code, I finally got to look it over. My guess would be that you have an infinite loop infinite loop in displayCtoFTable and displayFtoCTable . Of course, I'm not entirely sure what's happening in those two functions cause the two do loops don't seem to be doing anything. I might just be missing the purpose of the do's at the moment, but I think that's where your issue is.

void displayCtoFTable(double& start, double& end, int& increment)
{ double CtoF(double FTemp);
  double FTemp, curTemp;
  
  [B]start;
  do
    { end; } while(end <= start);

  do
    { increment;
      cout << "\n"; } while(increment <= 0);[/B]
      
  curTemp = start;
  
  cout << "Celsius\t\tFahrenheit\n\n";
  while(curTemp <= end)
       { cout << curTemp << D << "\t\t"
              << CtoF(FTemp) << D << endl;
         curTemp += increment; //could also use 'curCelsius = increment++'
       }
}

I understand you are trying to write a program to convert Celsius to Fahrenheit and Fahrenheit to Celsius, but it seems to me like you are making this much harder on yourself then it should be. I made a code just like that and mine was only 50 lines. Your code is almost three times that. Plus, you have functions that just say something and drop out which seems like a waste if you ask me. Now, if you have to do it that way cause of a boss or teacher or whatever, then fine, but it just seems like you're creating extra work for yourself.

Yes I agree with you on the whole length of this thing, but thats what the instructor wants. I am forced to make it this way, otherwise I would do everything a 'call by value.' In this example, I am supposed to have 'displayCtoFable' call on 'CtoF' to convert celsius to fahrenheit. I don't like it but those are the rules. And about the syntax, I just was in a hurry and stuck that stuff there.

I ended up getting it people! It should be like this....

#include <iostream>		//for basic environment
#include <iomanip>		//used in displayTable functions

using namespace std;	//for basic environment

const char D = char(248);

//function prototypes
void displayMenu ();
void getMenuSelection (char& conversionType);
void getStartEndAndIncrement(double& start, double& end, int& increment); 
void displayCtoFTable(double& start, double& end, int& increment);
double CtoF(double curTemp);
void displayFtoCTable(double& start, double& end, int& increment);
double FtoC(double curTemp);


int main()
{
int increment;
double start, end; 
bool notDone = false;
char	conversionType;	  

	cout << fixed << showpoint << setprecision(1);

	// Outer do.. while loop
   do
   {
    //Interact with user
	displayMenu ();
	getMenuSelection (conversionType);
	
	switch (conversionType)
	{
		case 'c':
		case 'C':	// convert C to F
			cout << "convert C to F \n" << endl;
			break;
		case 'f':
		case 'F':  // convert F to C
			cout << "convert F to C \n" << endl;
			break;
		case 'q':
		case 'Q':  // quit the program
			notDone = false;
			return 0;
			break;
		default:
			cout << conversionType <<"  Type of conversion is not recognized " << endl;
			
	} //end switch body

  }//end body of do
    while(notDone);
	 {		
	  if (conversionType == 'f' || conversionType == 'F')
		{
		  getStartEndAndIncrement(start, end, increment);
			displayFtoCTable(start, end, increment);
		}
		 if (conversionType == 'c' || conversionType == 'C')
		{
		  getStartEndAndIncrement(start, end, increment);
		 	displayCtoFTable(start, end, increment);
		}	
	}

cout << "leaving temperature conversion program. Press Enter";
cin.ignore (2);
return 0;
 
}//end main()

void displayMenu(void)
{
	cout << "Enter in the Temperature Converter you wish to use:\n";
	cout << "Press 'c' for Celsius, 'f' for Fahrenheit, or 'q' to Quit:" << endl;
}
void getMenuSelection(char& selection)
{
	cin >> selection;
}
void getStartEndAndIncrement(double& start, double& end, int& increment)
{
	cout << "Enter your Starting temperature" << endl;
	cin >> start;
	cout << "Enter your Ending temperature" << endl;
	cin >> end;
	cout << "Enter your desired increment" << endl;
	cin >> increment;
}
double CtoF(double curTemp)
{	double FTemp;
	FTemp = (1.8 * curTemp + 32);
	return FTemp;
}
void displayCtoFTable(double& start, double& end, int& increment)
{
	double CtoF(double FTemp);	
	double FTemp, curTemp;

    do
	{	start;
		end;
	}	
	while(end <= start);
	{	increment;
		cout << "\n";
	}	
	while(increment <= 0);
	{	curTemp = start;
		cout << "Celsius\t\tFahrenheit" << endl;
		cout << "\n";
	}
    while(curTemp <= end)
	{	FTemp = CtoF(curTemp);
		cout << curTemp << D << "\t\t"
			 << FTemp << D << endl;
		curTemp += increment;   //could also use 'curTemp = increment++'
	}
}
double FtoC(double curTemp)
{	double CTemp;
	CTemp = 5*(curTemp - 32) / 9;
	return CTemp;
}
void displayFtoCTable(double& start, double& end, int& increment)
{
	double FtoC(double CTemp);	
	double CTemp, curTemp;
   
    do
	{	start;
		end; 
	}	
	while(end <= start);
	{	increment;
		cout << "\n";
	}
	while(increment <= 0);
	{	curTemp = start;
		cout << "Fahrenheit\t\tCelsius" << endl;
		cout << "\n";
	}
    while(curTemp <= end)
	{	CTemp = FtoC(curTemp);
		cout << curTemp << D << "\t\t"
			 << CTemp << D << endl;
		curTemp += increment;   //could also use 'curTemp = increment++'
	}
}

Now can someone show me how to loop 'main'....I'm out of Ideas and I feel burtn!

Your do while loops are still wrong. Remember, a do while look is just like a while loop, but the condition is checked at the end. It should look like this.

do
{ 
//does something here
} while (!done);

That's it. There is no extra {} after for the while part.

The reason your loop isn't work is cause you're saying "do the stuff if brackets while." There's no condition to compare it to, so it just falls out. Now, if it said while (notDone != true); or while (notDone == false); .

Now, once you change that, you'll need to move where you put the calls for getStartEndAndIncrement() and displayFtoCTable() and displayCtoFTable().

The same thing is affecting your displayFtoCTable() and displayCtoFTable(). Just to give you an example, this is what mine looks like.

void displayFtoCTable(double start, double end, int increment)
{	
   double CTemp, curTemp;
   
   curTemp = start;
   cout << "\n\n\t Fahrenheit\tCelsius\n"
        << "\t-----------------------\n";
   do
	{ CTemp = FtoC(curTemp);
	  cout << "\t " << curTemp << D << "\t\t"
	       << CTemp << D << endl;
	  curTemp += increment;
    } while(curTemp <= end);
    cout << "\n\n";
}

I was able to compact your code down to 116 lines, just so you can see how much extra coding you have. If you can't figure out how to fix your main do while loop, just post again and I can help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.