954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to loop if the input is numbers and word??

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");
}
samsons17
Posting Whiz in Training
233 posts since Oct 2009
Reputation Points: 6
Solved Threads: 1
 

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
Team Colleague
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
 

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

samsons17
Posting Whiz in Training
233 posts since Oct 2009
Reputation Points: 6
Solved Threads: 1
 
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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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??

samsons17
Posting Whiz in Training
233 posts since Oct 2009
Reputation Points: 6
Solved Threads: 1
 

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??

samsons17
Posting Whiz in Training
233 posts since Oct 2009
Reputation Points: 6
Solved Threads: 1
 

>>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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You