How to Read in Double Column Int Text File C++
#one
Reading data from a two-cavalcade text File
Posted 01 August 2011 - 03:41 AM
Howdy,I am stuck in reading my information from a text 2-cavalcade text file.I have opened the file and can read out each column data. Only when I want to utilise the data of a column(e.thou. the first column) for some other calculations, the calculation is merely done for the last 3 numbers of the column, and for each terminal number of the column, the adding is repeated 97 times ( which is the number of numbers in the column). Please see the lines of code:
string Load; ifstream openfile("F:\\Fifty-51.txt"); // Open load profile file while (getline(openfile,Load)) { istringstream is(Load); double P; double Q; is >> P >> Q; // Reads P and Q separately from the file for (int i=ane; i<97; i++) { double Pt; double 1000=0.2101; // constant value Pt = K*P; // This is where my problem is. cout << Pt << endl;
Pt only outputs the last three elements of the cavalcade.
My tab-delimited file is of this format:
0.1790 0.0000
0.1819 0.0000
0.1837 0.0000
0.1839 0.0000
0.1821 0.0000
0.1787 0.0000
0.1741 0.0000
Delight, I demand your suggestions. Thanks in advance.
This mail service has been edited by JackOfAllTrades: 01 August 2011 - 04:38 AM
Reason for edit:: Stock-still lawmaking tags
#ii
Re: Reading data from a 2-cavalcade text File
Posted 01 August 2011 - 03:59 AM
Welcome to the forum, Hislow!
Get-go, you need to always, ALWAYS, click on the [ code] icon in the forum editor, and so paste your code between the provided code tags. Otherwise your code gets squished all to the left, and is very hard to report.
2d, your last char in your program, is a square bracket, and should be a curly caryatid: } non ].
Your compiler should be giving you lot an error on that.
3rd, your logic is off the marking. Call up of the starting time fourth dimension through the loops you have. What 97 values do you have for that commencement value in P?
Y'all need to work this out on paper by hand. Programming is all about solving logic problems similar this, and this problem is only too good to tell you the answer to it.
Don't let anyone rob you lot of the satisfaction of solving this one, Hislow!
#3
Re: Reading data from a two-column text File
Posted 01 Baronial 2011 - 04:12 AM
Thanks Adak for your response. The foursquare brace "]" came in when I tried to insert my code by clicking on the [code].
I would be glad if you could please explain more about my wrong approach? I am actually new to C++ and I am blind to some mistakes . Thanks for your understanding.
This post has been edited by Hislow: 01 August 2011 - 04:12 AM
#4
Re: Reading data from a two-column text File
Posted 01 Baronial 2011 - 04:38 AM
Howdy Adak, thanks once again. Your response made me to call back and realise that I don't fifty-fifty demand a "for" loop. You are a great instructor!!! Now it works! And by the mode; i just started C++ two weeks agone
#5
Re: Reading data from a two-column text File
Posted 01 Baronial 2011 - 04:48 AM
Accept your lawmaking and practice it, on newspaper and pencil, past manus. You'll see the trouble as presently as you take a loop effectually that inner loop.
Y'all don't really empathize the steps needed to solve this problem, and you MUST understand the problem, (better than you would believe!), before you can program a solution to it.
Here'south what I desire you lot to exercise:
ane) With a paper and pencil, work through this trouble with small iv-7 value exam numbers in a test file. Write downward each step you took, to do it, brand each stride VERY specific and small.
Check them, that you have all the steps right. You'll probably skip over some and demand to fill up in some steps, in this cheque.
2) Now have those steps you lot wrote down in #1, and translate them into steps for the computer.
Don't think Language syntax >> reckoner code >> estimator logic >> problem.
Retrieve Trouble >> Agreement the problem >> logic >> computer logic (functions and such) >> estimator code >> linguistic communication syntax.
Get the problem solving juices flowing FROM the problem, through your brain, and out to the computer logic and syntax.
Sitting down at the keyboard and putting together some lawmaking, is something yous do only when y'all already take a good agreement of the steps needed to solve the problem. Otherwise, it can be a disaster to work this way.
#vi
Re: Reading data from a ii-column text File
Posted 02 August 2011 - 12:41 AM
Adak, on 01 Baronial 2011 - 04:48 AM, said:
Take your lawmaking and exercise it, on paper and pencil, past manus. You'll see the problem every bit before long as you take a loop effectually that inner loop.
You don't really empathize the steps needed to solve this trouble, and you MUST sympathize the trouble, (better than you would believe!), before y'all can program a solution to information technology.
Here's what I want you to practice:
1) With a newspaper and pencil, work through this problem with pocket-size 4-seven value test numbers in a test file. Write down each footstep you took, to do it, make each step VERY specific and modest.
Check them, that you take all the steps correct. You'll probably skip over some and need to fill up in some steps, in this cheque.
2) Now take those steps y'all wrote down in #1, and translate them into steps for the calculator.
Don't think Language syntax >> computer code >> computer logic >> problem.
Retrieve Problem >> Understanding the problem >> logic >> estimator logic (functions and such) >> computer code >> language syntax.
Get the problem solving juices flowing FROM the problem, through your brain, and out to the computer logic and syntax.
Sitting down at the keyboard and putting together some code, is something you do merely when you lot already accept a adept understanding of the steps needed to solve the problem. Otherwise, it can be a disaster to work this way.
Thanks Adak, I really appreciate your advice and suggestions.
#7
Re: Reading data from a two-cavalcade text File
Posted 02 August 2011 - 04:41 AM
Your lawmaking really looks fine. And then, trace it:
const double K=0.2101; // abiding value ifstream openfile("F:\\L-51.txt"); // Open up load profile file string Load; while (getline(openfile,Load)) { cout << "Line read: " << Load << endl; istringstream is(Load); double P, Q; is >> P >> Q; cout << "P = " << P << " Q = " << Q << endl; for (int i=1; i<97; i++) { cout << i << " "; double Pt = M*P; // This is where my problem is. cout << Pt << endl;
You lot practise realize, y'all're printing out the same value 96 times?
Likewise, I'd consider moving that processing logic elsewhere:
void processPQ(double P, double Q) { const double One thousand=0.2101; // constant value cout << "P = " << P << " Q = " << Q << endl; for (int i=1; i<97; i++) { cout << i << " "; double Pt = Thou*P; // This is where my problem is. cout << Pt << endl; //... } void processFile(const char *filename) { string Load; ifstream openfile(filename) { while (getline(openfile,Load)) { istringstream is(Load); double P, Q; is >> P >> Q; processPQ(P, Q); } openfile.close(); }
Now y'all can examination your sub process without reading from a file, if you similar.
#8
Re: Reading data from a 2-column text File
Posted 02 August 2011 - 05:14 AM
baavgai, on 02 Baronial 2011 - 04:41 AM, said:
Your code actually looks fine. And then, trace it:
const double Thousand=0.2101; // constant value ifstream openfile("F:\\L-51.txt"); // Open up load profile file cord Load; while (getline(openfile,Load)) { cout << "Line read: " << Load << endl; istringstream is(Load); double P, Q; is >> P >> Q; cout << "P = " << P << " Q = " << Q << endl; for (int i=one; i<97; i++) { cout << i << " "; double Pt = K*P; // This is where my problem is. cout << Pt << endl;
You do realize, y'all're printing out the aforementioned value 96 times?
Also, I'd consider moving that processing logic elsewhere:
void processPQ(double P, double Q) { const double K=0.2101; // constant value cout << "P = " << P << " Q = " << Q << endl; for (int i=1; i<97; i++) { cout << i << " "; double Pt = K*P; // This is where my trouble is. cout << Pt << endl; //... } void processFile(const char *filename) { string Load; ifstream openfile(filename) { while (getline(openfile,Load)) { istringstream is(Load); double P, Q; is >> P >> Q; processPQ(P, Q); } openfile.shut(); }
Now you can test your sub process without reading from a file, if you like.
Thanks baavgai, I accept actually solved the trouble. I am quite a new guy in C++, so I guess I tin can learn some more things from the corrected code from you. Thanks again.
#9
Re: Reading data from a two-cavalcade text File
Posted 02 August 2011 - 08:14 AM
baavgai, on 02 Baronial 2011 - 04:41 AM, said:
Your code actually looks fine. And then, trace it:
const double Grand=0.2101; // constant value ifstream openfile("F:\\L-51.txt"); // Open load profile file cord Load; while (getline(openfile,Load)) { cout << "Line read: " << Load << endl; istringstream is(Load); double P, Q; is >> P >> Q; cout << "P = " << P << " Q = " << Q << endl; for (int i=1; i<97; i++) { cout << i << " "; double Pt = M*P; // This is where my problem is. cout << Pt << endl;
Y'all exercise realize, y'all're printing out the same value 96 times?
Also, I'd consider moving that processing logic elsewhere:
void processPQ(double P, double Q) { const double K=0.2101; // constant value cout << "P = " << P << " Q = " << Q << endl; for (int i=1; i<97; i++) { cout << i << " "; double Pt = Yard*P; // This is where my problem is. cout << Pt << endl; //... } void processFile(const char *filename) { cord Load; ifstream openfile(filename) { while (getline(openfile,Load)) { istringstream is(Load); double P, Q; is >> P >> Q; processPQ(P, Q); } openfile.shut(); }
At present yous can test your sub process without reading from a file, if you similar.
#ten
Re: Reading data from a two-column text File
Posted 02 August 2011 - 08:26 AM
Hello Guys, my code is working fine and I am all smiles with that
I tried going one step further by using the getline() function to simultaneously open a second file ( which is of the aforementioned format with the first file) which I also want to be included in the "while loop". So, I get the following error:
error C2374: 'openfile': redefinition; multiple initialization
I empathize what the mistake mean! Is there a manner to become through this using by the same getline() ? Thanks for your response.
Source: https://www.dreamincode.net/forums/topic/241843-reading-data-from-a-two-column-text-file/
0 Response to "How to Read in Double Column Int Text File C++"
Post a Comment