Question

I have a problem i cannot figure out at all!

in my program the user enters numbers to be sorted. i had to be able to sort infinity, negative infinity and the so called "Nullity" (these i defined early in the program)

if the user wants to enter infinity for example they have to enter "Pinf" into the string.

my issue is i store the users input in a std::string and then check if the string is "pinf" or "Pinf" even tho i have entered the number 3 so the string is "3", it still goes into the if statement, what have i done wrong?!

My code is below;

    string Temp;
    cin>> Temp;
    if (Temp.find("Pinf")||Temp.find("pinf")) {
        Num = Pinfinity;
    }

It thinks the if statement is true everytime.

Was it helpful?

Solution 2

If you are just searching for Pinf or pinf then you can use this. Note the logical or operator is ||.

 if (Temp == "Pinf" || Temp == "pinf") {

OTHER TIPS

1.Error - you are using | instead of ||.

2.Error - findreturns

The position of the first character of the first match. If no matches were found, the function returns string::npos. You should change

if (Temp.find("Pinf")|Temp.find("pinf")) {

to

if ((Temp.find("Pinf") != string::npos) || (Temp.find("pinf") != string::npos)) {

| is a bitwise or operator. Use || in place of |

if ( Temp.find("Pinf") != npos || Temp.find("pinf") != npos ) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top