A bit different from my last post.
This assignment/program is suppose to read an external txt file (which looks like):
Book Title
Author
Price
Book Title
Author
Price
Etc...
change the price by giving it a 20% discount, and then write the information to a different txt file. We are suppose to use a header file, main file, and body file for some reason (practice, I guess). My program compiled without errors, but when I try to launch it, it gives a a bunch of errors stating:
Error 1 error LNK2005: "int bookCounter" (?bookCounter@@3HA) already defined in book.obj C:\Users\blind_000\documents\visual studio 2013\Projects\Lab02\Lab02\main.obj Lab02
Error 2 error LNK2005: "class std::basic_ifstream<char,struct std::char_traits<char> > infile" (?infile@@3V?$basic_ifstream@DU?$char_traits@D@std@@@std@@A) already defined in book.obj C:\Users\blind_000\documents\visual studio 2013\Projects\Lab02\Lab02\main.obj Lab02
and about 5 more like that. I have no idea what they mean. Any help would be appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
//Header
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#ifndef BOOK_H
#define BOOK_H
ifstream infile;
ofstream outfile;
int bookCounter;
struct bookTable {
string title, author;
double price, newPrice;
};
void readCalc();
#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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
//book.cpp body
#include "book.h"
void readCalc() {
bookTable Book[100];
bookCounter = 0;
infile.open("books.txt");
if (!infile) {
cout << "Unable to open Books.txt" << endl;
return;
}
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;
bookCounter++;
}
infile.close();
outfile.open("sales.txt");
for (int i = 0; i <= bookCounter; i++) {
outfile << Book[i].title << endl;
outfile << Book[i].author << endl;
outfile << Book[i].newPrice << endl;
}
outfile.close();
}
|
1 2 3 4 5 6 7 8 9 10 11 12
|
//main.cpp file
#include "book.h"
int main() {
readCalc();
cout << "Sales.txt has been created." << endl;
return 0;
}
|