how do i go about getting a if statment with a string like the following code
PHP Code:
if(cmd == "help")
{
fn = 1;
}
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!
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:You may be asking why I asked if strcmp() failed, or equalled zero. Though maybe this will help:Code:#include <stdio.h> #include <string.h> int main() { char str[] = "Hello"; if (!strcmp(str, "Hello")) printf("It matched!\n"); return 0; }
- <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.
Panz, if you want to use comparision disregarding case, use stricmp instead (note the 'i'). It's used the same way.
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.
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; }
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.