Trying to count the number of similar items in the text file after writing the text but i am getting an No operator << matches these operands operand type are std::ofstream >> std::string
. Code works but when adding the while loop is where i get the error textfile >> item
. Does it have anything to do with ofstream of the textfile?
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include<string>
using namespace std;
int main()
{
string accord[6];
ofstream textfile;
textfile.open("C:\\temp\\1.txt");
cout << "Enter a 6 cylinder car : " << endl;
for (int x = 0; x < 6; x++) {
getline(cin, accord[x]);
}
for (int x = 0; x < 6; x++) {
textfile << accord[x] << endl;
}
int count = 0;
string item;
while (!textfile.eof()) {
textfile >> item;
if (item == "6") {
count++;
}
}
cout << count << "found!" << endl;
textfile.close();
return 0;
}