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");
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Jiwe
Junior Poster in Training
71 posts since Jun 2009
Reputation Points: 10
Solved Threads: 9
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>>and your last suggestion (test<'1') only could be used if we declared the variable as char
Oops -- it should have been text < "1"
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343