Thread: Fstream derived, Array-driven search.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    Fstream derived, Array-driven search.

    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:

    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;    
    }
    The program is supposed to search the array for information assigned to it by a loop and some file manipulation.

    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.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It appears that your problem is that the cin extraction operator>> is leaving the end of line character in the input buffer which is being extracted by the getline() and placed into the beginning of the string. So instead of having 2 characters you have 3 characters for your code for all but the first entry. Try placing a catBob.ignore() after you extract your last name.

    Jim

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use of std::string objects requires the <string> header. If you can compile without this present then your implementation is bringing the header in when you include another header. You should not rely on this and explicitly include the <string> header.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-19-2008, 03:04 AM
  2. Why Array is derived data type ?
    By forumuser in forum C Programming
    Replies: 2
    Last Post: 10-19-2007, 06:01 AM
  3. array search
    By volguy in forum C Programming
    Replies: 3
    Last Post: 10-23-2006, 08:10 AM
  4. fstream char array input question
    By mcdms in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 10:40 PM
  5. Search an array
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2003, 03:25 PM

Tags for this Thread