Copying words into an array of char ?!?

Thread Solved

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Copying words into an array of char ?!?

 
0
  #1
Nov 27th, 2006
Hello ladies and gents,

I'm trying to copy two words, for instance test and word into an array of characters and have this:

  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. char word[10]= "";
  6. char *wordArr[10];
  7.  
  8. for(size_t i = 0; i < 2; i++)
  9. {
  10. std::cin >> word;
  11. *(wordArr + i) = word;
  12. }
  13.  
  14. for(size_t i = 0; i < 2; i++)
  15. std::cout << wordArr[i] << '\n';
  16.  
  17. std::cin.ignore(2);
  18.  
  19. return 0;
  20. }

Thing is, it prints the last word twice as if it doesn't store the first word, what am I doing wrong, can anyone point me in the right direction?

I know I could use strings or even a vector, but, my idea is to work out a version using characters first, then strings and then a vector.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Copying words into an array of char ?!?

 
0
  #2
Nov 27th, 2006
Well, found a solution like this:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. char word[10]= "";
  6. char wordArr[10][10];
  7.  
  8. for(size_t i = 0; i < 2; i++)
  9. {
  10. size_t j;
  11. std::cin >> word;
  12. for(j = 0; j < strlen(word); j++)
  13. wordArr[i][j] = word[j];
  14. wordArr[i][j] = '\0';
  15. }
  16.  
  17. for(size_t i = 0; i < 2; i++)
  18. {
  19. for(size_t j = 0; j < strlen(wordArr[i]); j++)
  20. std::cout << wordArr[i][j];
  21. std::cout << '\n';
  22. }
  23.  
  24. std::cin.ignore(2);
  25.  
  26. return 0;
  27. }

But, there has to be a way of using char *wordArr[10];
right?

It would make it easier since the use of a pointer to an array of char's wouldn't need to be terminated by yourself, the compiler would do this like Ancient Dragon said in another thread right.

Does any of you guys know how I could use that type of definition in my program?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Copying words into an array of char ?!?

 
1
  #3
Nov 27th, 2006
Hope something in here meets your needs and helps you understand the syntax.

char * wordArr[10];

means wordArr is an array of 10 char pointers, not that wordArray is a pointer to 10 char arrays.

char * word = new char[10];

means word is a pointer to a char array that can contain up to 10 char.

Now, an example of putting words into array directly. without need for user termination
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. char word[10]= "";
  7. char wordArr[10][10];
  8. int i;
  9.  
  10. for(i = 0; i < 10; ++i)
  11. cin >> wordArr[i];
  12.  
  13. for(i = 0; i < 10; ++i)
  14. cout << wordArr[i] << endl;
  15.  
  16. cin.get();
  17. }
Inputting words into an array indirectly
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. char word[10] = "";
  7. char wordArr[10][10];
  8. int i;
  9.  
  10. for(i = 0; i < 10; ++i)
  11. {
  12. cin >> word;
  13. strcpy(wordArr[i], word);
  14. }
  15.  
  16. for(i = 0; i < 10; ++i)
  17. cout << wordArr[i] << endl;
  18.  
  19. cin.get();
  20. }
Using pointer to pointer to char instead of array of char arrays
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. char **wordArr;
  7. int i, j, k;
  8. cin >> i;
  9. cin >> j;
  10. wordArr = new char*[i];
  11. for( j = 0; j < i; ++i)
  12. wordArr[i] = new char[j];
  13.  
  14. for(j = 0; j < i; ++j)
  15. cin >> wordArr[j];
  16.  
  17. for(j = 0; j < i; ++j)
  18. cout >> wordArr[j] << endl;
  19.  
  20. for(j = 0; j < i; ++j)
  21. delete[] wordArr[i];
  22.  
  23. delete wordArr;
  24. cin.get();
  25. }
Using array of char pointers instead of pointer to pointer to char
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. char *wordArr[10];
  7. int i, j;
  8. cin >> j;
  9.  
  10. for(i = 0; i < 10; ++i)
  11. wordArr[i] = new char[j];
  12.  
  13. for(i = 0; i < 10; ++i)
  14. cin >> wordArr[i];
  15.  
  16. for (i = 0; i < 10; ++i)
  17. cout << wordArr[i] << endl;
  18.  
  19. for(i = 0; i < 10; ++i)
  20. delete[] wordArr[i];
  21.  
  22. cin.get()
  23. }
Another version using array of char pointers:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. char * wordArr[2] = { {"Hello"}, {"World"} };
  7. cout << wordArr[0] << ' ' << wordArr[1] << endl;
  8.  
  9. cin.get()
  10. }
Last edited by Lerner; Nov 27th, 2006 at 11:00 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Copying words into an array of char ?!?

 
0
  #4
