-1

Hey I am trying to figure out how to access an a object out of the scope of the block. I defined Person personData in the For loop which writes data to the file. And after the loop I wanted to access the object again to update the values on the file but its giving me the error - personData is undefined identifier.

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include "Person.h"

using namespace std;

int main() {

    string lastName;
    string firstName;
    int age;

    //file output and creation
    ofstream outPerson("nameage.dat", ios::out | ios::binary);

    Person randPerson;

    randPerson.setLastName("unassigned");
    randPerson.setFirstName("");
    randPerson.setAge(0);
    randPerson.setId(0);

    //for loop to initialize the file with 100 records that store values lastName and firstName
    for (int i = 0; i < 100; i++) {
        outPerson.write(reinterpret_cast<const char*>(&randPerson), sizeof(Person));  //use write to output to file 
    }

    cout << "File Created" << endl;

    //file input and termination 
    ifstream inPerson("nameage.dat", ios::in | ios::out | ios::binary);

    //loops through 10 times to input 10 values (RECORD VALUES) 
    for (int j = 0; j < 2; j++) {
        int id = 0;
        do {
            cout << "Enter a valid id number: (1-100)" << endl;
            cin >> id;
        } while ((id<1)||(id>100)); //breaks do-while after it receives a valid input 


        Person personData;

        inPerson.seekg((id - 1) * sizeof(Person));
        inPerson.read(reinterpret_cast<char*>(&personData), sizeof(Person));

        //checks to see if there is already data in that area, if not then proceed to record data onto file 
        if (personData.getId() == 0) {
            cout << "Enter lastname: ";
            cin >> lastName;
            cout << "Enter firstname: ";
            cin >> firstName;
            cout << "Enter age: ";
            cin >> age;

            //sets data for the particular object 
            personData.setLastName(lastName);
            personData.setFirstName(firstName);
            personData.setAge(age);
            personData.setId(id);

            //seek position in file of user-specified record 
            outPerson.seekp((personData.getId() - 1) * sizeof(Person));

            //write user-specified information in file 
            outPerson.write(reinterpret_cast<const char*>(&personData), sizeof(Person));

            cout << "Record inserted" << endl;
        }
        else {
            cout << "There is already data there. Try another ID number" << endl;
        }//end if
    }//end for loop


    int idSearch;

    do {
        cout << "Enter a ID number: " << endl;
        cin >> idSearch;
    } while ((idSearch < 1) || (idSearch > 100));

    if (personData.getId() != 0) {
        cout << "Enter new Last name";
        cin >> lastName;
        cout << "Enter new first name";
        cin >> firstName;
        cout << "Enter age";
        cin >> age;

        //sets data for the particular object 
        personData.setLastName(lastName);
        personData.setFirstName(firstName);
        personData.setAge(age);
        personData.setId(idSearch);

        //seek position in file of user-specified record 
        outPerson.seekp((personData.getId() - 1) * sizeof(Person));

        //write user-specified information in file 
        outPerson.write(reinterpret_cast<const char*>(&personData), sizeof(Person));

        cout << "Record updated" << endl;
    }

    inPerson.read(reinterpret_cast<char*>(&personData), sizeof(Person));





    system("pause");
    return 0;
}

I'm assuming the problem is because the object cant be accessed when out of scope. So how would I go about accessing the object from the statement below the for loop. Thank you.

Jared
  • 11
  • 3

1 Answers1

2

It's one of the core ideas of C++ data model: data is deleted as soon, as it leaves the scope.

For this to work, you'd need to change the scope of personData (for example, move variable definition outside of the loop).

But be cautious using something like that. In the very best case personData would store data left by the last iteration of the loop.

John Cvelth
  • 522
  • 1
  • 6
  • 19