简体   繁体   中英

String to float using data type conversions

I'm having a headache trying to run this piece of code that I have. I'm trying to identify whether the value of float that i have entered will be the same data type as float. This is my coding.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    float x1, y1, x2, y2, x3, y3, percentage;
    string sym1, sym2;
    int count;

    cout<<"Enter the first fraction : ";

    do{
        cin>>x1>>sym1>>y1;

        if(x1 != float(x1) || sym1 != string(sym1) || y1 != float(y1))
        {
            cout<<"Please enter the correct fraction : ";
        }
        else if(sym1 != "/")
        {
        cout<<"Sorry. This is not a fraction. Please enter again : ";
        }
        else
        {
            break;
        }
    }while(x1 == float(x1) || sym1 == string(sym1) || y1 == float(y1));

    cout<<"Enter the second fraction : ";

    do{
        cin>>x2>>sym2>>y2;

        if(x2 != float(x2) || sym2 != string(sym2) || y2 != float(y2))
        {
            cout<<"Please enter the correct fraction : ";
        }
        else if(sym2 != "/")
        {
            cout<<"Sorry. This is not a fraction. Please enter again : ";
        }
        else
        {
            break;
        }
    }while(x2 == float(x2) || sym2 == string(sym2) || y2 == float(y2));

    x3 = x1 * x2;
    y3 = y1 * y2;

    percentage = (x3*100)/y3;

    cout<<x1<<"/"<<y1<<" and "<<x2<<"/"<<y2<<" is "<<x3<<"/"<<y3<<"\n";
    cout<<x3<<"/"<<y3<<" is "<<percentage<<"%";

    return 0;
}

The piece of code that i'm trying to change is this

    do{
        cin>>x1>>sym1>>y1;

        if(x1 != float(x1) || sym1 != string(sym1) || y1 != float(y1))
        {
            cout<<"Please enter the correct fraction : ";
        }
        else if(sym1 != "/")
        {
        cout<<"Sorry. This is not a fraction. Please enter again : ";
        }
        else
        {
            break;
        }
    }while(x1 == float(x1) || sym1 == string(sym1) || y1 == float(y1));

it seems that when i entered 4 / 6 or any other related fraction formats, it reads properly. same goes to 4 * 6, it prints out the expected output. but when i entered a / 6 or 6 / a, it goes into logical error, an infinite loop. its like it is wrong somewhere in data conversions in if statements and while statements. or is it because the data type used is wrong? i couldnt trace what the problem might be. is there any solution on how to do this? please do help. Thank you in advance brothers and sisters.

There is no way for any of these comparisons to return false.

if(x1 != float(x1) || sym1 != string(sym1) || y1 != float(y1))
{
    cout<<"Please enter the correct fraction : ";
}

x1 and y1 are floats, and casting them to float does not change their value in any way. The std::string comparison operators also compare the contents of the string, so this comparison will also always return true.

You are using the same statement as your loop condition, which leads to your infinite loop. Try using just if(sym1 != "/") for both conditions (better still: evaluate the comparison only once, and store the result in a boolean. doing things twice leads to bugs when you change something later and forget to change it everywhere).

For more details on how the operator>> is working, see eg cppreference

Quote:

Until C++11:

If extraction fails (eg if a letter was entered where a digit is expected), value is left unmodified and failbit is set.

Since C++11:

If extraction fails, zero is written to value and failbit is set. If extraction results in the value too large or too small to fit in value, std::numeric_limits::max() or std::numeric_limits::min() is written and failbit flag is set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM