Question: So i have written this piece of code: #include ourvector.h #include #include #include #include using namespace std; struct RatingsTiny { string name; ourvector ratings; };
So i have written this piece of code:
#include "ourvector.h" #include #include #include #include
using namespace std;
struct RatingsTiny { string name; ourvector ratings; };
// Opening the file "ratingsTiny.txt" and pushing data into appropriate vector int openRatingsTiny(ourvector &loadVec1, string filename2) { ifstream inFS; string str; int val; inFS.open(filename2); if (!inFS.is_open()) { cout << "Could not open file myfile.txt." << endl; return 1; } while (getline(inFS, str)) { RatingsTiny myObj; myObj.name = str; getline(inFS, str); istringstream ss(str); // storing data one by one in the ratings vector for (int i = 0; i < 9; i++) { ss >> val; myObj.ratings.push_back(val); } loadVec1.push_back(myObj); } return 0; }
// Opening the file "podcastsTiny.txt" int openPodcastsTiny(ourvector &ourVec, string filename2) { ifstream inFS; string str; inFS.open(filename2); if (!inFS.is_open()) { cout << "Could not open file myfile.txt." << endl; return 1; } while (getline(inFS, str)) { ourVec.push_back(str); } return 0; }
// This function takes care of the load command from the user input void loadCommand(string filename1, string filename2, ourvector &loadVector) { string name; int number; cout << endl; cout << "Reading items file..." << endl; ourvector loadVec; openPodcastsTiny(loadVec, filename1); // Printing the content of the podcastsTiny file for (auto UI:loadVec) { cout << UI << endl; } cout << endl; cout << "Reading ratings file..." << endl; openRatingsTiny(loadVector, filename2); // Printing the content of ratingsTiny file for (int i = 0; i < loadVector.size(); i++) { name = loadVector[i].name; cout << name << endl; for (int j = 0; j < loadVector[i].ratings.size(); j++) { number = loadVector[i].ratings[j]; cout << number << " "; } cout < // This function is to give user the ending message void endingMessage() { cout << endl; cout << "-----------------------------" << endl; cout << endl; cout << endl; cout << "Thank you for using the Recommendations app!" << endl; }
void loginName(ourvector logVec) { string username; cin >> username; //ourvector logVec; //openRatingsTiny(logVec); for (int i = 0; i < logVec.size(); ++i) { if (logVec[i].name == username) { cout << endl; cout << "-----------------------------" << endl; cout << endl; cout << "Success." << endl; cout << "Logged in as: " << username << endl; return; } else { cout << endl; cout << "-----------------------------" << endl; cout << endl; cout << "User not found, please try again." << endl; cout << "Logged in as: N/A" << endl; return; } } }
int main() { string command; ourvector V; cout << "To start the app, load the data." << endl; cout << "Type \"load itemsFile ratingsFile\" and press enter." << endl; cout << endl; cout << "Enter command or # to quit: "; cin >> command; while (command != "#") { if (command == "load") { string filename1; string filename2; cin >> filename1; cin >> filename2; loadCommand(filename1, filename2, V); cout << endl; cout << "Welcome to the Recommendations App!" << endl; } cout << "Enter command or # to quit: "; cin >> command; if (command == "login") { loginName(V); } cout << "Enter command or # to quit: "; cin >> command; } endingMessage(); return 0; }
And the file is:
podcastsTiny.txt:
S-Town Radiolab Serial Reply All This American Life The Daily Stuff You Should Know Armchair Expert Planet Money
ratingsTiny.txt:
Jesse -3 5 -3 0 -1 -1 0 0 5 Shakea 5 0 5 0 5 0 0 0 1 Batool 5 -5 0 0 0 0 0 -3 -5 Muhammad 0 0 0 -5 0 -3 0 0 0 Maria 5 0 5 0 0 0 0 1 0 Alex 5 0 0 5 0 5 5 1 0 Riley -5 3 -5 0 -1 0 0 0 3
Question:
What I want to do is that when the user press login it will then ask for a username and if that username exists in the vector in which I have put my data from ratingsTiny.txt, it will print out whatever I have done in my loginName() function? So, basically, i have to search in the vector but i don't know why I am getting wrong. Can you please edit my code?
I appreciate your help.