i tried to do something like this in one of the program i made....
if the user entered something that is not the same with the number and the word that are available in the program,it will ask the user to enter the input again..
But the problem is that the loop is continuing in my program even if i enter the correct word or number..
how to fix this simple problem?

#include <iostream>
using namespace std;

int main ()
{
    string test;

    do {

        cout<<"enter the input :";
        cin>>test;

    }while (test<"1" || test>"5" || test !="a" || test!="b");
}

Dani AI

Generated

The loop kept running because the condition logic is wrong: you want to repeat while the input is NOT any of the allowed values, but the original expression used operators that make that test always true for every single input. and were on the right track — the fix is to treat the check as a membership test (is the input in the allowed set?) and keep everything as strings (or convert to numbers explicitly) so comparisons behave predictably.

A simple, robust pattern is to keep a whitelist of allowed string tokens and loop until the user enters one of them. This also accepts numeric text (like "42") and words without mixing types:

#include <iostream>
#include <string>
#include <unordered_set>

int main() {
    std::unordered_set<std::string> allowed{"yes", "no", "10", "20"};
    std::string input;
    do {
        std::cout << "Enter input: ";
        std::getline(std::cin, input);
    } while (allowed.find(input) == allowed.end());
    // valid input here
}

Notes and troubleshooting

  • Use std::getline to avoid leftover newline problems when mixing input methods.
  • If you need numeric ranges, convert with std::stoi (or strtol) and handle exceptions/errors; do not compare numeric ranges using string ordering.
  • Trim whitespace and normalize case (convert to lowercase) before comparing for more forgiving input.
  • If the loop still behaves oddly, print the raw input (show hidden spaces/newlines) to debug what the program actually read.

This approach addresses ’s original do/while loop problem and avoids brittle boolean expressions by centralizing valid inputs into one clear check.

Recommended Answers

All 7 Replies

The while statement is wrong. If I enter a '1' the loop will continuje because it is not an 'a', which is one of the tests. Try this while (test<"1" || test>"5" && test !="a" && test!="b");

i tried to do something like this in one of the program i made....
if the user entered something that is not the same with the number and the word that are available in the program,it will ask the user to enter the input again..
But the problem is that the loop is continuing in my program even if i enter the correct word or number..
how to fix this simple problem?

#include <iostream>
using namespace std;

int main ()
{
    string test;

    do {

        cout<<"enter the input :";
        cin>>test;

    }while (test<"1" || test>"5" || test !="a" || test!="b");
}

The problem is you cant compare a string to an intenger so you could only compare to the "a" and "b" letter. And you certainly want to do test != "a" && test != "b", else it will continue looping forever.

That should answer and fix your problem.

EDIT: If you really want to compare to a number you could compare like this:
test != "1" && test != "2", else you cannot do unless you declare test variable as an intenger but then you can only compare to numbers and not letters.

owh.....thank you....
:)

The problem is you cant compare a string to an intenge.

The program is not comparing a string to an integer, "1" is a string, not an integer.

>>EDIT: If you really want to compare to a number you could compare like this:
test != "1" && test != "2", else you cannot do unless you declare test variable as an intenger

That's not correct either. test < '1' works just great. Compile and run the program and you will find out.

yeah you are right..
your suggestion before this is not working...
and your last suggestion (test<'1') only could be used if we declared the variable as char....
all of this become problem when using string as it invloves both words and numbers to be compared....

is there any other ways could be used??

yeah you are right..
your suggestion before this is not working...
and your last suggestion (test<'1') only could be used if we declared the variable as char....
all of this become problem when using string as it invloves both words and numbers to be compared....

is there any other ways could be used??

>>and your last suggestion (test<'1') only could be used if we declared the variable as char

Oops -- it should have been text < "1"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.