Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Can someone help me with a C++ program?
Here's the thing, I have written 2 programs. One that converts inches to centimeters and one that converts cm to inches. Now I am writing a program that will provide you with a menu to choose which one you want to convert. The programs runs but only gives me a blank cmd screen. I am not sure what else I can do. I have tried rewriting it a different way but I get a bunch of errors. A little help would be great.
#include <iostream>
#include <iomanip>
using namespace std;
float inchestocentimeters(const float cmPerInch, float inch);
float centimeterstoinches(const float inchPerCm, float cm);
int main()
{
const float cmPerInch = 2.54, inchPerCm = 1.0;
float inch, cm;
char type;
cin >> type;
// Choose the type of conversion.
cout << "Select the measurement you want to convert from the list." << endl;
cout << "I)nches to centimeters." << endl;
cout << " OR " << endl;
cout << "C)entimeters to inches." << endl;
// Sets the decimal place.
cout << fixed << showpoint << setprecision(2);
// Uses if else statements to call the correct function.
if ((type == 'I')||(type == 'i'))
{
cout << "Enter the number of inches you would like to convert to centimeters.\n";
cin >> inch;
cout << inchestocentimeters(cmPerInch, inch);
cout << "The conversion in centimeters is " << cm << " cm" << endl;
}else if ((type == 'C')||(type == 'c'))
{
cout << " Enter the number of centimeters that you would like to convert to inches.\n";
cin >> cm;
cout << centimeterstoinches(inchPerCm, cm);
cout << "The conversion from centimeters to inches is " << inch << " inches" << endl;
}else
{
cout << "You have entered an invalid entry.\n";
cout << "You must choose 'I' to convert inches to centimeters.\n";
cout << "OR\n";
cout << "You must choose 'C' to convert centimeters to inches." << endl;
}
cout << "Press ENTER to end the program." << endl;
cin.ignore();
cin.get();
}
// This function will convert inches to centimeters.
float inchestocentimeters(const float cmPerInch, float inch)
{
return inch = cmPerInch * inch;
}
// This function will convert centimeters to inches.
float centimeterstoinches(const float inchPerCm, float cm)
{
return cm = inchPerCm * (cm * 0.3937008);
}
Thanks man that worked!!
However I still have a problem. Now when I compile the code it shows me the menu and then I will choose 'i' or 'c'. The code will compile and convert the number but it gives me a break point at "cout
It gives me a break point at "cout
1 Answer
- 9 years agoFavorite Answer
Dont you think you have a cin>>type at the wrong spot?
Look at the starting of your code int main() thing cin>>type supposed to be after
// Choose the type of conversion.
cout << "Select the measurement you want to convert from the list." << endl;
cout << "I)nches to centimeters." << endl;
cout << " OR " << endl;
cout << "C)entimeters to inches." << endl;