2d arrays and strings

L

lousy beginner

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
 
J

jbruno4000

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: ";
}
 
J

Jupiter5F

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

Sean Kenwrick

jbruno4000 said:
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
 
J

jbruno4000

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top