Help with setting values to char arrays (strings);
-
I have a problem with this code, if anyone could please help me out, I would greatly appreciate it! I'm trying to make a program that has a certain number of 'char arrays.' When the program starts, it's supposed to randomly concatentate any of the two strings. My program has a problem in four lines of code, and I don't understand how to do it any different. Thank you for your time and help! (P.S., though this snippet of code only shows 4 different strings, my actual program will have at least 25); //Test program. The object of this program is to //have different strings randomly concatenate #include #include using namespace std; //or #include , depending on the compiler #include int main() { srand(GetTickCount()); char word[4][128]; //I want to store different strings into word[0], word[1], word[2], and word[3] word[0][128] = "word1"; //This is set of four lines is where the problem is word[1][128] = "word2"; word[2][128] = "word3"; word[3][128] = "word4"; //----end of the problem int firstword; int secondword; firstword = rand() % 4; secondword = rand() % 4; strcat(word[firstword], word[secondword]); cout << ""<< word[firstword] <<""; cin.ignore(); return 0; }
-
I have a problem with this code, if anyone could please help me out, I would greatly appreciate it! I'm trying to make a program that has a certain number of 'char arrays.' When the program starts, it's supposed to randomly concatentate any of the two strings. My program has a problem in four lines of code, and I don't understand how to do it any different. Thank you for your time and help! (P.S., though this snippet of code only shows 4 different strings, my actual program will have at least 25); //Test program. The object of this program is to //have different strings randomly concatenate #include #include using namespace std; //or #include , depending on the compiler #include int main() { srand(GetTickCount()); char word[4][128]; //I want to store different strings into word[0], word[1], word[2], and word[3] word[0][128] = "word1"; //This is set of four lines is where the problem is word[1][128] = "word2"; word[2][128] = "word3"; word[3][128] = "word4"; //----end of the problem int firstword; int secondword; firstword = rand() % 4; secondword = rand() % 4; strcat(word[firstword], word[secondword]); cout << ""<< word[firstword] <<""; cin.ignore(); return 0; }
First problem: The line
word[0][128] = "word1";
doesn't assign the string "word1" toword[0]
, which is what I assume you want to do. These four lines probably shouldn't even compile, since on the left side of the assignment you are referencing a single character (word[0][128]
) and on the right side you have a string constant. Second problem: You can't assign simple strings in the way you have. In C/C++, for simple strings, you use thestrcpy()
function. Here's that part of the code, cleaned up:strcpy(word[0],"word1");
strcpy(word[1],"word2");
strcpy(word[2],"word3");
strcpy(word[3],"word4");
Software Zen:
delete this;
-
I have a problem with this code, if anyone could please help me out, I would greatly appreciate it! I'm trying to make a program that has a certain number of 'char arrays.' When the program starts, it's supposed to randomly concatentate any of the two strings. My program has a problem in four lines of code, and I don't understand how to do it any different. Thank you for your time and help! (P.S., though this snippet of code only shows 4 different strings, my actual program will have at least 25); //Test program. The object of this program is to //have different strings randomly concatenate #include #include using namespace std; //or #include , depending on the compiler #include int main() { srand(GetTickCount()); char word[4][128]; //I want to store different strings into word[0], word[1], word[2], and word[3] word[0][128] = "word1"; //This is set of four lines is where the problem is word[1][128] = "word2"; word[2][128] = "word3"; word[3][128] = "word4"; //----end of the problem int firstword; int secondword; firstword = rand() % 4; secondword = rand() % 4; strcat(word[firstword], word[secondword]); cout << ""<< word[firstword] <<""; cin.ignore(); return 0; }
or try this for(i=0;i<4;i++) sprintf(word[i],"word%d",i); :rose: Meinhard
-
I have a problem with this code, if anyone could please help me out, I would greatly appreciate it! I'm trying to make a program that has a certain number of 'char arrays.' When the program starts, it's supposed to randomly concatentate any of the two strings. My program has a problem in four lines of code, and I don't understand how to do it any different. Thank you for your time and help! (P.S., though this snippet of code only shows 4 different strings, my actual program will have at least 25); //Test program. The object of this program is to //have different strings randomly concatenate #include #include using namespace std; //or #include , depending on the compiler #include int main() { srand(GetTickCount()); char word[4][128]; //I want to store different strings into word[0], word[1], word[2], and word[3] word[0][128] = "word1"; //This is set of four lines is where the problem is word[1][128] = "word2"; word[2][128] = "word3"; word[3][128] = "word4"; //----end of the problem int firstword; int secondword; firstword = rand() % 4; secondword = rand() % 4; strcat(word[firstword], word[secondword]); cout << ""<< word[firstword] <<""; cin.ignore(); return 0; }
-
Thank you so much everybody for your help! My program finally works thanks to your help!
-
Thank you so much everybody for your help! My program finally works thanks to your help!
:-O