0

Hi, I've been trying to write a converter that converts several units. I want it to be able to prompt the user to retype an input if they type an invalid input... I'm struggling with this part. This is what I have so far.

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main ()
{
string response ="n";
float conversion;
float x;
cin.precision(4);
cout.precision(4);
cout.setf(ios::fixed,ios::floatfield);
 do
{
  cout << "Hello! You are using Michael's converter." << endl;
  cout << "Choose one of the following conversions:" << endl;
  cout << "1. Fahrenheit to Celsius" << endl;
  cout << "2. Celsius to Fahrenheit" << endl;
  cout << "3. Meters to Feet" << endl;
  cout << "4. Feet to Meters" << endl;
  cout << "5. Pounds to Kilograms" << endl;
  cout << "6. Kilograms to Pounds" << endl;
  cout << "Enter a number from 1-6:" << endl;
  cin >> conversion;
      if (conversion == 1)
        {
         cout << "Fahrenheit to Celsius Converter" << endl;
         cout << "Enter a temperature, in degrees Fahrenheit (e.g. 22)."<< endl;
         cin >> x;
         cout <<x<< " degrees F" <<  " = " << (x-32)*(5/9)<<" degrees C"<< endl;
        }
      if (conversion == 2)
        {
         cout << "Celsius to Fahrenheit Converter."<< endl;
         cout << "Enter a temperature, in degrees Celsius (e.g. 0)." << endl;
         cin >> x;
         cout << x << " degrees C " <<  " = " << x*((9/5))+32 << " degrees F "<< endl;
        }
      if (conversion == 3)
        {
         cout << "Meters to feet conversion." << endl;
         cout <<"Enter a number in meters (e.g. 7)."<< endl;
         cin >> x;
         cout << x << "meters = "<< x*3.2808 <<" feet."<< endl;
        }
      if (conversion == 4)
        {
         cout << "Feet to Meters Conversion." << endl;
         cout << "Enter a number in feet (e.g. 3)" << endl;
         cin >> x;
         cout << x << "feet " <<" = "  << x*0.3048 <<" meters."<< endl;
        }
      if (conversion == 5)
        {
         cout << "Pounds to Kilograms Converter." << endl;
         cout << "Enter a number in pounds (e.g. 23)." << endl;
         cin >> x;
         cout << x << " pounds" <<  " = " << x*.3048<<" meters."<< endl;
        }
      if (conversion == 6)
        {
         cout << "Kilograms to Pounds Converter." << endl;
         cout << "Enter a number in kilograms (e.g. 4)." << endl;
         cin >> x;
         cout << x<<" kilograms " << " = " << x*2.205<< " pounds." << endl;
        }
      if (conversion !=1|| conversion !=2|| conversion !=3|| conversion !=4|| conversion !=5|| conversion !=6)
        {
         cout <<"Please enter an integer from 1 to 6."<<endl;
	 cin  >> conversion;
        }
}
         cout << "Do you want to do this again? (Y/N)" << endl;
         cin >> response;
        }
 while (response == "Y" || response == "y");
}

Thanks in advance for the help!

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Red Goose
0
if (conversion !=1|| conversion !=2|| conversion !=3|| conversion !=4|| conversion !=5|| conversion !=6)

Can be changed to

else

Since the above is implied through the preceding code.

Also be careful with your brackets. Line 73 is the end of your do loop and line 76 is the end of int Main(). You also have one surplus bracket. Don't forget your return statement for int Main() either.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.