| | |
Copying words into an array of char ?!?
Thread Solved |
Hello ladies and gents,
I'm trying to copy two words, for instance test and word into an array of characters and have this:
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.
I'm trying to copy two words, for instance test and word into an array of characters and have this:
C Syntax (Toggle Plain Text)
#include <iostream> int main() { char word[10]= ""; char *wordArr[10]; for(size_t i = 0; i < 2; i++) { std::cin >> word; *(wordArr + i) = word; } for(size_t i = 0; i < 2; i++) std::cout << wordArr[i] << '\n'; std::cin.ignore(2); return 0; }
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.
Well, found a solution like this:
But, there has to be a way of using
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?
C Syntax (Toggle Plain Text)
#include <iostream> int main() { char word[10]= ""; char wordArr[10][10]; for(size_t i = 0; i < 2; i++) { size_t j; std::cin >> word; for(j = 0; j < strlen(word); j++) wordArr[i][j] = word[j]; wordArr[i][j] = '\0'; } for(size_t i = 0; i < 2; i++) { for(size_t j = 0; j < strlen(wordArr[i]); j++) std::cout << wordArr[i][j]; std::cout << '\n'; } std::cin.ignore(2); return 0; }
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?
•
•
Join Date: Jul 2005
Posts: 1,673
Reputation:
Solved Threads: 261
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
Inputting words into an array indirectly
Using pointer to pointer to char instead of array of char arrays
Using array of char pointers instead of pointer to pointer to char
Another version using array of char pointers:
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
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char word[10]= ""; char wordArr[10][10]; int i; for(i = 0; i < 10; ++i) cin >> wordArr[i]; for(i = 0; i < 10; ++i) cout << wordArr[i] << endl; cin.get(); }
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char word[10] = ""; char wordArr[10][10]; int i; for(i = 0; i < 10; ++i) { cin >> word; strcpy(wordArr[i], word); } for(i = 0; i < 10; ++i) cout << wordArr[i] << endl; cin.get(); }
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char **wordArr; int i, j, k; cin >> i; cin >> j; wordArr = new char*[i]; for( j = 0; j < i; ++i) wordArr[i] = new char[j]; for(j = 0; j < i; ++j) cin >> wordArr[j]; for(j = 0; j < i; ++j) cout >> wordArr[j] << endl; for(j = 0; j < i; ++j) delete[] wordArr[i]; delete wordArr; cin.get(); }
C Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int main() { char *wordArr[10]; int i, j; cin >> j; for(i = 0; i < 10; ++i) wordArr[i] = new char[j]; for(i = 0; i < 10; ++i) cin >> wordArr[i]; for (i = 0; i < 10; ++i) cout << wordArr[i] << endl; for(i = 0; i < 10; ++i) delete[] wordArr[i]; cin.get() }
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { char * wordArr[2] = { {"Hello"}, {"World"} }; cout << wordArr[0] << ' ' << wordArr[1] << endl; cin.get() }
Last edited by Lerner; Nov 27th, 2006 at 11:00 am.
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
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 :!:
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 :!:
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?
The intention here was just two add two words into the array to try out. Was I correct that I had to add the
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?
C Syntax (Toggle Plain Text)
for(size_t i = 0; i < 2; i++) { size_t j; std::cin >> word; for(j = 0; j < strlen(word); j++) wordArr[i][j] = word[j]; wordArr[i][j] = '\0'; }
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? 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?
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?
C Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> int main() { char wordArr[5][10]; short contr; for(size_t i = 0; i < 5; i++) std::cin >> wordArr[i]; for(size_t i = 0; i < 5; i++) { contr = strcmp(wordArr[i], wordArr[i+1]); if (contr != 0) std::cout << wordArr[i] << '\n'; } std::cin.ignore(2); return 0; }
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 .
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.
•
•
Join Date: Jul 2005
Posts: 1,673
Reputation:
Solved Threads: 261
>>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.
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:
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.
C Syntax (Toggle Plain Text)
for(size_t i = 0; i < 5; i++) //for each word in the array { //compare this word with the next word in the array contr = strcmp(wordArr[i], wordArr[i+1]); //if the two words are not the same if (contr != 0) //print out current word std::cout << wordArr[i] << '\n'; }
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:
C Syntax (Toggle Plain Text)
bool found = false for each word in the array //this word for each word prior to this word //current word if this word is the same as current word set found to true if not found print out this word
>> 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."
I'll try my best to explain it more thoroughly next time :!:
@ Lerner, thanks for the explanation.
•
•
•
•
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.
@ Lerner, thanks for the explanation.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Linker info
- Next Thread: Deleting characters function ( with just one parameter)
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






