Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

comparing

mouse 163mouse 163 Member Posts: 14
hi everybody...
i want to test to see if an input string
is part of an array of srtings
for example
if I declare a string of 8 months:
[code]const char *months[8] = {"January", "February", "March", "April", "May", "June", "July", "August"};[/code]
how do i test if a cin'd month name is part of that set
(so i can have a trap if invalid data is entered)
I have tried many combination of string compare and am not having any luck
thanks

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    You have to test them one at a time. There are other ways, but they use STL collection classes.


    [code]
    const char *months[8] = {"January", "February", "March", "April", "May", "June", "July", "August"};

    int main()
    {
    for(int i = 0; i < 8; i++)
    {
    if( strcmp(months[i],"March") == 0)
    {
    // found the month, its in the ith array elemenmt
    }
    }
    }
    [/code]




    : hi everybody...
    : i want to test to see if an input string
    : is part of an array of srtings
    : for example
    : if I declare a string of 8 months:
    : [code]const char *months[8] = {"January", "February", "March", "April", "May", "June", "July", "August"};[/code]
    : how do i test if a cin'd month name is part of that set
    : (so i can have a trap if invalid data is entered)
    : I have tried many combination of string compare and am not having any luck
    : thanks
    :
    :

  • mouse 163mouse 163 Member Posts: 14
    :thanks for the reply...I was trying to do that using a pointer and it messed me up a bit....I've got a good compile now..:-D



    You have to test them one at a time. There are other ways, but they use STL collection classes.
    :
    :
    : [code]
    : const char *months[8] = {"January", "February", "March", "April", "May", "June", "July", "August"};
    :
    : int main()
    : {
    : for(int i = 0; i < 8; i++)
    : {
    : if( strcmp(months[i],"March") == 0)
    : {
    : // found the month, its in the ith array elemenmt
    : }
    : }
    : }
    : [/code]
    :
    :
    :
    :
    : : hi everybody...
    : : i want to test to see if an input string
    : : is part of an array of srtings
    : : for example
    : : if I declare a string of 8 months:
    : : [code]const char *months[8] = {"January", "February", "March", "April", "May", "June", "July", "August"};[/code]
    : : how do i test if a cin'd month name is part of that set
    : : (so i can have a trap if invalid data is entered)
    : : I have tried many combination of string compare and am not having any luck
    : : thanks
    : :
    : :
    :
    :

Sign In or Register to comment.