file only saves one time data when use in loop












-1















I have made a code in which data is taken from one whole file and only part of it is stored in the other file. But when I put it in loop it does not work has it should and I am having trouble locating the error can someone guide me where I am doing mistake.
Here is the things what my below written code does:
Takes user name from the user and creates the user name file then asks user if he wants to choice how many books from English book shelf he wants to choice.



NOW THE ERROR:

it does not take multiple inputs if user press 2 it only takes 1 input and copies one book no name to user file but does not do it for the next time



I MAY HAVE SOME CLUE MAYBE:

I think it maybe the get-line in using in the code, which may not be working for the second time the loop executes.



I have tried modifying my code in mostly each possible way I could, but I think may be I'm still new to programming field. So my logical thinking may be not so good. That is why I need little guidance only.



#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;
string user;
int main()
{
cout<<"enter your name to create your Libaray file for book recordn";
getline(cin,user);
ofstream outFile(user.c_str(),ios::app); //user named file
string line;
ifstream inFile("English_book_shelf.txt"); //file with books name and
//number(labels)
int count,c;
cout<<"How many books do you want to buy?n";
cin>>c;
for(int j=0;j<c;j++)
{
cout<<"Enter your choices:n";
cin>>count;
while(count)
{
getline(inFile, line);
if (--count == 0)
{
outFile << line <<endl;
break;
}
}
}
}


I want my code to take multiple input from the user and store it in the user file.
Like if he want 3 books, the code should run copy 3 books from the book shelf file and copy it in the user file.
If he want 5 books, the code should run copy 5 books from the book shelf file and copy it in the user file and so own.










share|improve this question

























  • What do you think this line does ? if (--count == 0)

    – Sid S
    Jan 2 at 5:00
















-1















I have made a code in which data is taken from one whole file and only part of it is stored in the other file. But when I put it in loop it does not work has it should and I am having trouble locating the error can someone guide me where I am doing mistake.
Here is the things what my below written code does:
Takes user name from the user and creates the user name file then asks user if he wants to choice how many books from English book shelf he wants to choice.



NOW THE ERROR:

it does not take multiple inputs if user press 2 it only takes 1 input and copies one book no name to user file but does not do it for the next time



I MAY HAVE SOME CLUE MAYBE:

I think it maybe the get-line in using in the code, which may not be working for the second time the loop executes.



I have tried modifying my code in mostly each possible way I could, but I think may be I'm still new to programming field. So my logical thinking may be not so good. That is why I need little guidance only.



#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;
string user;
int main()
{
cout<<"enter your name to create your Libaray file for book recordn";
getline(cin,user);
ofstream outFile(user.c_str(),ios::app); //user named file
string line;
ifstream inFile("English_book_shelf.txt"); //file with books name and
//number(labels)
int count,c;
cout<<"How many books do you want to buy?n";
cin>>c;
for(int j=0;j<c;j++)
{
cout<<"Enter your choices:n";
cin>>count;
while(count)
{
getline(inFile, line);
if (--count == 0)
{
outFile << line <<endl;
break;
}
}
}
}


I want my code to take multiple input from the user and store it in the user file.
Like if he want 3 books, the code should run copy 3 books from the book shelf file and copy it in the user file.
If he want 5 books, the code should run copy 5 books from the book shelf file and copy it in the user file and so own.










share|improve this question

























  • What do you think this line does ? if (--count == 0)

    – Sid S
    Jan 2 at 5:00














-1












-1








-1








I have made a code in which data is taken from one whole file and only part of it is stored in the other file. But when I put it in loop it does not work has it should and I am having trouble locating the error can someone guide me where I am doing mistake.
Here is the things what my below written code does:
Takes user name from the user and creates the user name file then asks user if he wants to choice how many books from English book shelf he wants to choice.



NOW THE ERROR:

it does not take multiple inputs if user press 2 it only takes 1 input and copies one book no name to user file but does not do it for the next time



I MAY HAVE SOME CLUE MAYBE:

