Read File into Array

closed account (2hTDizwU)
Hello there!

I have a read/write assignment I'm working on and I'm stuck on the part where the books.cpp reads the file and changes data to write to another file (I haven't gotten to the write part yet). What needs to be done is read the book title, author, and price from a file, and change the price by discounting it by 20%. I'm not going to ask for the output help yet, just the read and change data. There are multiple books in the read file.

We are required to use a header file, plus a two other files. Here is what I have so far. I'm getting weird errors on my infile.getline lines but I have no idea what they mean. It's random jibirish! I don't know how I need to properly write my readWrite function.

the txt file reads like:
Title
Author
Price
Title
Author
Price
etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 //book.h header

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#ifndef BOOK_H
#define BOOK_H

fstream infile;

struct bookTable {
	string title, author;
	double price, newPrice;
};

bookTable Book[100];

void readWrite();

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//book.cpp file

#include "book.h"

void readWrite() {

	infile.open("books.txt");

	if (!infile) {
		cout << "Unable to open Books.txt" << endl;
		return;
	}

	for (int i=0; !infile.eof(); i++) {

		infile.getline >> Book[i].title;
		infile.getline >> Book[i].author;
		infile >> Book[i].price;

		Book[i].newPrice = Book[i].price * .8;
		
	}

	infile.close();
}


1
2
3
4
5
6
7
8
9
10
11
//main.cpp file

#include "book.h"


int main() {
	
	readWrite();

	return 0;
}


Thank you for your help!
Last edited on
closed account (2hTDizwU)
Here are the errors I receive when compiling if you guys want to see them. I get 20 of these but they all essentially say the same thing:

Error 1 error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'overloaded-function' c:\users\blind_000\documents\visual studio 2013\projects\lab02\lab02\book.cpp 14 1 Lab02

Error 2 error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'overloaded-function' c:\users\blind_000\documents\visual studio 2013\projects\lab02\lab02\book.cpp 14 1 Lab02

Error 3 error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,_Ty &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'overloaded-function' c:\users\blind_000\documents\visual studio 2013\projects\lab02\lab02\book.cpp 14 1 Lab02

Error 4 error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'overloaded-function' c:\users\blind_000\documents\visual studio 2013\projects\lab02\lab02\book.cpp 14 1 Lab02

Error 5 error C2784: 'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)' : could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'overloaded-function' c:\users\blind_000\documents\visual studio 2013\projects\lab02\lab02\book.cpp 14 1 Lab02

Last edited on
See anything wrong with lines 16 and 17 of book.cpp?

Do not define global variables in header files. Including those headers in multiple .cpp files means you'll be attempting to supply multiple definitions for the same variables. Also, it's a very bad practice to put using namespace std; in a header file.
closed account (2hTDizwU)
I see. I googled solutions on how to do it and I found that one and someone claimed it worked. I have modified it and it now compiles. Unfortunately my results are a butchered mess.

I will post my code and show you what is in the file and what my results show me:

1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i = 0; !infile.eof(); i++) {

		getline(infile, Book[i].title);
		getline(infile, Book[i].author);
		infile >> Book[i].price;

		Book[i].newPrice = Book[i].price * .8;

		cout << "Title: " << Book[i].title << endl;
		cout << "Author: " << Book[i].author << endl;
		cout << "Price: " << Book[i].newPrice << endl;

	}


File shows (just the first three books as an example):
The Goldfinch
Donna Tartt
6.99
The Husband's Secret
Liane Moriarty
9.99
All the Light We Cannot See
Anthony Doerr
10.99

Results show:
The Goldfinch
Donna Tartt
0
.99
The Husband's Secret
0
iane Moriarty
9.99
0
ll the light we cannot see
Anthony Doerr
0
0.99



....what a mess! :P What's weird is for the infile >> Book[i].price; line, it initially crashed my program, so I changed the arrows to << and then it gave me the results above. This is my first time really trying to write something with arrays and a file, so I'm struggling just a bit.

And I got rid of the book.cpp in the meantime and put the function into the int main function just to test it because it was causing me more headaches! :(

Last edited on
closed account (2hTDizwU)
*bump* Any ideas on the issue in my previous post?
Topic archived. No new replies allowed.