Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with setting values to char arrays (strings);

Help with setting values to char arrays (strings);

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Coolarj10
    wrote on last edited by
    #1

    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; }

    G M C 3 Replies Last reply
    0
    • C Coolarj10

      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; }

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      First problem: The line word[0][128] = "word1"; doesn't assign the string "word1" to word[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 the strcpy() 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;

      Fold With Us![^]

      1 Reply Last reply
      0
      • C Coolarj10

        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; }

        M Offline
        M Offline
        meinhard_risch
        wrote on last edited by
        #3

        or try this for(i=0;i<4;i++) sprintf(word[i],"word%d",i); :rose: Meinhard

        1 Reply Last reply
        0
        • C Coolarj10

          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; }

          C Offline
          C Offline
          Coolarj10
          wrote on last edited by
          #4

          Thank you so much everybody for your help! My program finally works thanks to your help!

          G M 2 Replies Last reply
          0
          • C Coolarj10

            Thank you so much everybody for your help! My program finally works thanks to your help!

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #5

            You're welcome :rose:.


            Software Zen: delete this;

            Fold With Us![^]

            1 Reply Last reply
            0
            • C Coolarj10

              Thank you so much everybody for your help! My program finally works thanks to your help!

              M Offline
              M Offline
              meinhard_risch
              wrote on last edited by
              #6

              :-O

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups