4 Replies - 1604 Views - Last Post: 03 March 2010 - 06:19 AM Rate Topic: -----

#1 Guest_Kyle*


Reputation:

io file manipulation

Posted 02 March 2010 - 06:58 PM

I was given the task to do the following:

Implement a function called "LoadDatabase(...)", in which you open the file "studentrecord.txt" (provided in Lab 3) and read the file to populate the first N elements (the file initially contains 10 records, but the contents will be changed by your program --- keep reading) of the array you defined, using the code you developed in Lab 7. Note this function should be called by your main function upon starting.
Your program should then becomes menu-driven. Your menu should look like this.
Program Name/Your Name/Copyright info here
Please choice:
(1) Add a student record
(2) Delete a student record
(3) Find a student's information
(4) Display all information in the database
(5) Exit Program
For each menu item, call a function that implement the menu function. After finishing processing, display the menu again and wait for the next input.
Note:
(0) Your array initially has 10 student records from the file.
(1) When the user chooses option 1 (Add a student record), the program should read the info of a student (Name, ID, GPA) from keyboard to a StudentRec variable, and append it at the end of the array (hint: use a integer variable to track the index of the last record in the array and copy the new record to this location. Initially the index has a value of 10, since elements 0-9 are occupied. However, do not hard-code value 10 as the initial value, as the number of records in the file can be changed. You should count the number of records while reading the file, and use the count as the initial value of the index).
You can continue adding records, but your program should giving a warning and do not accept new entry if the number of records exceeds the array size (hint: how to test this? You don't want adding 90 records manually to see if it works! Instead, change the array size to, say, 12, and you only need to add two records before seeing the warning.)
(2) You do not need to implement option 2 (Delete a student record). Write an empty function that does nothing. It is called a "stud" function since it is only a placeholder. We will implement the function next week.
(3) For option 3 (Find a student's information), the program should asking the user to enter the student's ID. The program then uses the ID as the search key to perform a linear search on the array. If the record is found, display all the info about the student (using code developed in Lab 7). Otherwise display "Student Record Not Found".
(4) For option 4 (Display all information in the database), using the code you developed in Lab 7 to display the entire database.
(5) When user chooses option 5 (exit), the program should call a function called "SaveDatabase(...)", in which you save all the records in the array back to the file "studentrecord.txt", so your newly added records will not be lost --- Next time you starts the program again, the newly added records will automatically be loaded! The program then terminates.

This is what I have done so far. Please help me to finish this program
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <iomanip>

using namespace std;
void display();
void addrecord();
void deleterecord();
void info();
void display();

int main()
{
ifstream infile;
        infile.open("StudentRecords.txt");
        if(!infile) //File could not be opened
            {cout << "Error: The file could not be opened.\n";
                exit(0);
            }
    char StudentRec[100];
    int userinput;

    do{

    cout << "Please choose:\n";
    cout << "(1) Add a student record\n";
    cout << "(2) Delete a student record\n";
    cout << "(3) Find a student's information\n";
    cout << "(4) Display all information in the database\n";
    cout << "(5) Exit Program\n";
    int x=-1;
    cin >> x;

        switch (x)
        {
            case 1: {
                if (x<0)
                {
                cout << "Error in input.";
                exit(0);
                }
                cout << addrecord << endl;
                break;
                    }
            case 2: {
                if (x<0)
                {
                cout << "Error in input.";
                exit(0);
                }
                cout << deleterecord << endl;
                break;
                    }
            case 3: {
                if (x<0)
                {
                cout << "Error in input.";
                exit(0);
                }
                cout << info << endl;
                break;
                    }
            case 4: {
                if (x<0)
                {
                cout << "Error in input.";
                exit(0);
                }
                display();
                break;
                    }
                ;
             default:
                cout << "Improper Input";
                break;
        };

    }
    while(userinput!=5);



    return 0;

}
void display(){
int x;
fstream infile;
        infile.open("StudentRecords.txt");
        if(!infile) //File could not be opened
            {cout << "Error: The file could not be opened.\n";
                exit(0);
            }
infile >> x;

}

This post has been edited by JackOfAllTrades: 03 March 2010 - 05:52 AM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE!!!...[/code]


Is This A Good Question/Topic? 0

Replies To: io file manipulation

#2 ZOMBIE!!!  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 200
  • Joined: 28-October 09

Re: io file manipulation

Posted 02 March 2010 - 07:07 PM

We wont just finish it. Tell us what problems you have and we will try to help.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_taylorc8*


Reputation:

Re: io file manipulation

Posted 02 March 2010 - 08:05 PM

Okay, Mr./Mrs. gues_Kyle
I'm not quite sure what you're doing with this??
Can you pass objects by reference? Such as an ifstream?
void display(){
int x;
fstream infile;
infile.open("StudentRecords.txt");
if(!infile) //File could not be opened
{cout << "Error: The file could not be opened.\n";
exit(0);
}
infile >> x;

}




heres a small example of passing objects by reference, also PLEAse do tell us what problems you're having..

#include <iostream>
#include <fstream>

bool OpenFile(ifstream &); // function prototype


using namespace std; // standard namespace

int main()
{

ifstream inFile; // our ifstream object

//give the function our object as a reference argument
if(!OpenFile(inFile))
return 1; // file did not open, exit the main function

//and now the inFile is opened, or an error message was displayed and the program terminated
return 0;
}

bool OpenFile(ifstream & inFile)
{
inFile.open("thefilewewillworkwith",ios::in);

if(inFile.is_open())
return true;

else
{
cout << "The file did not open.\n";
return false;
}
}


Was This Post Helpful? 0

#4 Guest_Kyle*


Reputation:

Re: io file manipulation

Posted 02 March 2010 - 11:32 PM

Ok so I am confused with how to have the program read the text file. I created the text file "StudentRecords.txt" in the same folder as the Lab8.cpp but i can figure out how to get it to read. And also when the user chooses each option how to get my void statment for options 1-4 to display. If anyone could help that would be great
Was This Post Helpful? 0

#5 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: io file manipulation

Posted 03 March 2010 - 06:19 AM

Read up on some C++ FILE IO.

There are many ways to do this, you might do a quick google search of "software engineering" and "C++ File IO" and "you can't fool me with an american name like Kyle"

Why don't you use function calls to separate your file operations into a different block of code.


fstream inFile("studentrecords.txt");

switch(x)
{
case 1:
AddRecord(inFile); // this function could ask for the details of the info, and then write it into the file.
break;

case 2:
DeleteRecord(inFile); // here you can scan through the file, and find the record you need to delete!
break;

/* Etc.*/

}



It's up to you to Write the functions yourself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1