getting data from an infile

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;
};

int main()
{
product disk, printer, moniter, mouse, keyboard, audio, phone, flash, modem, router;
ifstream infile;

infile.open("input.txt");
infile >> disk.name;
infile >> disk.q1;
infile >> disk.q2;
cout << disk.name << " " << disk.q1 << " " << disk.q2 << endl;

infile.close();

system("PAUSE");
}

with the text file containing:
Disk 40 70 80 95
Printer -30 -20 -10 -50
Moniter 80 -30 20 55
Mouse -40 20 50 30
Keyboard -20 -50 -90 -100
Audio -50 90 130 200
Phone 90 20 -100 -200
Flash 80 -40 20 50
Modem 30 90 -50 -10
Router 85 -95 95 -85

but when i run the program i get this as a result:
" 7409272 2686776'
So, can someone help me fix this?
closed account (zb0S216C)
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.
Last edited on
here is a function i use in one of my programs to get input from text files.. you can edit it to your usage if you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void readnote(int nday)
{
    char b[256];
    char* filename[256];
    cout << "Your note: \n" << endl;
    if (nday==0)*filename=((char*)"sunday.txt");
    if (nday==1)*filename=((char*)"monday.txt");
    if (nday==2)*filename=((char*)"tuesday.txt");
    if (nday==3)*filename=((char*)"wednesday.txt");
    if (nday==4)*filename=((char*)"thursday.txt");
    if (nday==5)*filename=((char*)"friday.txt");
    if (nday==6)*filename=((char*)"saturday.txt");
    ifstream inmonday(*filename);
    while (!inmonday.eof())
    {
        inmonday >> b;
        cout << b;
        if (inmonday.fail()) break;
        if (!inmonday.eof()) cout << " ";
    }
    return;
}
Firstly, you should initialize your product members in the constructor before using them.

----How do i do that exactly?

Secondly, check if the file has actually opened by calling is_open( )

---after putting this in, i got the else statement saying that the file wasn't opened

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.

---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.
closed account (zb0S216C)
----How do i do that exactly?

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...
}


References:
http://www.cplusplus.com/doc/tutorial/classes/ http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
I've got pretty much no idea what you mean about the constructors, or what exactly is the code for the program for it.

I've moved the file into the same directory, it's making progress now but it's still not right.
Currently, the code is

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

struct product
{
string name;
int q1;
int q2;
int q3;
int q4;
};

int main()
{
product products[10];
ifstream infile;

infile.open("input.txt");

if (infile.is_open())
{
while (infile.good())
{cout << (char) infile.get();
infile.close();
}
}
else
cout << "Error opening file";

int i;
for (short i = 0; i < 10; i++)
{
infile >> products[i].name;
infile >> products[i].q1;
infile >> products[i].q2;
infile >> products[i].q3;
infile >> products[i].q4;
}
cout << products[0].name << " " << products[0].q1 << " " << products[0].q2 << endl;

infile.close();

system("PAUSE");
}

and the output is
"D 7147392 232"

So, what should i do now?
closed account (zb0S216C)
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; )
Last edited on
It's working now, thanks a lot.
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?
closed account (zb0S216C)
GodPyro, just so you know, there have been quite a few discussions about the reliability of eof.
Last edited on
@Framework
Really? Do you have any links to the discussion thread? Would be interesting to see.

@pantsbandit
If you have to just display it you could simplify your program in this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Variables
ifstream infile("input.txt");
char line[256];

if (infile.is_open())
{
	while(!infile.eof())
	{
		infile.getline(line, 256);
		cout << line << endl;
	}
}
else
{
	cerr << "Error opening file";
}

// Close file
infile.close();
Topic archived. No new replies allowed.