-3

I want to write a code that finds the derivative to any function and want to take a user input on what type of function they're inputting is. Essentially I tried to write a while loop that only accept valid answers but the while loop isn't behaving as intended. Here is my code:

int main()
{
    std::cout << "Welcome to the differentiation calculator: "; 
    std::cout << std::endl; 
    std::cout << "Please input what type of function you have: Algebraic,Trigonometric,Exponential, Hyperbolic : " << std::endl;
    std::string UsersTypeOfFunction; 
    std::cin >> UsersTypeOfFunction; 

    while (UsersTypeOfFunction != "Hyperbolic" || UsersTypeOfFunction != "hyperbolic" || UsersTypeOfFunction != "Exponential" || UsersTypeOfFunction != "exponential" || UsersTypeOfFunction != "Trigonometric" || UsersTypeOfFunction != "trigonometric" || UsersTypeOfFunction != "Algebraic " || UsersTypeOfFunction != "algebraic")
    {
        std::cout << "Please input a Valid function type. "; 
        std::cin >> UsersTypeOfFunction; 
    }

    return 0; 
}

if my code is the issue please let me know

5
  • 9
    You got AND / OR the wrong way around. while (UsersTypeOfFunction != "Hyperbolic" || UsersTypeOfFunction != "hyperbolic" - if the value of the variable is Hyperbolic, then the second part of this condition will still be true, because the value is not hyperbolic; and since you joined all those individual conditions via OR, the whole thing is also still true. You want to continue your loop while the variable does not have the first value, and not the second one, and ... Commented Nov 3 at 14:37
  • Also make functions for complex boolean expressions, it is more readable and allows you to test time with testdata. Commented Nov 3 at 15:30
  • 1
    Beginners confuse || with && a lot. There are probably a dozen duplicates to this question, but I'm not sure how to search for them. Commented Nov 3 at 15:38
  • My recommendation is to convert the input string to all lower or all upper case. This reduces the number of comparisons. See std::transform, std::tolower, and std::toupper. Commented Nov 3 at 17:01
  • Side note: nothing in this code needs the extra stuff that std::endl does. Use '\n' to end a line unless you have a good reason not to. Commented Nov 3 at 17:09

1 Answer 1

0

Although you got the correct explanation in the comment, you might go a step further and try to avoid redundant code.

First, you could define a std::vector holding the allowed values:

std::vector<std::string> functions = {
    "Hyperbolic",    "hyperbolic",
    "Exponential",   "exponential",
    "Trigonometric", "trigonometric"
};

then you can take advantage of the algorithm available in the C++ standard library. In this specific case, you could use std::find that can tell whether a given value can be found for instance in a vector.

Since you have to enter at least once the value, you should use a do/while loop instead of a while loop. Putting all together, you could have e.g.

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>

int main()
{
    std::cout << "Welcome to the differentiation calculator: ";
    std::cout << std::endl;

    std::vector<std::string> functions = {
        "Hyperbolic",    "hyperbolic",
        "Exponential",   "exponential",
        "Trigonometric", "trigonometric"
    };

    std::string UsersTypeOfFunction;

    do {
        std::cout << "Please input a Valid function type: ";
        std::cin >> UsersTypeOfFunction;
    } while (std::find(functions.begin(), functions.end(), UsersTypeOfFunction) == functions.end());
}
Sign up to request clarification or add additional context in comments.

3 Comments

You could do less compares if you converted the input string to all lower case or all upper case. What happens if the User types in "hYperBolic"?
@ThomasMatthews, you're right. My intent was mainly to pinpoint for the OP the fact that the standard library provides algorithms such as std::find, so I made no great effort to improve the whole snippet.
Thanks so much for help, this really saved me a headache.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.