I think it maybe the get-line in using in the code, which may not be working for the second time the loop executes.



I have tried modifying my code in mostly each possible way I could, but I think may be I'm still new to programming field. So my logical thinking may be not so good. That is why I need little guidance only.



#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;
string user;
int main()
{
cout<<"enter your name to create your Libaray file for book recordn";
getline(cin,user);
ofstream outFile(user.c_str(),ios::app); //user named file
string line;
ifstream inFile("English_book_shelf.txt"); //file with books name and
//number(labels)
int count,c;
cout<<"How many books do you want to buy?n";
cin>>c;
for(int j=0;j<c;j++)
{
cout<<"Enter your choices:n";
cin>>count;
while(count)
{
getline(inFile, line);
if (--count == 0)
{
outFile << line <<endl;
break;
}
}
}
}


I want my code to take multiple input from the user and store it in the user file.
Like if he want 3 books, the code should run copy 3 books from the book shelf file and copy it in the user file.
If he want 5 books, the code should run copy 5 books from the book shelf file and copy it in the user file and so own.










share|improve this question
















I have made a code in which data is taken from one whole file and only part of it is stored in the other file. But when I put it in loop it does not work has it should and I am having trouble locating the error can someone guide me where I am doing mistake.
Here is the things what my below written code does:
Takes user name from the user and creates the user name file then asks user if he wants to choice how many books from English book shelf he wants to choice.



NOW THE ERROR:

it does not take multiple inputs if user press 2 it only takes 1 input and copies one book no name to user file but does not do it for the next time



I MAY HAVE SOME CLUE MAYBE:

I think it maybe the get-line in using in the code, which may not be working for the second time the loop executes.



I have tried modifying my code in mostly each possible way I could, but I think may be I'm still new to programming field. So my logical thinking may be not so good. That is why I need little guidance only.



#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;
string user;
int main()
{
cout<<"enter your name to create your Libaray file for book recordn";
getline(cin,user);
ofstream outFile(user.c_str(),ios::app); //user named file
string line;
ifstream inFile("English_book_shelf.txt"); //file with books name and
//number(labels)
int count,c;
cout<<"How many books do you want to buy?n";
cin>>c;
for(int j=0;j<c;j++)
{
cout<<"Enter your choices:n";
cin>>count;
while(count)
{
getline(inFile, line);
if (--count == 0)
{
outFile << line <<endl;
break;
}
}
}
}


I want my code to take multiple input from the user and store it in the user file.
Like if he want 3 books, the code should run copy 3 books from the book shelf file and copy it in the user file.
If he want 5 books, the code should run copy 5 books from the book shelf file and copy it in the user file and so own.







c++ copy file-handling






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 7:36









William Miller

1,436317




1,436317










asked Jan 2 at 4:55









Zero001Zero001

14




14













  • What do you think this line does ? if (--count == 0)

    – Sid S
    Jan 2 at 5:00



















  • What do you think this line does ? if (--count == 0)

    – Sid S
    Jan 2 at 5:00

















What do you think this line does ? if (--count == 0)

– Sid S
Jan 2 at 5:00





What do you think this line does ? if (--count == 0)

– Sid S
Jan 2 at 5:00












1 Answer
1






active

oldest

votes


















0














The line



if (--count == 0)


is the culprit. Simplify your code and you won't have to worry about such silly errors.



while(count >= 0)
{
getline(inFile, line);
outFile << line <<endl;
--count;
}





share|improve this answer


























  • sir i tried this method but its not working properly. The code runs ask for the user to choice and copies the highest value number of line the user inputs. For example :cout<<"How many books do you want to buy?n"; cin>>4(loop runs 4 times) and in this four times the highest number the user gives let suppose 2,1,5,3 the file copies 5 lines from 1 to 5

    – Zero001
    Jan 3 at 12:05











  • @zero001, the conditional needs to be count >= 0. Updatd the answer just now.

    – R Sahu
    Jan 3 at 14:13













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%2f54001384%2ffile-only-saves-one-time-data-when-use-in-loop%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









0














