Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 2d arrays and strings

Reply
Thread Tools

2d arrays and strings

 
 
lousy beginner
Guest
Posts: n/a
 
      11-30-2003
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
 
Reply With Quote
 
 
 
 
jbruno4000
Guest
Posts: n/a
 
      11-30-2003
>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[i];
cout << endl << "Enter next symbol: ";
}

 
Reply With Quote
 
 
 
 
Jupiter5F
Guest
Posts: n/a
 
      11-30-2003

>cout <<"enter first symbol: ";
>for(int i = 0; i < 6; i++)
>{
> cin >> emoticon[i];
> 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[i];
}

That way you don't end up with an extra output statement when you're done.
 
Reply With Quote
 
Sean Kenwrick
Guest
Posts: n/a
 
      12-01-2003

"jbruno4000" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> >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[i];
> 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



 
Reply With Quote
 
jbruno4000
Guest
Posts: n/a
 
      12-01-2003
>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 ...
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      12-01-2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between << and += for Strings and Arrays. Bug? Pieter Hugo Ruby 13 11-26-2009 10:16 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM



Advertisments