Hey guys, I have recently taken a class in C++ as part of my program for a degree in Computer Science. I was given the task of supplying an array with data through file and file manipulation by my oh-so-lovely lecturer.
My code looks like this:
The program is supposed to search the array for information assigned to it by a loop and some file manipulation.Code://including pre-processor directives #include <iostream> #include <fstream> //make use of the C++ standard libraries using namespace std; int main() { //declaring and initializing variables ifstream bobCat; string code = ""; string firstName[25] = {""}; string lastName[25] = {""}; string catBob[25] = {""}; string ans = ""; //openning text file and attenuating it to object bobCat.open ("Advanced27.txt", ios::in); //affixing the data from the file to the three array indices for (int count = 0; count <= 24; count++){ getline(bobCat, catBob[count], '#'); bobCat >> firstName[count]; bobCat >> lastName[count]; } // if (bobCat.is_open()){ cout << "Welcome to Bobcat.exe. \n\n"; while (code != "Z3"){ cout << "If you would like to begin an employer search " << "please enter: \n\n|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-" << "|-|-|-|-|-|-|-|-|\n" << "| - F1 -> for Full-time employees who sell new cars.| " << "\n| - F2 -> for Full-time employees who sell old cars.|" << "\n| - P1 -> for Part-Time employees who sell new cars.|" << "\n| - P2 -> for Part-Time employees who sell old cars.|" << "\n| - or Z3 -> to end Bobcat.exe |\n" << "|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|\n"; cin >> code; while (code != "F1" && code != "F2" && code != "P1" && code != "P2" && code != "Z3"){ //if (code != "F1" && code != "F2" && code != "P1" && code != "P2" && code != "Z3"){ cout << "ERROR 101: Unsuitable Code Transaction...\n " << "Please try again.\n\n"; cout << "If you would like to begin an employer search " << "please enter: \n\n|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-" << "|-|-|-|-|-|-|-|-|\n" << "| - F1 -> for Full-time employees who sell new cars.| " << "\n| - F2 -> for Full-time employees who sell old cars.|" << "\n| - P1 -> for Part-Time employees who sell new cars.|" << "\n| - P2 -> for Part-Time employees who sell old cars.|" << "\n| - or Z3 -> to end Bobcat.exe |\n" << "|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|\n"; cin >> code; //} //} while (code == "Z3"){ cout << "Are you sure you want to quit BobCat.exe?\n" << "Please type yes to continue or no. \n\n"; cin >> ans; if (ans == "yes"){ cout << "Thank you for using BobCat.exe. Enjoy your day!"; code = "Z3"; break; } else if (ans == "no"){ cout << "If you would like to begin an employer search " << "please enter: \n\n|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-" << "|-|-|-|-|-|-|-|-|\n" << "| - F1 -> for Full-time employees who sell new cars.| " << "\n| - F2 -> for Full-time employees who sell old cars.|" << "\n| - P1 -> for Part-Time employees who sell new cars.|" << "\n| - P2 -> for Part-Time employees who sell old cars.|" << "\n| - or Z3 -> to end Bobcat.exe |\n" << "|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|\n"; cin >> code; } } cout << "Employees of the " << code << " type include:\n"; for (int countUp = 0; countUp <= 13; countUp++){ if (catBob[countUp] == code){ cout << firstName[countUp] << " " << lastName[countUp] << "\n"; } } } } system("PAUSE"); return 0; }
I expect to have the list of corresponding list to be printed when the user inputs a particular code but alas this does not occur.
The file, that my lecturer gave me to use for this particular project, has its information written like this:
F1#John James
P2#Mary Dapper
F2#Mary Jones
P1#Jeff Bride
F2#Joel Adkari
P2#Kristy Jacob
F1#June Joana
F1#Jacob Sueba
P2#Tammy Janes
F1#Charles Smith
P1#Bridget Hines
P2#Jerry Kramer
P1#John Adams
P2#William Smith
P1#Jenny Adair
F1#Connie Gray
F1#Chris Thomas
F1#Carl Angles
P2#Jenny Jonah
F1#Carol Verace
P1#Ann Jerame
P2#Kate Krumpet
F2#Janice Paulo
P1#Suman Cary
P1#Ann Patel
Unfortunately i only get as far as Joel James.
How can i correct my code? Thank you.