2d arrays and strings

Discussion in 'C++' started by lousy beginner, Nov 30, 2003.

  1. 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
     
    lousy beginner, Nov 30, 2003
    #1
    1. Advertising

  2. lousy beginner

    jbruno4000 Guest

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


    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;
    cout << endl << "Enter next symbol: ";
    }
     
    jbruno4000, Nov 30, 2003
    #2
    1. Advertising

  3. lousy beginner

    Jupiter5F Guest


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

    Actually, a better way to write that is:

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

    That way you don't end up with an extra output statement when you're done.
     
    Jupiter5F, Nov 30, 2003
    #3
  4. "jbruno4000" <> wrote in message
    news:...
    > >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
    > >
    > >
    > >
    > >
    > >
    > >

    >
    > 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;
    > cout << endl << "Enter next symbol: ";
    > }
    >


    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(emoticons[0],":)")==0)
    printf("WhooHoo\n");

    Hope this helps...

    Sean
     
    Sean Kenwrick, Dec 1, 2003
    #4
  5. lousy beginner

    jbruno4000 Guest

    >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(emoticons[0],":)")==0)
    > printf("WhooHoo\n");
    >
    >Hope this helps...
    >
    >Sean
    >
    >
    >

    I initially thought your version was the sort of thing the originator was after
    but it all seemed too easy. Anyway ...
     
    jbruno4000, Dec 1, 2003
    #5
  6. lousy beginner

    red floyd Guest

    jbruno4000 wrote:

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


    I'd make it:

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

    That's more of the intent of that sort of table anyways.
     
    red floyd, Dec 1, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Reyn
    Replies:
    3
    Views:
    2,341
    Bob Hairgrove
    Jun 22, 2004
  2. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    404
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    810
    Malcolm
    Jun 24, 2006
  4. Philipp
    Replies:
    21
    Views:
    1,184
    Philipp
    Jan 20, 2009
  5. Pieter Hugo
    Replies:
    13
    Views:
    217
    Robert Klemme
    Nov 26, 2009
Loading...

Share This Page