user2340686
user2340686

Reputation: 97

C++ Input validation for strings with loop

I'm trying to make a program in which the user enters in a number and a letter representing what unit of measurement their using (ex inches=i, feet=f, etc) then the letter inputted is used on a series of if statements to see which function to go to convert the number to how many meters it would be. I added an input validation for the units of measure(which are being used as a string variable).My problem is when I input the letter I want to use the program thinks what I entered is invalid even when the input is correct. I removed the input validation and also noticed that the string doesn't even go through any of the if statements. The code is something like this the #include included:

#include <iostream>
#include <string>
using namespace std;
float inTOmeters(float);
float ftTOmeters(float);
float cmTOmeters(float);
float yTOmeters(float);
int main{
    float measurement, measurement;
    string unit;
    cout<<"Enter the number you want to be measured"<<endl;
    cin>>measure;
    cout<<"Now enter the unit of measurement you want to use"<<endl;
    cout<<"i=inches, f=feet, c=centimeters, y=yards, m=meters"<<endl;
    cin<<unit;
    while(unit !="i"||unit !="m"||unit !="c"||unit !="y"||unit !="f"){
        cout<<"Invalid input pick from I, m, c, y, or f"<<endl;
        cin>>unit;
    }
    if(unit=="i"){
        measurementm=inTOmeters(measurement);
    }
    if(unit=="c"){
        measurementm=cmTOmeters(measurement);
    }
    if(unit=="f"){
        measurementm=ftTOmeters(measurement);
    }
    if(unit=="y"){
        measurementm=yTOmeters(measurement);
    }
    else{
        measurementm=measurement;
    }

    cout<<"your measurement will be"<<measurementm<<"in meters."<<endl;
}

I didn't include the functions because I know they work. My question is how do I make it so my loop and if statements function when given the correct input? Also how do I make it so the code accepts capital letters of the correct input?

Upvotes: 0

Views: 2555

Answers (3)

Chris Olsen
Chris Olsen

Reputation: 3471

As mentioned, this code has numerous problems and could not possibly compile. Try this:

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

float inTOmeters(float);
float ftTOmeters(float);
float cmTOmeters(float);
float yTOmeters(float);

int main()
{
    float input, measurement=0.0;
    char unit;

    cout<<"Enter the number you want to convert:"<<endl;
    cin>>input;

    cout<<"Now enter the unit of measurement you want to use:"<<endl;
    cout<<"i=inches, f=feet, c=centimeters, y=yards, m=meters"<<endl;

    while(true){

        cin >> unit;
        unit = std::toupper(unit);

        if(unit=='I'){
            measurement=inTOmeters(measurement);
        }
        else if(unit=='C'){
            measurement=cmTOmeters(measurement);
        }
        else if(unit=='F'){
            measurement=ftTOmeters(measurement);
        }
        else if(unit=='Y'){
            measurement=yTOmeters(measurement);
        }
        else if(unit=='M'){
            measurement = input;
        }
        else if(unit=='X'){
            break;
        }
        else{
            cout << "'" << unit << "'" << "is invalid. Choose i, m, c, y, or f." << endl;
            continue;
        }

        cout << input << " is " << measurement << " in meters." << endl;
        break;
    }
}

Upvotes: 0

bboll
bboll

Reputation: 82

There are a few problems I see with your program right off the bat. From the code you've provided the variable measure has not been declared and you've declared two variables named measurement.

As for the input, have you thought about handling it with a switch statement? You can use characters for the different cases and write the default case to handle any invalid input.

Lastly, instead of a while loop you could just have your function int main with a return of return main(); which would be a fine way to loop this simple program.

Upvotes: 1

john
john

Reputation: 87944

A few errors

You never input unit, you need cin >> unit; somewhere.

Logic error, you used 'or' when you should of used 'and'.

 while(unit !="i" && unit !="m" && unit !="c" && unit !="y" && unit !="f"){
        cout<<"Invalid input pick from I, m, c, y, or f"<<endl;
        cin>>unit;
    }

I you think about it a minute you'll realise that unit does not equal "i" OR unit does not equal "m" is always true.

Upvotes: 0

Related Questions