writting data from structure in different function

This is a discussion on writting data from structure in different function within the C++ Programming forums, part of the General Programming Boards category; Code: data.cpp: In function `int WriteDatabase()': data.cpp:58: `PersonInfo' undeclared (first use this function) data.cpp:58: (Each undeclared identifier is reported only ...

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    writting data from structure in different function

    Code:
    data.cpp: In function `int WriteDatabase()':
    data.cpp:58: `PersonInfo' undeclared (first use this function)
    data.cpp:58: (Each undeclared identifier is reported only once
    data.cpp:58: for each function it appears in.)
    Those are the errors I am getting from this:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int FillDatabase ( void );
    int WriteDatabase ( );
    
    struct Database
    {
    	string FirstName, LastName;
    	int Age;
    	string Marital;
    	string Occupation;
    };
    
    int main ( void )
    {  
    	FillDatabase();
    	WriteDatabase();
      	return 0;
    }
    
    int FillDatabase ( void )
    {
    	Database Person; 
    	Database * PersonInfo;
    	PersonInfo = &Person;
    
    	cout<< "Name: ";
    	cin>> Person.FirstName >> Person.LastName;
    
    	cout<< "Age: ";
    	cin>> Person.Age;
    
    	cout<< "Marital Status: ";
    	cin>> Person.Marital;
    
    	cout<< "Occupation: ";
    	cin>> Person.Occupation;
    
    	return 0;
    }
    
    int WriteDatabase ( )
    {
    	fstream Log;
    	Log.open("Data.txt", ios::app);
    	
    	if (Log == NULL)
    	{
    		cout <<"Open Error" <<endl;
    		return 1;
    	}
    
    	Log <<"Name:" <<endl
    	    <<PersonInfo->FirstName <<" " <<PersonInfo->LastName <<endl
    	    <<"Age:" <<endl
    	    <<PersonInfo->Age <<endl
    	    <<"Marital Status:" <<endl
    	    <<PersonInfo->Marital <<endl
    	    <<"Occupation:" <<endl
    	    <<PersonInfo->Occupation <<endl  <<endl;
    	
    	Log.close();
    	return 0;
    }
    What am I missing?
    I am trying to modularize this thing as much as possible, and while it's still in the stage of taking shape, I can't seem to figure out how to write the data from the structure in it's own function.
    The world is waiting. I must leave you now.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    PersonInfo's scope is in FillDatabase, it doesn't exist in WriteDatabase. Either increase it's scope (make it global, ick), or pass it as a parameter to WriteDatabase.

    Code:
    // not tested, just quickly hacked up, likely not to work..
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    struct Database
    {
    string FirstName, LastName;
    int Age;
    string Marital;
    string Occupation;
    };
    
    int FillDatabase ( Database* PersonInfo );
    int WriteDatabase ( Database* PersonInfo);
    
    int main ( void )
    { 
    Database PersonInfo;
    FillDatabase(&PersonInfo);
    WriteDatabase(&PersonInfo);
      return 0;
    }
    
    int FillDatabase ( Database* PersonInfo)
    {
    cout<< "Name: ";
    cin>> PersonInfo->FirstName >> PersonInfo->LastName;
    
    cout<< "Age: ";
    cin>> PersonInfo->Age;
    
    cout<< "Marital Status: ";
    cin>> PersonInfo->Marital;
    
    cout<< "Occupation: ";
    cin>> PersonInfo->Occupation;
    
    return 0;
    }
    
    int WriteDatabase (Database* PersonInfo)
    {
    fstream Log;
    Log.open("Data.txt", ios::app);
    
    if (Log == NULL)
    {
    cout <<"Open Error" <<endl;
    return 1;
    }
    
    Log <<"Name:" <<endl
        <<PersonInfo->FirstName <<" " <<PersonInfo->LastName <<endl
        <<"Age:" <<endl
        <<PersonInfo->Age <<endl
        <<"Marital Status:" <<endl
        <<PersonInfo->Marital <<endl
        <<"Occupation:" <<endl
        <<PersonInfo->Occupation <<endl  <<endl;
    
    Log.close();
    return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    Thumbs up

    See, I have pm'ed, instant messaged, emailed, and talked with skilled friends about this problem.
    They told me global or scope increase.
    I couldn't find anything about this.

    I'll just shape it around a bit, start to add in the error checking I planned on after this problem was solved, and I'm set.

    Thanks a ton SilentStrike.
    (your code did work btw)
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Passing structure data to a function?
    By diddy02 in forum C Programming
    Replies: 8
    Last Post: 08-17-2002, 04:15 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21