I am a c++ rookie and do not know what I am doing wrong. My assignment is to compare two different .txt files, each containing an item along with either the number of items and the price of that item. I am then trying to print the names and prices of the items. Lets say I am using the .txt file namesAndQuantity.txt which includes:
3 books
4 pens
And a .txt file namesAndPrice.txt which includes:
pens 3.45
books 19.55
The code I am using only prints out the first match:
#include <iostream>
#include <fstream>
#include <cmath>
int main(){
string nameOfItemP, nameOfItemQ;
double priceOfItem;
int numberOfItems;
ifstream inData;
ifstream inData2;
inData.open("namesAndQuantity.txt");
inData2.open("namesAndPrice.txt");
while (inData>>numberOfItems>>nameOfItemQ){
while (inData2>>nameOfItemP>>priceOfItem){
if (nameOfItemP==nameOfItemQ){
cout<<nameOfItemQ<<endl;
cout<<priceOfItem;
}
}
}
This code only prints out the first line:
books
19.55
What can I do to better it?