2d arrays and strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lousy beginner

    2d arrays and strings

    Hi there,

    I need some help (obviously...), so if anyone can give me some advice
    I'd really appreciate it!
    I have a c++ project to do (for college) which can take input from the
    user in the form of an emoticon (;) :) :-| etc) and then output a
    short description of the emoticon.
    But I've only started reading on two-dimensional arrays, and i still
    don't know how to use them properly, so I can't even get past the
    initialising part of the program... how do i use multi-character
    arrays?... do i start a for-loop to fill in the 2d array which holds
    the emoticons? or do i use {} and in between i put the emoticons,
    separated by commas?... (is strcpy involved in any way in here?)
    please help, i promise i'll do my best with the other parts of the
    project if i can get help for this one!
    thanks for readin,

    lousy 1st-year-computer-student
  • jbruno4000

    #2
    Re: 2d arrays and strings

    >Hi there,[color=blue]
    >
    >I need some help (obviously...), so if anyone can give me some advice
    >I'd really appreciate it!
    >I have a c++ project to do (for college) which can take input from the
    >user in the form of an emoticon (;) :) :-| etc) and then output a
    >short description of the emoticon.
    >But I've only started reading on two-dimensional arrays, and i still
    >don't know how to use them properly, so I can't even get past the
    >initialising part of the program... how do i use multi-character
    >arrays?... do i start a for-loop to fill in the 2d array which holds
    >the emoticons? or do i use {} and in between i put the emoticons,
    >separated by commas?... (is strcpy involved in any way in here?)
    >please help, i promise i'll do my best with the other parts of the
    >project if i can get help for this one!
    >thanks for readin,
    >
    >lousy 1st-year-computer-student
    >
    >
    >
    >
    >
    >[/color]

    Here are 2 ways you could initialize your array:

    char emoticon[6] = {';', '(', ')', ':', '-', '|'};

    anotherway is to load them within the program:

    char emoticon[6];

    cout <<"enter first symbol: ";
    for(int i = 0; i < 6; i++)
    {
    cin >> emoticon[i];
    cout << endl << "Enter next symbol: ";
    }

    Comment

    • Jupiter5F

      #3
      Re: 2d arrays and strings

      [color=blue]
      >cout <<"enter first symbol: ";
      >for(int i = 0; i < 6; i++)
      >{
      > cin >> emoticon[i];
      > cout << endl << "Enter next symbol: ";
      >}
      >
      >
      >[/color]
      Actually, a better way to write that is:

      for(int i = 0; i < 6; i++)
      {
      cout << endl << "Enter symbol: ";
      cin >> emoticon[i];
      }

      That way you don't end up with an extra output statement when you're done.

      Comment

      • Sean Kenwrick

        #4
        Re: 2d arrays and strings


        "jbruno4000 " <jbruno4000@aol .com> wrote in message
        news:2003113012 1805.25936.0000 1233@mb-m05.aol.com...[color=blue][color=green]
        > >Hi there,
        > >
        > >I need some help (obviously...), so if anyone can give me some advice
        > >I'd really appreciate it!
        > >I have a c++ project to do (for college) which can take input from the
        > >user in the form of an emoticon (;) :) :-| etc) and then output a
        > >short description of the emoticon.
        > >But I've only started reading on two-dimensional arrays, and i still
        > >don't know how to use them properly, so I can't even get past the
        > >initialising part of the program... how do i use multi-character
        > >arrays?... do i start a for-loop to fill in the 2d array which holds
        > >the emoticons? or do i use {} and in between i put the emoticons,
        > >separated by commas?... (is strcpy involved in any way in here?)
        > >please help, i promise i'll do my best with the other parts of the
        > >project if i can get help for this one!
        > >thanks for readin,
        > >
        > >lousy 1st-year-computer-student
        > >
        > >
        > >
        > >
        > >
        > >[/color]
        >
        > Here are 2 ways you could initialize your array:
        >
        > char emoticon[6] = {';', '(', ')', ':', '-', '|'};
        >
        > anotherway is to load them within the program:
        >
        > char emoticon[6];
        >
        > cout <<"enter first symbol: ";
        > for(int i = 0; i < 6; i++)
        > {
        > cin >> emoticon[i];
        > cout << endl << "Enter next symbol: ";
        > }
        >[/color]

        A better way (IMHO) is as follows:

        char * emoticons[]={
        ":-)",
        ";-)",
        ":-(".
        };

        It easier to read (and to type). Then each element of the array called
        emoticons is a pointer to a null terminated string and you can access each
        character of the emoticon like you would for a 2d array e.g:

        c=emoticons[0][0]; // this would be a :

        also you can do string comparisons like:

        if(strcmp(emoti cons[0],":-)")==0)
        printf("WhooHoo \n");

        Hope this helps...

        Sean



        Comment

        • jbruno4000

          #5
          Re: 2d arrays and strings

          >A better way (IMHO) is as follows:[color=blue]
          >
          >char * emoticons[]={
          > ":-)",
          > ";-)",
          > ":-(".
          >};
          >
          >It easier to read (and to type). Then each element of the array called
          >emoticons is a pointer to a null terminated string and you can access each
          >character of the emoticon like you would for a 2d array e.g:
          >
          >c=emoticons[0][0]; // this would be a :
          >
          >also you can do string comparisons like:
          >
          >if(strcmp(emot icons[0],":-)")==0)
          > printf("WhooHoo \n");
          >
          >Hope this helps...
          >
          >Sean
          >
          >
          >[/color]
          I initially thought your version was the sort of thing the originator was after
          but it all seemed too easy. Anyway ...

          Comment

          • red floyd

            #6
            Re: 2d arrays and strings

            jbruno4000 wrote:
            [color=blue][color=green]
            >>A better way (IMHO) is as follows:
            >>
            >>char * emoticons[]={
            >> ":-)",
            >> ";-)",
            >> ":-(".
            >>};
            >>[/color][/color]

            I'd make it:

            const char * emoticons[] = {
            // yada yada yada
            };

            That's more of the intent of that sort of table anyways.

            Comment

            Working...