Placing two 2-d arrays into a text file












0















I'm having trouble getting a code that takes an array input of first name and last name, and another array that stores lab scores.



The rules I have to follow are.




  1. Initialize 2D char arrays by taking input from user, using cin.getline() function instead of cin. This 2D array holds the name record for all five students, where each row corresponds to one student.


  2. Initialize 2D int type array for lab scores using rand() function with not lab score being greater than 10. This 2D array hold score of total 4 labs of all students/


  3. Finallys write the data into a text file in the following format, where the first row should be written as it is:
    Example:



Name1 Lname1 7 6 3 5



Name2 Lname2 10 2 6 7



.



.



Name5 Lname5 6 5 4 3



This is my code so far:



#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
ofstream fout, fin;
char lab_names[5][2];
int lab_grades[5][4];
fout.open("lab_grade.txt",ios::app);
fin.open("lab_grade.txt",ios::app);

for(int i=0; i<4; i++)
{
for(int k=0; k<5; k++)
{
if(lab_names[i][k]==lab_names[0][0])
{
cin.get(lab_names[0][0]);
}
if(lab_names[i][k]==lab_names[0][1])
{
cin.get(lab_names[0][1]);
}
if(lab_names[i][k]==lab_names[1][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[1][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[2][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[2][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[3][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[3][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[4][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[4][1])
{
cin.get(lab_names[5][2]);
}
else
{
lab_grades[i][k]=rand()%10+1;
}

}
cout<<"n";
}
fout<<lab_grades;
fout.close();
return 0;
}


Can someone help me get this sorted out?



When I use getline it causes several problems and doesn't allow the program to even attempt to run.










share|improve this question

























  • Somewhat related: char lab_names[5][2]; can only store 1 letter plus a null terminator per person. If std:: string is available for use consider string lab_names[5][2]; This will hold whole names, allocating space as required. Note that the way the data is stored will give you problems with names like Victor Von Doom, and Von Doom is not the sort of person you should upset by getting his name wrong.

    – user4581301
    Nov 21 '18 at 19:30











  • You are headed in a generally bad direction. Start by reading your way through the file token by token (in this case a token would be anything surrounded by whitespace) and writing it to the console. Once you can correctly read a file you can consider moving on to reading and storing. The >> should help you greatly.

    – user4581301
    Nov 21 '18 at 19:32













  • Opening the same file twice at the same time is not a good idea. Usually the second opener loses and the file fails to open. If you want to read a file and write to the file, 1. open the file for reading. 2. read the file into memory 3. close the file. 4. update the copy of the file in memory. 5. open the file for writing. overwrite the file with the copy in memory. Trying to modify a text file in place reading and writing at the same time sounds more efficient but is orders of magnitude more difficult and that soaks up any savings darn fast.

    – user4581301
    Nov 21 '18 at 19:44











  • You are asked to use getline but you use get

    – Damien
    Nov 21 '18 at 20:01
















0















I'm having trouble getting a code that takes an array input of first name and last name, and another array that stores lab scores.



The rules I have to follow are.




  1. Initialize 2D char arrays by taking input from user, using cin.getline() function instead of cin. This 2D array holds the name record for all five students, where each row corresponds to one student.


  2. Initialize 2D int type array for lab scores using rand() function with not lab score being greater than 10. This 2D array hold score of total 4 labs of all students/


  3. Finallys write the data into a text file in the following format, where the first row should be written as it is:
    Example:



Name1 Lname1 7 6 3 5



Name2 Lname2 10 2 6 7



.



.



Name5 Lname5 6 5 4 3



This is my code so far:



#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
ofstream fout, fin;
char lab_names[5][2];
int lab_grades[5][4];
fout.open("lab_grade.txt",ios::app);
fin.open("lab_grade.txt",ios::app);

for(int i=0; i<4; i++)
{
for(int k=0; k<5; k++)
{
if(lab_names[i][k]==lab_names[0][0])
{
cin.get(lab_names[0][0]);
}
if(lab_names[i][k]==lab_names[0][1])
{
cin.get(lab_names[0][1]);
}
if(lab_names[i][k]==lab_names[1][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[1][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[2][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[2][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[3][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[3][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[4][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[4][1])
{
cin.get(lab_names[5][2]);
}
else
{
lab_grades[i][k]=rand()%10+1;
}

}
cout<<"n";
}
fout<<lab_grades;
fout.close();
return 0;
}


Can someone help me get this sorted out?



When I use getline it causes several problems and doesn't allow the program to even attempt to run.










share|improve this question

























  • Somewhat related: char lab_names[5][2]; can only store 1 letter plus a null terminator per person. If std:: string is available for use consider string lab_names[5][2]; This will hold whole names, allocating space as required. Note that the way the data is stored will give you problems with names like Victor Von Doom, and Von Doom is not the sort of person you should upset by getting his name wrong.

    – user4581301
    Nov 21 '18 at 19:30











  • You are headed in a generally bad direction. Start by reading your way through the file token by token (in this case a token would be anything surrounded by whitespace) and writing it to the console. Once you can correctly read a file you can consider moving on to reading and storing. The >> should help you greatly.

    – user4581301
    Nov 21 '18 at 19:32













  • Opening the same file twice at the same time is not a good idea. Usually the second opener loses and the file fails to open. If you want to read a file and write to the file, 1. open the file for reading. 2. read the file into memory 3. close the file. 4. update the copy of the file in memory. 5. open the file for writing. overwrite the file with the copy in memory. Trying to modify a text file in place reading and writing at the same time sounds more efficient but is orders of magnitude more difficult and that soaks up any savings darn fast.

    – user4581301
    Nov 21 '18 at 19:44











  • You are asked to use getline but you use get

    – Damien
    Nov 21 '18 at 20:01














0












0








0








I'm having trouble getting a code that takes an array input of first name and last name, and another array that stores lab scores.



The rules I have to follow are.




  1. Initialize 2D char arrays by taking input from user, using cin.getline() function instead of cin. This 2D array holds the name record for all five students, where each row corresponds to one student.


  2. Initialize 2D int type array for lab scores using rand() function with not lab score being greater than 10. This 2D array hold score of total 4 labs of all students/


  3. Finallys write the data into a text file in the following format, where the first row should be written as it is:
    Example:



Name1 Lname1 7 6 3 5



Name2 Lname2 10 2 6 7



.



.



Name5 Lname5 6 5 4 3



This is my code so far:



#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
ofstream fout, fin;
char lab_names[5][2];
int lab_grades[5][4];
fout.open("lab_grade.txt",ios::app);
fin.open("lab_grade.txt",ios::app);

for(int i=0; i<4; i++)
{
for(int k=0; k<5; k++)
{
if(lab_names[i][k]==lab_names[0][0])
{
cin.get(lab_names[0][0]);
}
if(lab_names[i][k]==lab_names[0][1])
{
cin.get(lab_names[0][1]);
}
if(lab_names[i][k]==lab_names[1][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[1][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[2][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[2][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[3][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[3][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[4][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[4][1])
{
cin.get(lab_names[5][2]);
}
else
{
lab_grades[i][k]=rand()%10+1;
}

}
cout<<"n";
}
fout<<lab_grades;
fout.close();
return 0;
}


Can someone help me get this sorted out?



When I use getline it causes several problems and doesn't allow the program to even attempt to run.










share|improve this question
















I'm having trouble getting a code that takes an array input of first name and last name, and another array that stores lab scores.



The rules I have to follow are.




  1. Initialize 2D char arrays by taking input from user, using cin.getline() function instead of cin. This 2D array holds the name record for all five students, where each row corresponds to one student.


  2. Initialize 2D int type array for lab scores using rand() function with not lab score being greater than 10. This 2D array hold score of total 4 labs of all students/


  3. Finallys write the data into a text file in the following format, where the first row should be written as it is:
    Example:



Name1 Lname1 7 6 3 5



Name2 Lname2 10 2 6 7



.



.



Name5 Lname5 6 5 4 3



This is my code so far:



#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
ofstream fout, fin;
char lab_names[5][2];
int lab_grades[5][4];
fout.open("lab_grade.txt",ios::app);
fin.open("lab_grade.txt",ios::app);

for(int i=0; i<4; i++)
{
for(int k=0; k<5; k++)
{
if(lab_names[i][k]==lab_names[0][0])
{
cin.get(lab_names[0][0]);
}
if(lab_names[i][k]==lab_names[0][1])
{
cin.get(lab_names[0][1]);
}
if(lab_names[i][k]==lab_names[1][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[1][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[2][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[2][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[3][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[3][1])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[4][0])
{
cin.get(lab_names[5][2]);
}
if(lab_names[i][k]==lab_names[4][1])
{
cin.get(lab_names[5][2]);
}
else
{
lab_grades[i][k]=rand()%10+1;
}

}
cout<<"n";
}
fout<<lab_grades;
fout.close();
return 0;
}


Can someone help me get this sorted out?



When I use getline it causes several problems and doesn't allow the program to even attempt to run.







c++ arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 20:12







drakefire

















asked Nov 21 '18 at 19:19









drakefiredrakefire

11




11













  • Somewhat related: char lab_names[5][2]; can only store 1 letter plus a null terminator per person. If std:: string is available for use consider string lab_names[5][2]; This will hold whole names, allocating space as required. Note that the way the data is stored will give you problems with names like Victor Von Doom, and Von Doom is not the sort of person you should upset by getting his name wrong.

    – user4581301
    Nov 21 '18 at 19:30











  • You are headed in a generally bad direction. Start by reading your way through the file token by token (in this case a token would be anything surrounded by whitespace) and writing it to the console. Once you can correctly read a file you can consider moving on to reading and storing. The >> should help you greatly.

    – user4581301
    Nov 21 '18 at 19:32













  • Opening the same file twice at the same time is not a good idea. Usually the second opener loses and the file fails to open. If you want to read a file and write to the file, 1. open the file for reading. 2. read the file into memory 3. close the file. 4. update the copy of the file in memory. 5. open the file for writing. overwrite the file with the copy in memory. Trying to modify a text file in place reading and writing at the same time sounds more efficient but is orders of magnitude more difficult and that soaks up any savings darn fast.

    – user4581301
    Nov 21 '18 at 19:44











  • You are asked to use getline but you use get

    – Damien
    Nov 21 '18 at 20:01



















  • Somewhat related: char lab_names[5][2]; can only store 1 letter plus a null terminator per person. If std:: string is available for use consider string lab_names[5][2]; This will hold whole names, allocating space as required. Note that the way the data is stored will give you problems with names like Victor Von Doom, and Von Doom is not the sort of person you should upset by getting his name wrong.

    – user4581301
    Nov 21 '18 at 19:30











  • You are headed in a generally bad direction. Start by reading your way through the file token by token (in this case a token would be anything surrounded by whitespace) and writing it to the console. Once you can correctly read a file you can consider moving on to reading and storing. The >> should help you greatly.

    – user4581301
    Nov 21 '18 at 19:32













  • Opening the same file twice at the same time is not a good idea. Usually the second opener loses and the file fails to open. If you want to read a file and write to the file, 1. open the file for reading. 2. read the file into memory 3. close the file. 4. update the copy of the file in memory. 5. open the file for writing. overwrite the file with the copy in memory. Trying to modify a text file in place reading and writing at the same time sounds more efficient but is orders of magnitude more difficult and that soaks up any savings darn fast.

    – user4581301
    Nov 21 '18 at 19:44











  • You are asked to use getline but you use get

    – Damien
    Nov 21 '18 at 20:01

















Somewhat related: char lab_names[5][2]; can only store 1 letter plus a null terminator per person. If std:: string is available for use consider string lab_names[5][2]; This will hold whole names, allocating space as required. Note that the way the data is stored will give you problems with names like Victor Von Doom, and Von Doom is not the sort of person you should upset by getting his name wrong.

– user4581301
Nov 21 '18 at 19:30





Somewhat related: char lab_names[5][2]; can only store 1 letter plus a null terminator per person. If std:: string is available for use consider string lab_names[5][2]; This will hold whole names, allocating space as required. Note that the way the data is stored will give you problems with names like Victor Von Doom, and Von Doom is not the sort of person you should upset by getting his name wrong.

– user4581301
Nov 21 '18 at 19:30













You are headed in a generally bad direction. Start by reading your way through the file token by token (in this case a token would be anything surrounded by whitespace) and writing it to the console. Once you can correctly read a file you can consider moving on to reading and storing. The >> should help you greatly.

– user4581301
Nov 21 '18 at 19:32







You are headed in a generally bad direction. Start by reading your way through the file token by token (in this case a token would be anything surrounded by whitespace) and writing it to the console. Once you can correctly read a file you can consider moving on to reading and storing. The >> should help you greatly.

– user4581301
Nov 21 '18 at 19:32















Opening the same file twice at the same time is not a good idea. Usually the second opener loses and the file fails to open. If you want to read a file and write to the file, 1. open the file for reading. 2. read the file into memory 3. close the file. 4. update the copy of the file in memory. 5. open the file for writing. overwrite the file with the copy in memory. Trying to modify a text file in place reading and writing at the same time sounds more efficient but is orders of magnitude more difficult and that soaks up any savings darn fast.

– user4581301
Nov 21 '18 at 19:44





Opening the same file twice at the same time is not a good idea. Usually the second opener loses and the file fails to open. If you want to read a file and write to the file, 1. open the file for reading. 2. read the file into memory 3. close the file. 4. update the copy of the file in memory. 5. open the file for writing. overwrite the file with the copy in memory. Trying to modify a text file in place reading and writing at the same time sounds more efficient but is orders of magnitude more difficult and that soaks up any savings darn fast.

– user4581301
Nov 21 '18 at 19:44













You are asked to use getline but you use get

– Damien
Nov 21 '18 at 20:01





You are asked to use getline but you use get

– Damien
Nov 21 '18 at 20:01












0






active

oldest

votes











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%2f53419156%2fplacing-two-2-d-arrays-into-a-text-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53419156%2fplacing-two-2-d-arrays-into-a-text-file%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()