Thread: Problem With search a file and printing to screen

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    4

    Problem With search a file and printing to screen

    Hello,
    First time posting

    I have a problem with reading a file and printing it to the screen
    the .dat file is as follows:

    1 will smith 23_jabrone_drive NY
    2 john doe 12_mount_ave NY
    3 debbie renolds 33_old_farm_rd NY

    What I want to do is be able to search the file and print out the information on the screen. The problem is, is that if I press in id 1 it will print out the string. However if I press 2 it will not print the record it will print just the number 2. Can some one please help me?
    Here is the code i tried..

    int StudentID() // Function : To show people ID
    {

    ifstream acctFile ("Accounts1.dat"); // This is the accounts database file
    AccountRec acctRec;

    int rec;

    cout<< "Please enter students Private ID Number: "; //This gets retrieves the rec #.
    cin >> rec;
    acctFile >> rec >> acctRec.firstName
    >> acctRec.lastName >> acctRec.address >>acctRec.state;

    cout<<endl;
    cout <<" Record Number: " << rec << endl;
    cout <<" First Name: " << acctRec.firstName << endl;
    cout <<" Last Name: " << acctRec.lastName<< endl;
    cout <<" Home Street: " << acctRec.address << endl;
    cout <<" State: " << acctRec.state << endl;


    }

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    acctFile >> rec >> acctRec.firstName
    >> acctRec.lastName >> acctRec.address >>acctRec.state;
    You want to wrap that line with a loop which will loop as many times as the value of "rec".So if you user enter 2, the program reads the first record, and then the second record (overwriting the first one), go out of the loop and prints the last record read.
    It's not the best solution at all since I don't know your coding level.
    Welcome to C Board and please use code tags
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    Ok here is what I came up wtih but still the same problem.. Can you help me with the code which you explained I have a hard time understandig how to implement the code to read the complete line of one rec.

    Ok here is what I cam up with but it still wont work correctly. I am close but cant figure it out.

    <code>
    int StudentID() // Function : To show people ID
    {

    ifstream acctFile ("Accounts1.dat"); // This is the accounts database file
    AccountRec acctRec;

    int rec;

    cout<< "Please enter students Private ID Number: "; //This gets retrieves the rec #.
    cin >> rec;


    acctFile.seekg(rec);//This is jsut added but same problem still arrises

    acctFile >> rec >> acctRec.socID >> acctRec.firstName
    >> acctRec.lastName >> acctRec.phone >> acctRec.address
    >> acctRec.town >>acctRec.state;

    cout<<endl;
    cout <<" Record Number: " << rec << endl;
    cout <<" First Name: " << acctRec.firstName << endl;
    cout <<" Last Name: " << acctRec.lastName<< endl;
    cout <<" Home Street: " << acctRec.address << endl;
    cout <<" State: " << acctRec.state << endl;

    }

    </code>

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    do you call the function StudentID() repeatedly from main. If so then you need to add these before start reading again
    Code:
    is.clear();    // it's necessary if you previously read EOF
    is.seekg (0, ios::beg);
    Text file you're reading from is not in binary form most likely so you can't just jump to the offset based on user input.
    So just read from beginning of file everytime and loop (see my previous post)
    Last edited by alphaoide; 05-01-2004 at 11:49 AM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    Ok I was able to solve the problem with the number being printed out at the end of the list of results. BUt now I ran into another problem. Look at the code before to see what I am doing since Im not the best explainer. Well when I ran the first while loop I get the results I wanted however when the other while loop tires to execute to get the results I have to hit a number an enter the number of times that that rec number is..Almost like i am going through spaces. Forinstance i want record 3 so I enter three and hit enter..the first while loop executes and i get the results of name first last name and ex.. however to get the result for the second while loop I must hit a number and enter 3 times to get the rsults. Is there a way I can reset the values of spaces after teh first while loop so that it will automatically take in the record number which i entered in the beginning of the program and run through both loops..Also I have tried putting the second while loop in the first one but i get the same result and problem? Here is teh code I hope the code helps makes sence of my problem..Thank you to all.

    Code:
    void FinancialRec(char rec)      //Reading and Printing Out Financial Records
    {
      ifstream finaFile ("FinancialRec.dat");  //Input file
      ifstream acctFile ("Accounts1.dat");   // This is the accounts database file
      ifstream newFile ("Accounts2.dat");
    
      char z;
      const float Id1 = 5555;   //Password
      float record;
      char line[100];
    
     cout << "Welcome Please Enter Access Password" <<endl;
     cout << "Password: ";
     cin  >> record;
     cout << endl;
     if(record == Id1 )  //If password is correct continue
       {
         cout<< "Please enter students Private ID Number: ";   //This gets retrieves the rec #.
         cin >> rec;
    
    while(!acctFile.eof())
    {
         acctFile.getline(line,200);
         if(line[0]==rec)
         {
    
                acctFile.seekg((acctFile.tellg()-strlen(line)),ios::beg); //seek to the begginning of the line
                acctFile.getline(line,200,' '); //get everything up to the space
                cout << "Social Security #: " << line << endl;
                acctFile.getline(line,200,' ');
                cout << "First Name: " << line << endl;
                acctFile.getline(line,200,' '); //get everything up to the space
                cout << "Last Name: " << line << endl;
                acctFile.getline(line,200,' ');
                cout << "Phone Number: " << line << endl;
                acctFile.getline(line,200,' '); //get everything up to the space
                cout << "Home Street: " << line << endl;
                acctFile.getline(line,200,' ');
                cout << "Home Town: " << line << endl;
                acctFile.getline(line,200); //get everything up to the space
                cout << "State: " << line << endl;
    		}
    }
    
     while(!finaFile.eof())
    {
         finaFile.getline(line,100);
         if(line[0]==rec)
         {
    
                finaFile.seekg((finaFile.tellg()-strlen(line)),ios::beg); //seek to the begginning of the line
                finaFile.getline(line,100,' '); //get everything up to the space
                cout << "Semester and Year: " << line << endl;
                finaFile.getline(line,100,' ');
                cout << "School's Total Tuition: " << line << endl;
                finaFile.getline(line,100,' '); //get everything up to the space
                cout << "Grant/Scholarship Money: " << line << endl;
                finaFile.getline(line,100,' ');
                cout << "School's Tech Fee: " << line << endl;
                finaFile.getline(line,100,' '); //get everything up to the space
                cout << "Payments Recieved/Paid: " << line << endl;
                finaFile.getline(line,100);
                cout << "Remaining Payments: " << line << endl;
    
         }
    cin >>z;
    }
    
    }
      else
       {
    
       cout << "Sorry Wrong ID Number" <<endl;
       cin >> z;
    
       }
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-06-2008, 02:43 PM
  2. Homework HELP - Problem Printing to file
    By brentshreve in forum C Programming
    Replies: 5
    Last Post: 04-23-2005, 02:11 AM
  3. Printing out contents of a file to the screen
    By Simon in forum C Programming
    Replies: 18
    Last Post: 10-21-2002, 08:05 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Printing to a file problem
    By neva4getme in forum C Programming
    Replies: 1
    Last Post: 10-11-2001, 10:39 AM