Thread: Hey, um strings?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    Hey, um strings?

    Yea, hey. Gotsa question. It is as follows, well first off, I made this code just to maybe help you guys understand what i mean, caz i don't know how else to do it.
    Code:
    #include <iostream.h>
    
    main()
    {
    	char value[5];
    	for(int index=0; index<=5; index++)
    	{
    		cout<<"Enter a direction:\n";
    		cin>>value[index];
    	}
    	if(value[0]=='g' && value[1]=='o' && value[2]=='l' && value[3]=='e' && value[4]=='f' && value[5]=='t')
    	{
    		cout<<"Works.";
    	}
    	else
    	{
    		cout<<"Still works...";
    	}
    	return 0;
    }
    That works, but what I want to do is say Enter a direction: and you can type as many things as you want (increase the size of the array, which i can do) and when you're done typing your direction you would simply press q for example, for quit, and it would exit the loop. What i would then like it to do, in place of
    Code:
    	if(value[0]=='g' && value[1]=='o' && value[2]=='l' && value[3]=='e' && value[4]=='f' && value[5]=='t')
    i would like it to be something such as if(the entire array of value, reading from place 0 to the terminator=="goleft") then do blah...how would i go about this.

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    first of all. your loop iterates 6 times just type (index < 5)

    for(int index=0; index<=5; index++)

    i don't think that matters too much though...

    Now to your question.

    About your first question.... What you were thinking about is
    expanding the size of the array during progam's
    execution...thtat's dynamic arrays and i don't recomend this for
    your level of programming...that's a no no....


    To best solve such a thing is to utilize simple 'string' manipulating
    routines. for example....

    Code:
    // in your preprocessor directives you need a string header...
    
    #include<string.h>
    
    int main()
    {
         string firstString;
         string secondString = "goleft";
    
         cout << "Please enter a string and press enter :";
         cin >> firstString;
    
         // what you need to do now is somehow compare firstString
         // with the initialized value in secondString... you obtain this
         // by passing 2 string to a function that's defined in string.h
         // as followes....
    
         strcmp(firstString, secondString);
    
         // blah blah....
    
    }
    i just gave you the gist how to do such a thing the rest I'll leave
    to you... you can find a tone of information on string.h and its
    functions as well as their uses.... thus you'll be able to explore
    this library and find a lot more usefull functions that let you
    manipulate strings......

    Hint: pay attention to the return types of these functions....

    Good Luck...

    matheo917

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    Thanks, I'll work on that tonight when I have time.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by matheo917

    #include<string.h>
    [...]
    strcmp(firstString, secondString);
    I would use

    #include <string>
    using std::string;

    and

    firstString == secondString
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I think strings are a bit tricky. Not as tricky as pointers maybe, but enough to be confusing at first.

    First, let's take a step back. This line declares a char array with space to hold 5 char.

    char value[5];

    This is a loop that runs 6 times:

    for(int index=0; index<=5; index++)

    To prove it do this:

    for(int index=0; index<=5; index++)
    {
    cout << index << endl;
    }

    Therefore if you do this:

    for(int index=0; index<=5; index++)
    {
    cout<<"Enter a direction:\n";
    cin>>value[index];
    }

    you will be trying to enter 6 characters in space for only 5. That won't work. The 6th char will be accepted, but the 6th char will be written to a spot in memory that may or may not be open for new input. There is no way to know. Therefore it is potentially very dangerous and should be avoided.

    If you did this:

    for(int index=0; index < 5; index++)
    {
    cout<<"Enter a direction:\n";
    cin>>value[index];
    }

    you won't overwrite the space alotted to you.

    However, let's say the problem was a typo and you meant to declare an array to hold 6 char like this:

    char value[6];

    then the for loop would be fine. The problem then would be that you would have to deal with each char one at a time, like you did. For small char arrays, that's fine. And in some instances it's exactly what you want to do. However, the string construct allows you to deal with the characters in a more convenient way. A string is defined as a null terminated array of type char. That means there must be a null character, which you will never see but will become a close friend after a while, after the last visible char in the array. In practical terms that means you must declare the char array to hold one more character than you "want". Specifically, if you want to hold the string "goleft", there are 6 visible char and one invisible char so you need a char array of at least size 7, not size 5 or size 6 to hold it.

    char value[7];

    Why bother? Well, compare the following to the loop you used:

    cout<<"Enter a direction:\n";
    cin >> value;

    Not bad, huh! Likewise you can compare two strings as well. For example if you wanted to do something if the instruction was "goleft", you could do this:

    if(strcmp(value, "goleft") == 0)
    {
    cout << "move one space to the left" << endl;
    }

    In fact, there are a whole bunch of functions in string.h (or cstring depending on your compiler) that make you life easier than dealing with individual characters in a char array. However, the syntax for strcmp isn't terribly convenient or intuitive. In C it was the best you could do, though. In C++ however, you can do more. You can write a string class to take care of much of the inconvenience of using C style strings. In fact it is so useful that a standardized string class in available in the Standard Template Library that does just that. With strings from the STL you don't have to worry about overwriting the space you alotted and you can do things like:

    string value;
    cout << "enter a direction" << endl;
    if(value == "goleftr")
    {
    cout << "move one space to the left" << endl;
    }

    In fact if you try to use an STL string with functions from string.h, you will get an error statement.

    string value;
    if(strcmp(value, "goleft") == 0)//error!!!!

    Some people prefer to use STL strings for convenience and never look under the hood at the char arrays, etc. that make it work. Others, prefer to learn manipulation of C style strings (null terminated char arrays) before moving on to STL strings as STL strings are built upon C style strings, even if you don't see the latter. Either way works, as long as you realize what you are doing.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    70

    thanks

    Thank alot. I just used the:
    Code:
    #include <iostream.h>
    #include <string.h>
    
    main()
    {
    	char value[7];
    	cout << "enter a direction" << endl;
    	cin>>value;
    	if(strcmp(value, "goleft") == 0)
    	{
    		cout << "move one space to the left" << endl;
    	}
    	else
    	{
    		cout << "nothing." << endl;
    	}
    	return 0;
    }
    code. Which works just fine, thanks again =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  2. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 04:53 AM
  3. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM