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");
}

Edited 7 Years Ago by samsons17: n/a

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.

Edited 7 Years Ago by Jiwe: n/a

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"

Edited 7 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.