"if" and string arrays

This is a discussion on "if" and string arrays within the C++ Programming forums, part of the General Programming Boards category; Hello. I want the user to enter a string, and then i want to see if what he entered matches ...

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    "if" and string arrays

    Hello. I want the user to enter a string, and then i want to see if what he entered matches my criteria using if statement but i can't seem to get it working. how would you use an if statement on a string array, can you show me a small example? thanks

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    some code?

    all i can think of with what you said is something like this:
    Code:
    ...
    if(!strcmpi(somestring,anotherstring)){...}
    ...
    or:
    Code:
    ...
    if(!strcmpi(somestring,"constant string")){...}
    ...
    //edit: that's in the <cstring> library
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I might be wrong, but I thought that the boolean equals operator (==) was overloaded for the STL string class.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    I think jrahhali (wtf @ name) must be using C-strings.

    If that's the case, then jrahhali, look up the <string> STL
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  5. #5
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    Code:
    if (strcmp(string,"Value")==0) {
      echo "TRUE";
    }

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    echo? Shell script?

    How 'bout: cout << "TRUE";
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    Meh, brain doesn't convert php to c++ very well late a night...
    But you get the picture.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Here are some traditional C string functions:

    Code:
    const char * findfirstof(const char * ctn, const char * cmp)    
    {
      const char * base = cmp;
    
          for(; *ctn != 0; ++ctn)   
        {
             for(cmp=base; *cmp != 0; ++cmp)    
           
                 if(*ctn == *cmp)    return ctn;
         }
      return 0;
    }
    
    
    
    const char * findfirstnotof(const char * ctn, const char * cmp)    
    {  
      const char * base = cmp, vld;
    
          for(; *ctn != 0; ++ctn)    
        {
           vld = ctn;
               
              for(cmp=base; *cmp != 0; ++cmp)
                   
                   if(*ctn == *cmp)    vld = 0;
             
              if(vld) return vld;
         }
      return 0;
    }
    Works great for crude validation/parsing too.
    Last edited by Sebastiani; 10-07-2003 at 11:07 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 08:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21