If statments and strings?

This is a discussion on If statments and strings? within the C++ Programming forums, part of the General Programming Boards category; how do i go about getting a if statment with a string like the following code PHP Code:      if( ...

  1. #1
    Registered User PanzTec's Avatar
    Join Date
    Sep 2004
    Posts
    24

    If statments and strings?

    how do i go about getting a if statment with a string like the following code

    PHP Code:
        if(cmd == "help")
        {
            
    fn 1;
        } 
    The Matrix Will Live Again!

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    When comparing two strings, you could use strcmp(). strcmp() is a function included in the C library that Compares two strings.

    It works like the following:
    > Compares string1 to string2 character by character.

    Code example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	char str[] = "Hello";
    
    	if (!strcmp(str, "Hello"))
    		printf("It matched!\n");
    
    	return 0;
    }
    You may be asking why I asked if strcmp() failed, or equalled zero. Though maybe this will help:

    • <0 string1 is less than string2
    • 0 string1 is the same as string2
    • >0 string1 is greater than string2


    Hope this helps,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User PanzTec's Avatar
    Join Date
    Sep 2004
    Posts
    24
    Thank you! you are the man!
    The Matrix Will Live Again!

  4. #4
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Panz, if you want to use comparision disregarding case, use stricmp instead (note the 'i'). It's used the same way.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Or... Use the std::string class found in <string> and use the == operator. That may be an easier solution.

    cppreference.com has a reference on them
    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.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    52
    Maybe I dont quite get the question....however If I interperet it correctly you could use apstring.

    I know this is really noob to say but just incase you dont know what apstring does, it's an ap class that you can download off the net and you can incude the cpp into ur program.

    Once you have it you can use it as a variable type such as int or double.

    Ex.

    Code:
    #include <iostream.h>
    #include "apstring.h"
    
    int main()
    {
              apstring name;
    
              cout << "Enter your name..." << endl;
              cin >> name;
    
              if (name == "John")
              {
                     cout << "You have input the correct name..." << endl;
              }
    
              else
              cout << "You have input the incorrect name..." << endl;
    
              return 0;
    }

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A word of caution: Avoid apstring/apanything if possible (i.e. if no one is making you use them). They are really rather poorly written classes, and there is no reason not to use the standard class (std::string).

    Cheers
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If statments for a struct w/ strings
    By pandroff in forum C Programming
    Replies: 6
    Last Post: 12-01-2005, 10:37 AM
  2. Case statments with long strings... is it possible?
    By Unregistered in forum Game Programming
    Replies: 4
    Last Post: 06-21-2002, 11:22 PM
  3. if statments
    By JPed2003 in forum C++ Programming
    Replies: 8
    Last Post: 05-10-2002, 07:58 PM

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