Thread: Sequential Records

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    Question Sequential Records

    in our c++ clas we are writing a program that stores files in a sequential records. our teacher isnt the brightest and he doesn't help us. ive tried for about 2 weeks to figure it out, some please help.

    Code:
    //Wirte a program that writes a users name, address, and phone to a file.
    //Then display a menu for the user to choose to add a record, veiw the record, or end.
    
    #include<iostream.h>
    #include<fstream.h>
    #include<stdlib.h>
    
    	
    
    main()
    {
    	system("cls");
    	int choice = 0;
    	char name[26] = "                        ";
    	char address[30] = "                             ";
    	char phone[12] = "           ";
    	int A = 0;
    
    	
    	do
    	{
    	cout << "Main Menu" << '\n';
    	cout << "1. Add Record " << '\n' <<"2. Display File " << '\n' << "3. End " << '\n' << "Enter choice: ";
    	cin >> choice;
    	cout << '\n';
    	system("cls");
    	
    
    	if(choice == 1)
    	{
    		ofstream outfile;	
    		outfile.open("User.DAT", ios::out);
    	
    		do
    		{
    			cout << "Enter your name: ";
    			cin >> name;
    			cin.ignore(80, '\n');
    		
    			cout <<"Enter your address: ";
    			cin.get(address, 30);
    			cin.ignore(80, '\n');
    	
    			cout << "Enter your phone number: ";
    			cin.get(phone, 12);
    			cin.ignore(80, '\n');
    	
    			outfile << name;
    			outfile << address;	
    			outfile << phone;
    			
    			cout << '\n' << "Press 1 to add another file and 2 to exit to main menu: ";
    			cin >> A;
    		}while(A == 1);
    		
    		outfile.close();
    		
    	}
    
    	
    	ifstream infile;
    	if(choice == 2)
    	{
    	infile.open("User.DAT",ios::in);
    	infile >> name;
    	infile >> address;
    	infile >> phone;
    		do
    		{
    			cout << "Name: " << name << endl;
    			cout << "Address: " << address << endl;
    			cout << "Phone: " << phone << endl;
    		
    		}while(!infile.eof());
    	
    		infile.close();	
    	}		
    	cout << '\n' << '\n';
    	}
    	
    	while(choice !=3);
    
    	return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    As much fun as bug hunting when you don't know anything about the problem is, can you be more specific about what you are having trouble with? A better description of your "sequential records" would help as well. I can think of a good number of completely different definitions and implementations for "sequential records".
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    we are tring to have a user input a series of information and have it right to a file, then show that file and the records in it. I can not get the records to display the correct way. They are bundled together when i display them.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Is this what you are trying to do?
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    struct record {
      string name;
      string address;
      string phone;
    };
    
    bool interactive_fill_rec ( record& rec )
    {
      /* No input validation done */
      cout<<"Name: ";
      if ( !getline ( cin, rec.name ) )
        return false;
      cout<<"Address: ";
      if ( !getline ( cin, rec.address ) )
        return false;
      cout<<"Phone number: ";
      if ( !getline ( cin, rec.phone ) )
        return false;
    
      return true;
    }
    
    bool fill_rec ( record& rec, istream& in )
    {
      getline ( in, rec.name );
      getline ( in, rec.address );
      getline ( in, rec.phone );
      if ( !in )
        return false;
      else
        return true;
    }
    
    void write_rec ( record& rec, ostream& out )
    {
      out<< rec.name <<"\n"
         << rec.address <<"\n"
         << rec.phone <<"\n"<<endl;
    }
    
    int main()
    {
      record rec;
      ofstream out ( "test.txt" );
    
      while ( interactive_fill_rec ( rec ) )
        write_rec ( rec, out );
      out.close();
      ifstream in ( "test.txt" );
      while ( fill_rec ( rec, in ) )
        write_rec ( rec, cout );
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. reading a file into a block
    By mickey0 in forum C++ Programming
    Replies: 19
    Last Post: 05-03-2008, 05:53 AM
  4. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 11:41 AM
  5. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM