strcmp function in c++ help

Hello, I am having a slight issue with the strcmp function. I am comparing string data from two different files. I want the function to output a set of information if the strings are the same and a different set of data if the strings are different. My issue is, the function outputs the data that's the same but not different.

I had an else statement that compared the data if it was NOT equal but it only duplicated the data in the file.

PLEASE HELP.

Edit: Sorry, I forgot to mention what's in the files. One file is a listing of 100 books with 10 lines of information and an assigned market. The second file is a listing of the markets contained in the books file. However, the books file has a market that is not located in the markets file. The "missing" market is what is not priting or displaying.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// final project2 file

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

using namespace std;
const int ARRAY_SIZE = 500;
const int MARKET_ARRAY = 6;
ifstream infile, infile2;
ofstream outfile;

struct booksData
{
	string item_name, market, item_note;
	string listing_id, seller_sku, open_date, product_id;
	string price;
	string quantity, item_condition;
	string book_header_line;
} books[ARRAY_SIZE];

struct marketsData
{
	string market, name, street_address, city, state, country, market_header_line;
} markets[MARKET_ARRAY];

double tPrice;
int i;
string headings[10];

void readData(ifstream& infile, booksData books[]);
void readMarkets(ifstream& infile2, marketsData markets[]);
void calculateMarketInfo(booksData books[], marketsData markets[], int ARRAY_SIZE);

int main()
{
	infile.open("books.txt");
	outfile.open("report.txt");
	if (!infile)// exception handling if books file is not found
	{
		cout << "File did not open correctly. Please try again";
		system("pause");
		return 0;
	}
	getline(infile, books[i].book_header_line, '\n');
	cout << books[i].book_header_line << endl;
	outfile << "JD's Monthly Book Inventory Report" << endl;

	cout << "Processing Books Data " << endl;

	readData(infile, books);

	infile.close();

	infile2.open("markets.txt");
	if (!infile2)// exception handling if books file is not found
	{
		cout << "File did not open correctly. Please try again";
		system("pause");
		return 0;
	}

	getline(infile2, markets[i].market_header_line, '\n');
	cout << markets[i].market_header_line << endl;
	cout << "Processing Markets Data " << endl;

	readMarkets(infile2, markets);//function to read the markets.txt data

	calculateMarketInfo(books, markets, ARRAY_SIZE);//function to perform calculations on the books and markets data

	outfile.close();//close open files
	system("pause");
	return 1;
}

// function to read data from books,txt file
void readData(ifstream& infile, booksData books[])
{
	int i = 0;
	while (!infile.eof() && i < ARRAY_SIZE)//get file data from the books.txt file
	{
		getline(infile, books[i].item_name, '\t');
		getline(infile, books[i].listing_id, '\t');
		getline(infile, books[i].seller_sku, '\t');
		getline(infile, books[i].price, '\t');
		getline(infile, books[i].quantity, '\t');
		getline(infile, books[i].open_date, '\t');
		getline(infile, books[i].item_note, '\t');
		getline(infile, books[i].item_condition, '\t');
		getline(infile, books[i].product_id, '\t');
		getline(infile, books[i].market, '\n');
		tPrice = atof(books[i].price.c_str());
		i++;
	};
}//end readData function
// function to read data from the markets.txt file

void readMarkets(ifstream& infile2, marketsData markets[])//get market data from the market.txt file
{
	int i = 0;
	while (!infile2.eof() && i < MARKET_ARRAY)
	{
		getline(infile2, markets[i].market, '\t');
		getline(infile2, markets[i].name, '\t');
		getline(infile2, markets[i].street_address, '\t');
		getline(infile2, markets[i].city, '\t');
		getline(infile2, markets[i].state, '\t');
		getline(infile2, markets[i].country, '\n');
		i++;
	};
}//end readMarkets function

//function to perform calculations on the txt files
void calculateMarketInfo(booksData books[], marketsData markets[], int ARRAY_SIZE)
{	//initialize variables
	double totalMarket = 0, marketAverage = 0, totalAllMarkets = 0, marketValue = 0, averageAllMarkets = 0;
	int marketIndex = 0, bookIndex = 0, marketInventory = 0, totalMarketInventoryQuantity = 0;
	int marketInventoryRecords = 0, marketInventoryQuantity = 0, totalMarketInventoryRecords = 0;

	for (marketIndex = 0; marketIndex < MARKET_ARRAY; marketIndex++)//loop thru the market file
	{
		//output data to file
		outfile << endl << markets[marketIndex].market << '\t'
			<< markets[marketIndex].name << '\t'
			<< markets[marketIndex].street_address << '\t'
			<< markets[marketIndex].city << '\t'
			<< markets[marketIndex].state << '\t'
			<< markets[marketIndex].country << endl;
		
		for (bookIndex = 0; bookIndex < ARRAY_SIZE; bookIndex++)//loop thru the books file
		{
			if ((strcmp(books[bookIndex].market.c_str(), markets[marketIndex].market.c_str()) == 0))//compare the market field in both files
			{
				//output data to file
				outfile
					<< books[bookIndex].item_name
					<< setw(100 - books[bookIndex].item_name.size())
					<< books[bookIndex].price
					<< setw(10)
					<< books[bookIndex].quantity << '\t'
					<< books[bookIndex].product_id << endl;
			

				marketInventoryRecords++; // increment market inventory record counter
				marketInventoryQuantity = atoi(books[bookIndex].quantity.c_str()) + marketInventoryQuantity;
				totalMarketInventoryRecords++; // increment market inventory records counter
				totalMarketInventoryQuantity = atoi(books[bookIndex].quantity.c_str()) + totalMarketInventoryQuantity;
				marketValue = atof(books[bookIndex].price.c_str()) * atof(books[bookIndex].quantity.c_str());
				cout << "The Market Value of this is: " << showpoint << setprecision(2) << fixed << marketValue << endl;
				totalAllMarkets += marketValue;
				totalMarket += marketValue;
			}
		}
		outfile << endl << "Market Total's Data. " << markets[marketIndex].market << endl;
		outfile << "Total Market Inventory Records are: " << marketInventoryRecords << endl;
		marketInventoryRecords = 0; //reset inventory records counter
		outfile << "Total Market Inventory Quantity is: " << marketInventoryQuantity << endl;
		marketInventoryQuantity = 0; // reset inventory quantity counter
		cout << "The total market value is: " << totalMarket << endl;
	}
	outfile << endl << "All Market's Data: " << endl;
	outfile << "The Total Book Quantity of all Markets is: " << totalMarketInventoryQuantity << endl;//outputs the total invetory to the file
	outfile << "The Total Book Inventory Records of all Markets is: " << totalMarketInventoryRecords << endl;//outputs the total market inventory records to the file
	outfile << "The Total Market Value of all books is: $" << totalAllMarkets << endl;// ouputs the total value of all markets to the file
	averageAllMarkets = totalAllMarkets / totalMarketInventoryQuantity;
	outfile << "The Average Book Price for all Markets: $" << fixed << showpoint << setprecision(2) << averageAllMarkets << endl;
}
Last edited on
closed account (N36fSL3A)
Can you point out the line where you're having difficulties?
lines 115-142. Sorry and thanks! When the program runs, the output in the file prints "blank lines" for the data but doesn't output the data, if that makes sense.
I think the real problem is in lines 81 and 102. You loop on eof and assume input extraction is successful. By the time eof is encountered, you've very likely already had an input extraction fail, resulting in a "blank" record which you are seeing in your output function.

[edit: Of course, you also don't keep track of how many records are read, so if less than the max were read, that will also be a problem for you.]
Last edited on
You should not even think about using strcmp() as I see you already use std::string.
Use == operator directly. However, this will not magically fix your code.
thanks cire! however, when I edit the "markets" file to include info about the "invalid" market, the additional records print onto the output file.
thank you to all that responded. I figured out the program.
Topic archived. No new replies allowed.