do while loop with header file help

I need to code a menu options from changing inches to centimeters or centimeters to inches with a header file and I am lost.

#include <iostream>
#include "Header.h"
using namespace std;
int main()
{
int menu_option;
double inches;
double centimeters;


do
{
cout << "Please select an option, if not press q to quit \n ";
cout << "1. Inches to Cetnimeters \n";
cout << "2. Centimeters to Inches \n";
cout << "3. Exit \n";
cin >> menu_option;

switch (menu_option)
{
case 1:
cout << "Please enter the number of inches: ";
cin >> inches;
cout << total_centimeters << endl;
break;
case 2:
cout << "Please enter the number of centimeters: ";
cin >> centimeters;

cout << total_inches;
break;
case 3:
cout << "Exit" << endl;
break;
default:
{
cout << "Invalid input please try again. ";
}

}

} while (menu_option !=3);

system("Pause");
return 0;
}




HEADER FILE CODE


#pragma once
#include <iostream>
using namespace std;

double total_inches(int centimeters)
{
centimeters = centimeters / .3937 ;
return centimeters;
}
double total_centimeters(int inches)
{
inches = inches * 2.54;
return inches;
}
What are you lost with, what isn't working for you?

Also please use code tags (<> symbol) when posting code :)
Hello ernistine2,

As hoogo said:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


There are several problems with your program:

First what you call a header file is not. It should be a ".cpp" file and compiled and linked separately.

Second being a ".cpp" it should not be included in another ".cpp" as you did. This may work, but it is not right.

In the prototypes (that I added to the program) you receive the variable as an "int" this would work better as a "double". And do not forget to change the function definition.

You may like this way of doing the main menu better:
1
2
3
4
5
6
std::cout << "\n Please select an option, if not press q to quit \n";
std::cout << "\n 1. Inches to Centimeters";  // <---Chsnged spelling of "Cetnimeters".
std::cout << "\n 2. Centimeters to Inches";
std::cout << "\n 3. Exit";
std::cout<<"\n Enter choice: ";
std::cin >> menu_option;

I put most of the "\n"s at the beginning rather than at the end.

I added this before the switch:
 
std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Requires header file "iomanip". Only needs done once. 

This will setup the output to print 2 decimal places. "std::showpoint" will print ".00" if it occurs.

The line: cout << total_centimeters << endl; and cout << total_inches;. These two are function calls that require a parameter that is missing. I also think this might work better for you:
std::cout << "\n Total centimeters are " << total_centimeters(inches) << std::endl;

If you want to use "system("pause");" you might want to put "system("cls");" at the beginning of the do/while loop. You will also need to put "system("pause");" before the "break" statement of case 1 and case 2 to pause and see the results before you leave the switch.

I created "Functions.cpp" and put this in it:
1
2
3
4
5
6
7
8
9
10
11
12
13
constexpr double CENTITOINCH{ 0.3937 };
constexpr double INCHTOCENTI{ 2.54 };

double total_inches(double centimeters)
{
	centimeters = centimeters * CENTITOINCH;
	return centimeters;
}
double total_centimeters(double inches)
{
	inches = inches * INCHTOCENTI;
	return inches;
}

Your functions could simply be written as:
1
2
3
4
double total_inches(double centimeters)
{
	return centimeters * CENTITOINCH;
}

Either way works.

Because these functions are simple calculations there is no need for any header files.

Hope that helps,

Andy
Topic archived. No new replies allowed.