For my C++ class i have to make this little program that gets its data from a text file, so all i've written so far is
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct product
{
string name;
int q1;
int q2;
int q3;
int q4;
};
Firstly, you should initialize your product members in the constructor before using them. Secondly, check if the file has actually opened by calling is_open( ) ( See: http://www.cplusplus.com/reference/iostream/ifstream/is_open/ ). Thirdly, when reading your file, you should enter a loop. With each pass of the loop, you extract the name and the four numerical values.
This is my thought: The file has failed to open, leaving the members of the target structure uninitialized.
It's actually quite simple. Within you structure, you write the name of your structure followed by a set of parentheses, like this:
1 2 3 4 5 6 7 8
struct product
{
product( ... ) // This is your constructor.
{
// Initialize your members here...
q1 = 0; // Same with the rest of your members.
}
};
---after putting this in, i got the else statement saying that the file wasn't opened
That's a result of one or two things:
1) The filename is correct but not within the same directory of the executable.
2) The file is in the same directory as the executable but the given filename is incorrect.
---So how would that look in my program? I'm not that good at this and i really have no idea how to use the whole infile thing.
Well, if you want to use a loop for this it's best storing your product instances in the form of an array. If you do choose to do it this way, you can use the loop method which goes like this:
1 2 3 4 5 6 7 8
product Products[ 10 ];
// Other code...
for( short Line( 0 ); Line < 10; Line++ )
{
infile >> Products[ Line ].name;
infile >> Products[ Line ].q1;
// And so fourth...
}
You don't need the while loop. The for loop does what you need. It's good that you checked to see if the file was opened successfully with is_open( ).
Here's what you need to do:
1) Remove the entire while loop.
2) Place the entire for loop where the while loop used to be.
3) Place the cout line underneath the last line of the for loop( infile >> products[i].q4; )
From what I can see from your code you have a few logic errors.
Firstly if you do 'infile.close(); in your while loop. You are going to close the file with only being able to retrieve one '.get()' from it. You already close the file at the end of your main function so eliminate this from your while loop.
Secondly, the reason why you are getting that output is that you are only retrieving a single character from the file at a time through the use of '.get()'. Look at this: http://www.cplusplus.com/reference/iostream/istream/get/
Thirdly, do not use
while ( infile.good() ) and rather use while ( !infile.eof() )
The question I want to ask is do you have to store this information, modify it or just display it?
The question I want to ask is do you have to store this information, modify it or just display it?
---Well, i'll have to display it- which kind of leads to me asking if there's anyway to make a table that is properly aligned without having to manually input spaces for each line?