0

When the user enters "cot" and then enters 53.8 as the temperature, the code returns the 'Temp is too low' even though it's a valid temperature. I've played with changing the number in the determining if-statement up and down, but 53.8 is the only number that gives me this error.

Issue is in the second while loop in the main function where it sends the objTemp to the inValid Temp function (second to last function in code).

A user would first be prompted for a strange object and enter "cot", then enter "53.8" when prompted for the object's temperature afterwards.

I know I could change this around to get it to work, but I'm learning and really want to understand what's going on.

Any ideas?

    //Ask for temperature of obj. in Fahrenheit
    cout << "Enter the " << inString << "'s temperature in Fahrenheit." << endl;
    cin >> objTemp; 
    
    //Validate range| loop again until valid entry is made.
    while ((validRange = invalidTemp(objIs, objTemp = 53.8)))
    {
        if (validRange == 1)
             cout << "Entered temperature is too low.\n";

        else
            cout << "Entered temperature is too high.\n";
            
            cout << "Enter the " << inString << "'s temperature in Fahrenheit." << endl;
        cin >> objTemp;
    }



    //Choose from conversion menu|| User enters selection, and menu returns appropriate conversion factor
     convMenu(objTemp);

     //insert program pause


    return 0;
}



int invalidTemp(int object, float inTemp)
{
    switch(object)
    {
        case 1: if (inTemp < 85.8)  //temp for cat
                    return 1;
            else if (inTemp > 102.2)
                return 2;
            break;  

        case 2: if (inTemp < 53.8)  //temp for cot
                   return 1;
            else if (inTemp > 80.2)
                return 2;
            break;

        case 3: if (inTemp < 71.8)  //temp for cap
                   return 1;
            else if (inTemp > 88.2)
                return 2;
            break;
                
        case 4: if (inTemp < 90.8)  // temp for snake
                   return 1;
            else if (inTemp > 100.2)
                return 2;
            break;

        //default: cout << "Valid" << endl;
    }
    return 0;
}
irlupe
  • 11
  • 3
  • I cannot find where is the string "Temp is too low". Please provide a [mre]. So others won't take time to check all of your logic for input. It makes your question answered faster. – Louis Go Mar 25 '22 at 05:52
  • Also it might be the issue of rounding error. When input "53.8" it might be 53.777777.. in code..etc. When it comes to comparing floating number, always takes the delta you accept into consideration. Eg: +/- 0.01 deviation is allowed. – Louis Go Mar 25 '22 at 05:55
  • @LouisGo In my last question I was told I included too little, so I guess I'm still finding the sweet spot. Issue is in the second while loop in the main function where it sends the objTemp to the inValid Temp function (second to last function in code). A user would first be prompted for a strange object and enter "cot", then enter "53.8" when prompted for the object's temperature afterwards. – irlupe Mar 25 '22 at 06:01
  • 1
    53.8 float is smth 58.7998046875 that is less than 53.8 double that is smth 58.80000019073486328125. – 273K Mar 25 '22 at 06:02
  • Put minimum logic related to your question in the code snippet (if it's compiled on godbolt.org then it's better.). Do not include unrelated logics like getting input and validation...etc . Eg: the if statement you have the issue and the number hardcoded into your code to demonstrate the question. – Louis Go Mar 25 '22 at 06:03
  • @273K Interesting. Well, I changed the variable type to float for convTemp, but that didn't seem to solve my issue. Is there something else I should try? Thanks for explaining that. I found a related article and was able to understand it a little more. – irlupe Mar 25 '22 at 06:19
  • It's difficult to guess without seeing the code. In any case you continue use mixed float vars and double values. `inTemp` is float 58.7998046875, `53.8` is double 53.80000019073486328125. – 273K Mar 25 '22 at 06:27

0 Answers0