By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

2d arrays and strings

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
>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: ";
}

Jul 22 '05 #2

P: n/a
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.
Jul 22 '05 #3

P: n/a

"jbruno4000" <jb********@aol.com> wrote in message
news:20***************************@mb-m05.aol.com...
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

Jul 22 '05 #4

P: n/a
>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 ...
Jul 22 '05 #5

P: n/a
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.

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.