Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,124 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,780 people online right now. Registration is fast and FREE... Join Now!




adding a char to aan array of chars

 
Reply to this topicStart new topic

adding a char to aan array of chars

sergec
13 May, 2007 - 10:47 PM
Post #1

New D.I.C Head
*

Joined: 8 Nov, 2006
Posts: 8


My Contributions
Hi everybody

I am trying to make a little game of words: the user [child] gets a certain word, he/she has to add a letter to this word to create another word.

lets say i have this word

char word[4] = "Hell";

The user will type 'o' and he/she will get:
char new_word[5] = "Hello".

I cannot figure how to add a letter to this array.

Thank you for your help

Serge


User is offlineProfile CardPM
+Quote Post

Xing
RE: Adding A Char To Aan Array Of Chars
14 May, 2007 - 12:27 AM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
You can make use of strcat() or simply assigning to the fifth position.
User is offlineProfile CardPM
+Quote Post

sergec
RE: Adding A Char To Aan Array Of Chars
14 May, 2007 - 02:26 AM
Post #3

New D.I.C Head
*

Joined: 8 Nov, 2006
Posts: 8


My Contributions
QUOTE(Xing @ 14 May, 2007 - 01:27 AM) *

You can make use of strcat() or simply assigning to the fifth position.



If I use strcat() I get: "Invalid conversion from 'char' to 'const char *'.
And the second solution I already tried does not fit...: it does not work

Serge
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Adding A Char To Aan Array Of Chars
14 May, 2007 - 04:19 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
You'll have to add the required amount of elements (or memory) to the previously declared array.

Will it always be one additional letter? If so, you can simply declare an array large enough for your use originally, but if the amount of letters to be added may vary, you're better to re-size the array.

Are you using C or C++?
User is offlineProfile CardPM
+Quote Post

sergec
RE: Adding A Char To Aan Array Of Chars
14 May, 2007 - 05:02 AM
Post #5

New D.I.C Head
*

Joined: 8 Nov, 2006
Posts: 8


My Contributions
QUOTE(Amadeus @ 14 May, 2007 - 05:19 AM) *

You'll have to add the required amount of elements (or memory) to the previously declared array.

Will it always be one additional letter? If so, you can simply declare an array large enough for your use originally, but if the amount of letters to be added may vary, you're better to re-size the array.

Are you using C or C++?



I am using C++ and just one letter will always be added in any case
The compiler always complains of invalid conversion, I am under Linux.

Serge
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Adding A Char To Aan Array Of Chars
14 May, 2007 - 07:32 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
If you are using C++, why not use the string object instead of a character array?
CODE

string str1 = "Hell";
string str2="o";
string str3 = str1+str2;
cout<<str3;


User is offlineProfile CardPM
+Quote Post

sergec
RE: Adding A Char To Aan Array Of Chars
14 May, 2007 - 11:26 AM
Post #7

New D.I.C Head
*

Joined: 8 Nov, 2006
Posts: 8


My Contributions
QUOTE(Amadeus @ 14 May, 2007 - 08:32 AM) *

If you are using C++, why not use the string object instead of a character array?
CODE

string str1 = "Hell";
string str2="o";
string str3 = str1+str2;
cout<<str3;




Hi Amadeus

I usually use strings instead but it did not match in what i wanted to do...
However finally I found this solution after a lot of thoughts and tries...:

CODE

char wholeword[7];
char word[] = "Hello";
char letter = 's';
for(int x = 0; x < 5; x++)
    {
    wholeword[x] = word[x]; //5 letters of word
    }

wholeword[5] = letter;
wholeword[6] = word[5]; //NULL

cout << wholeword;
/

Serge
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Adding A Char To Aan Array Of Chars
14 May, 2007 - 11:48 AM
Post #8

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
If u want to do it whit char arrays do it like this:
CODE
char addchar='o';
char phrase[300]="hell";
int size;
for(int a = 0; a < 300;a++){//this loop is to determine the size
if(phrase[a]=='\0'){
size=a;
break;
}
}
phrase[size++]=addchar;//here is the code to put a char at the end
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Adding A Char To Aan Array Of Chars
14 May, 2007 - 12:02 PM
Post #9

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
That is a lot of wasted memory.
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Adding A Char To Aan Array Of Chars
14 May, 2007 - 12:04 PM
Post #10

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
QUOTE(Amadeus @ 14 May, 2007 - 01:02 PM) *

That is a lot of wasted memory.

What do you mean?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Adding A Char To Aan Array Of Chars
14 May, 2007 - 12:05 PM
Post #11

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
I mean that you have declared an array of 300 elements, of which you use 5 or six. the rest of the memory has been set aside in the application, but will not be used.
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Adding A Char To Aan Array Of Chars
14 May, 2007 - 12:17 PM
Post #12

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
i dont work alot with char arrays, so i dont know how to do it the best.

This post has been edited by manhaeve5: 14 May, 2007 - 12:17 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:11PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month