The line



if (--count == 0)


is the culprit. Simplify your code and you won't have to worry about such silly errors.



while(count >= 0)
{
getline(inFile, line);
outFile << line <<endl;
--count;
}





share|improve this answer


























  • sir i tried this method but its not working properly. The code runs ask for the user to choice and copies the highest value number of line the user inputs. For example :cout<<"How many books do you want to buy?n"; cin>>4(loop runs 4 times) and in this four times the highest number the user gives let suppose 2,1,5,3 the file copies 5 lines from 1 to 5

    – Zero001
    Jan 3 at 12:05











  • @zero001, the conditional needs to be count >= 0. Updatd the answer just now.

    – R Sahu
    Jan 3 at 14:13


















0














The line



if (--count == 0)


is the culprit. Simplify your code and you won't have to worry about such silly errors.



while(count >= 0)
{
getline(inFile, line);
outFile << line <<endl;
--count;
}





share|improve this answer


























  • sir i tried this method but its not working properly. The code runs ask for the user to choice and copies the highest value number of line the user inputs. For example :cout<<"How many books do you want to buy?n"; cin>>4(loop runs 4 times) and in this four times the highest number the user gives let suppose 2,1,5,3 the file copies 5 lines from 1 to 5

    – Zero001
    Jan 3 at 12:05











  • @zero001, the conditional needs to be count >= 0. Updatd the answer just now.

    – R Sahu
    Jan 3 at 14:13
















0












0








0







The line



if (--count == 0)


is the culprit. Simplify your code and you won't have to worry about such silly errors.



while(count >= 0)
{
getline(inFile, line);
outFile << line <<endl;
--count;
}





share|improve this answer















The line



if (--count == 0)


is the culprit. Simplify your code and you won't have to worry about such silly errors.



while(count >= 0)
{
getline(inFile, line);
outFile << line <<endl;
--count;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 14:14

























answered Jan 2 at 5:12









R SahuR Sahu

168k1293192




168k1293192













  • sir i tried this method but its not working properly. The code runs ask for the user to choice and copies the highest value number of line the user inputs. For example :cout<<"How many books do you want to buy?n"; cin>>4(loop runs 4 times) and in this four times the highest number the user gives let suppose 2,1,5,3 the file copies 5 lines from 1 to 5

    – Zero001
    Jan 3 at 12:05











  • @zero001, the conditional needs to be count >= 0. Updatd the answer just now.

    – R Sahu
    Jan 3 at 14:13





















  • sir i tried this method but its not working properly. The code runs ask for the user to choice and copies the highest value number of line the user inputs. For example :cout<<"How many books do you want to buy?n"; cin>>4(loop runs 4 times) and in this four times the highest number the user gives let suppose 2,1,5,3 the file copies 5 lines from 1 to 5

    – Zero001
    Jan 3 at 12:05











  • @zero001, the conditional needs to be count >= 0. Updatd the answer just now.

    – R Sahu
    Jan 3 at 14:13



















sir i tried this method but its not working properly. The code runs ask for the user to choice and copies the highest value number of line the user inputs. For example :cout<<"How many books do you want to buy?n"; cin>>4(loop runs 4 times) and in this four times the highest number the user gives let suppose 2,1,5,3 the file copies 5 lines from 1 to 5

– Zero001
Jan 3 at 12:05





sir i tried this method but its not working properly. The code runs ask for the user to choice and copies the highest value number of line the user inputs. For example :cout<<"How many books do you want to buy?n"; cin>>4(loop runs 4 times) and in this four times the highest number the user gives let suppose 2,1,5,3 the file copies 5 lines from 1 to 5

– Zero001
Jan 3 at 12:05













@zero001, the conditional needs to be count >= 0. Updatd the answer just now.

– R Sahu
Jan 3 at 14:13







@zero001, the conditional needs to be count >= 0. Updatd the answer just now.

– R Sahu
Jan 3 at 14:13






















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%2f54001384%2ffile-only-saves-one-time-data-when-use-in-loop%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

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]