-2

While searching for potential errors on a program that I am creating, I got "[Error] ISO C++ forbids comparison between pointer and integer [-fpermissive]."

The error is coming from my while loop, wherein I intended that the program accepts only the following inputs from the user. Right now, I am considering to use an array for this one.

How should I fix this? If anybody any more details, I am more than happy to provide them by showing what I am (as of late).

int main () // While using one of the three functions to make conversions, 
// the program will create the table based on following information. 
{
    char dimension;
    double u1, u2;  
    int starting_value, ending_value, increment, startingUnitDigits, 
    endingUnitDigits;
    cout << "-------------------------------------------------------------------------------------------------";
    cout << "Please answer the following questions as they appear.";
    cout << "Check spelling, use correct abbreviation for units of measurement, and avoid negative values.";
    cout << "-------------------------------------------------------------------------------------------------";
    cout << "Enter the dimensions from the following choices: length, mass and time. (i.e.: 'length,' 'Length').";
    while (!(dimension == "length" || dimension == "Length" || dimension == "mass" || dimension == "Mass" || dimension == "time" || dimension == "Time"))
    {
        cout << "----------------------------------------------------------------------------------------------------";
        cout << "Error! Input for Dimension, " << dimension << ", was either an invalid choice or typed incorrectly.";
        cout << "Please know that this programs accepts length, mass, and time only!";  
        cout << "----------------------------------------------------------------------------------------------------";
        cout << "Enter the dimensions from the following choices: length, mass and time. (i.e.: 'length,' 'Length').";
        cin >> dimension;   
rainer
  • 6,769
  • 3
  • 23
  • 37

1 Answers1

1

The comments are already quite exhaustive, so I just sum them up:

dimension is a single char that you cannot compare to string literals, hence the error. You should use a std::string instead.

Once you changed that the problem remains that you check user input before anything was read from cin. You should do it the other way around.

Your condition will be more readable if you use a container and try to find the user input in that container.

So it would be something like this:

std::vector<std::string> correct_input{"Foo","foo","f00"};
while(true) {
    std::string dimension;
    std::cin >> dimension;
    auto it = std::find(correct_input.begin(),correct_input.end(),dimension);
    if (std::cin.fail() || it==correct_input.end()) {
        // print error and continue to ask for input
        std::cin.clear();  // clear possible error flags
    } else {
       // do something with input
       break;
    }
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185