Nov 27th, 2006
Thanks for those examples Lerner. The reason I came up with my first example code was because the exercise tells you not to store two identical words, therefore I used two variables char word[10]= ""; and char wordArr[10][10];. That way, I can compare when entering a new word with the already added words to check whether it's not in the list.

I should've written the exercise out, but, I wanted to take this one in small parts as you guys always tell forumers to do, so they don't get overwhelmed with the code. Sorry for that. Anyway, the exercise goes as follows:

- Read a sequence of words from input. Use Quit as a word that therminates input. Print the words in the order they were entered. Don't print a word twice. Modify the program to sort the words before printing them.

I'll be back next week-end, don't have the time to do the exercise now, anyway, thanks again for the thourough explanatioin and examples :!:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Copying words into an array of char ?!?

 
0
  #5
Dec 2nd, 2006
Hey Lerner,

Finally got time to work on this exercise again, but, before I do, I'd like to ask whether this way of adding a word into another array is good or not. If not, what are the reasons this is not ideal?

  1. for(size_t i = 0; i < 2; i++)
  2. {
  3. size_t j;
  4. std::cin >> word;
  5. for(j = 0; j < strlen(word); j++)
  6. wordArr[i][j] = word[j];
  7. wordArr[i][j] = '\0';
  8. }

The intention here was just two add two words into the array to try out. Was I correct that I had to add the '\0'to the end of the word so that it would be correctly terminated?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Copying words into an array of char ?!?

 
0
  #6
Dec 2nd, 2006
Hi guys,

Was wondering if any of you could help me out, I'm trying to print the words only once, but, it seems I'm doing something wrong, could someone point me in the right direction?

  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. int main()
  5. {
  6. char wordArr[5][10];
  7. short contr;
  8.  
  9. for(size_t i = 0; i < 5; i++)
  10. std::cin >> wordArr[i];
  11.  
  12. for(size_t i = 0; i < 5; i++)
  13. {
  14. contr = strcmp(wordArr[i], wordArr[i+1]);
  15.  
  16. if (contr != 0)
  17. std::cout << wordArr[i] << '\n';
  18. }
  19.  
  20. std::cin.ignore(2);
  21.  
  22. return 0;
  23. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Copying words into an array of char ?!?

 
0
  #7
Dec 2nd, 2006
Why ain't you using std::strings?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Copying words into an array of char ?!?

 
0
  #8
Dec 2nd, 2006
Just wanted to tell you that sometimes your description is rather vague. Always post a question in such way that the person reading it doesn't have to use a compiler to test it out what is going wrong.

If your program displays something and takes some kind of user input, just paste a sample output which is erroneous or not as expected along with what did you expect. This kind of description will help us understanding the problem better.

Now as per your question is concerned, your program only checks to see whether the preceding string is equal to the current string and if it is so then it just skips printing the current string. But your program will fail in conditions where you alternate the words inputted to the program like:

hello
hell
hello
hell
hello

I hope you understand the twist .
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Copying words into an array of char ?!?

 
0
  #9
Dec 2nd, 2006
>>Was I correct that I had to add the '\0'to the end of the word so that it would be correctly terminated?

No, cin >> automatically null terminates input from either the keyboard or antother stream. The same goes for getline(), which is another way to read in a string.

  1. for(size_t i = 0; i < 5; i++) //for each word in the array
  2. {
  3. //compare this word with the next word in the array
  4. contr = strcmp(wordArr[i], wordArr[i+1]);
  5.  
  6. //if the two words are not the same
  7. if (contr != 0)
  8. //print out current word
  9. std::cout << wordArr[i] << '\n';
  10. }

You don't want that for two reasons. Number one, when i == 4 you will try to compare wordArr[4] with wordArr[5] but wordArr[5] doesn't exist. If you are lucky your program will crash. If you are unlucky you will get undefined behavior and who knows what will happen, it may even appear to work correctly. Number two, the logic isn't what you want. You want:
  1. bool found = false
  2. for each word in the array //this word
  3. for each word prior to this word //current word
  4. if this word is the same as current word
  5. set found to true
  6. if not found
  7. print out this word
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Copying words into an array of char ?!?

 
0
  #10
Dec 2nd, 2006
Originally Posted by iamthwee View Post
Why ain't you using std::strings?
>> First post "I know I could use strings or even a vector, but, my idea is to work out a version using characters first, then strings and then a vector."

Originally Posted by ~s.o.s~
Just wanted to tell you that sometimes your description is rather vague. Always post a question in such way that the person reading it doesn't have to use a compiler to test it out what is going wrong.
I'll try my best to explain it more thoroughly next time :!:

@ Lerner, thanks for the explanation.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC