Create 2 parallel string arrays from text file using getlineHow to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?Create ArrayList from arrayHow do I create a Java string from the contents of a file?PHP: Delete an element from an arraySort array of objects by string property valueHow do I create a file and write to it in Java?Reading a plain text file in JavaConvert ArrayList<String> to String[] arrayHow do I remove a particular element from an array in JavaScript?Loop through an array of strings in Bash?

Banach space and Hilbert space topology

What makes Graph invariants so useful/important?

How is it possible to have an ability score that is less than 3?

Japan - Plan around max visa duration

Can I interfere when another PC is about to be attacked?

Shell script can be run only with sh command

What is the offset in a seaplane's hull?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

Email Account under attack (really) - anything I can do?

Why linear maps act like matrix multiplication?

DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?

Why is an old chain unsafe?

What defenses are there against being summoned by the Gate spell?

"which" command doesn't work / path of Safari?

When blogging recipes, how can I support both readers who want the narrative/journey and ones who want the printer-friendly recipe?

Draw simple lines in Inkscape

What is the command to reset a PC without deleting any files

Is there a familial term for apples and pears?

Validation accuracy vs Testing accuracy

If Manufacturer spice model and Datasheet give different values which should I use?

What would the Romans have called "sorcery"?

Are there any consumables that function as addictive (psychedelic) drugs?

Why don't electromagnetic waves interact with each other?

GPS Rollover on Android Smartphones



Create 2 parallel string arrays from text file using getline


How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?Create ArrayList from arrayHow do I create a Java string from the contents of a file?PHP: Delete an element from an arraySort array of objects by string property valueHow do I create a file and write to it in Java?Reading a plain text file in JavaConvert ArrayList<String> to String[] arrayHow do I remove a particular element from an array in JavaScript?Loop through an array of strings in Bash?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















For this task, I am opening a text file and trying to read lines 1 and 3 into the array named front (at indices 0 and 1 respectively), and then read lines 2 and 4 into the array named back (at indices 0 and 1 respectively), but it's not quite doing it. Nothing is getting inputted into the arrays, my loop logic must be off. I want to read each line as is (with spaces included) up until the newline character. Any help is appreciated.



void initialize(string front[], string back[], ifstream &inFile)

string fileName = "DevDeck.txt"; //Filename
string line;
inFile.open(fileName); //Open filename

if (!inFile)

cout << "Couldn't open the file" << endl;
exit(1);


//Create the parallel arrays
while (!inFile.eof())

for (int index = 0; index < 4; index++)

getline(inFile, line);
front[index] = line; //Store in first array

getline(inFile, line);
back[index] = line; //Store in second array













share|improve this question




























    0















    For this task, I am opening a text file and trying to read lines 1 and 3 into the array named front (at indices 0 and 1 respectively), and then read lines 2 and 4 into the array named back (at indices 0 and 1 respectively), but it's not quite doing it. Nothing is getting inputted into the arrays, my loop logic must be off. I want to read each line as is (with spaces included) up until the newline character. Any help is appreciated.



    void initialize(string front[], string back[], ifstream &inFile)

    string fileName = "DevDeck.txt"; //Filename
    string line;
    inFile.open(fileName); //Open filename

    if (!inFile)

    cout << "Couldn't open the file" << endl;
    exit(1);


    //Create the parallel arrays
    while (!inFile.eof())

    for (int index = 0; index < 4; index++)

    getline(inFile, line);
    front[index] = line; //Store in first array

    getline(inFile, line);
    back[index] = line; //Store in second array













    share|improve this question
























      0












      0








      0








      For this task, I am opening a text file and trying to read lines 1 and 3 into the array named front (at indices 0 and 1 respectively), and then read lines 2 and 4 into the array named back (at indices 0 and 1 respectively), but it's not quite doing it. Nothing is getting inputted into the arrays, my loop logic must be off. I want to read each line as is (with spaces included) up until the newline character. Any help is appreciated.



      void initialize(string front[], string back[], ifstream &inFile)

      string fileName = "DevDeck.txt"; //Filename
      string line;
      inFile.open(fileName); //Open filename

      if (!inFile)

      cout << "Couldn't open the file" << endl;
      exit(1);


      //Create the parallel arrays
      while (!inFile.eof())

      for (int index = 0; index < 4; index++)

      getline(inFile, line);
      front[index] = line; //Store in first array

      getline(inFile, line);
      back[index] = line; //Store in second array













      share|improve this question














      For this task, I am opening a text file and trying to read lines 1 and 3 into the array named front (at indices 0 and 1 respectively), and then read lines 2 and 4 into the array named back (at indices 0 and 1 respectively), but it's not quite doing it. Nothing is getting inputted into the arrays, my loop logic must be off. I want to read each line as is (with spaces included) up until the newline character. Any help is appreciated.



      void initialize(string front[], string back[], ifstream &inFile)

      string fileName = "DevDeck.txt"; //Filename
      string line;
      inFile.open(fileName); //Open filename

      if (!inFile)

      cout << "Couldn't open the file" << endl;
      exit(1);


      //Create the parallel arrays
      while (!inFile.eof())

      for (int index = 0; index < 4; index++)

      getline(inFile, line);
      front[index] = line; //Store in first array

      getline(inFile, line);
      back[index] = line; //Store in second array










      c++ arrays loops file-io istream






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 5:35









      CornelCornel

      42127




      42127






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Your loop for (int index = 0; index < 4; index++) has the wrong condition since you want 4 strings in total but in each loop you get 2 so right now you would get 8 strings.



          I tried to run your code with that change like this:



          int main()

          string front[2];
          string back[2];

          ifstream inFile;

          initialize(front, back, inFile);

          cout << front[0] << endl << back[0] << endl << front[1] << endl << back[1];

          return 0;



          and it worked perfectly for me. It displayed:



          line1
          line2
          line3
          line4


          In order to help you further you should provide the DevDeck.txt file and the piece of code that calls this function.






          share|improve this answer























          • Thank you. It looks like the array sizes were off. //Global constant const int NUMCARDS = 2; /inside main string front[NUMCARDS]; string back[NUMCARDS]; ifstream infile; Works great now. Ty.

            – Cornel
            Mar 8 at 6:10












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55057290%2fcreate-2-parallel-string-arrays-from-text-file-using-getline%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Your loop for (int index = 0; index < 4; index++) has the wrong condition since you want 4 strings in total but in each loop you get 2 so right now you would get 8 strings.



          I tried to run your code with that change like this:



          int main()

          string front[2];
          string back[2];

          ifstream inFile;

          initialize(front, back, inFile);

          cout << front[0] << endl << back[0] << endl << front[1] << endl << back[1];

          return 0;



          and it worked perfectly for me. It displayed:



          line1
          line2
          line3
          line4


          In order to help you further you should provide the DevDeck.txt file and the piece of code that calls this function.






          share|improve this answer























          • Thank you. It looks like the array sizes were off. //Global constant const int NUMCARDS = 2; /inside main string front[NUMCARDS]; string back[NUMCARDS]; ifstream infile; Works great now. Ty.

            – Cornel
            Mar 8 at 6:10
















          1














          Your loop for (int index = 0; index < 4; index++) has the wrong condition since you want 4 strings in total but in each loop you get 2 so right now you would get 8 strings.



          I tried to run your code with that change like this:



          int main()

          string front[2];
          string back[2];

          ifstream inFile;

          initialize(front, back, inFile);

          cout << front[0] << endl << back[0] << endl << front[1] << endl << back[1];

          return 0;



          and it worked perfectly for me. It displayed:



          line1
          line2
          line3
          line4


          In order to help you further you should provide the DevDeck.txt file and the piece of code that calls this function.






          share|improve this answer























          • Thank you. It looks like the array sizes were off. //Global constant const int NUMCARDS = 2; /inside main string front[NUMCARDS]; string back[NUMCARDS]; ifstream infile; Works great now. Ty.

            – Cornel
            Mar 8 at 6:10














          1












          1








          1







          Your loop for (int index = 0; index < 4; index++) has the wrong condition since you want 4 strings in total but in each loop you get 2 so right now you would get 8 strings.



          I tried to run your code with that change like this:



          int main()

          string front[2];
          string back[2];

          ifstream inFile;

          initialize(front, back, inFile);

          cout << front[0] << endl << back[0] << endl << front[1] << endl << back[1];

          return 0;



          and it worked perfectly for me. It displayed:



          line1
          line2
          line3
          line4


          In order to help you further you should provide the DevDeck.txt file and the piece of code that calls this function.






          share|improve this answer













          Your loop for (int index = 0; index < 4; index++) has the wrong condition since you want 4 strings in total but in each loop you get 2 so right now you would get 8 strings.



          I tried to run your code with that change like this:



          int main()

          string front[2];
          string back[2];

          ifstream inFile;

          initialize(front, back, inFile);

          cout << front[0] << endl << back[0] << endl << front[1] << endl << back[1];

          return 0;



          and it worked perfectly for me. It displayed:



          line1
          line2
          line3
          line4


          In order to help you further you should provide the DevDeck.txt file and the piece of code that calls this function.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 6:06









          TwoOfDiamondsTwoOfDiamonds

          1619




          1619












          • Thank you. It looks like the array sizes were off. //Global constant const int NUMCARDS = 2; /inside main string front[NUMCARDS]; string back[NUMCARDS]; ifstream infile; Works great now. Ty.

            – Cornel
            Mar 8 at 6:10


















          • Thank you. It looks like the array sizes were off. //Global constant const int NUMCARDS = 2; /inside main string front[NUMCARDS]; string back[NUMCARDS]; ifstream infile; Works great now. Ty.

            – Cornel
            Mar 8 at 6:10

















          Thank you. It looks like the array sizes were off. //Global constant const int NUMCARDS = 2; /inside main string front[NUMCARDS]; string back[NUMCARDS]; ifstream infile; Works great now. Ty.

          – Cornel
          Mar 8 at 6:10






          Thank you. It looks like the array sizes were off. //Global constant const int NUMCARDS = 2; /inside main string front[NUMCARDS]; string back[NUMCARDS]; ifstream infile; Works great now. Ty.

          – Cornel
          Mar 8 at 6:10




















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55057290%2fcreate-2-parallel-string-arrays-from-text-file-using-getline%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

          Алба-Юлія

          Захаров Федір Захарович