need help with simple program - C++

Discussion in 'Computer Science & Culture' started by wu_weidong, Jul 21, 2002.

Thread Status:
Not open for further replies.
  1. wu_weidong Registered Member

    Messages:
    2
    hi,
    I am trying to finish this assignment that consists of 2 parts. First part
    requires me to write a program that allows the user to enter and store data into a text file named "student.dat". Second part requires me to read in and display the contents of the file "student.dat" in a table format.
    I have written the first part and it was successful. Then I wrote the
    second part but when I tried to display the contents, only the number of students (the variable "number") and the telephone numbers were displayed.
    However, if I were to manually type out the contents of "student.dat",
    everything would run fine. Why is this so?
    I have included the code for both parts below.
    I have another question and that is how do I read the names like this:
    David John Brown. The data would be like this:
    A12345 David John Brown 1234567
    How do I read in the name as one element of an array?
    Thank you!

    Code for the 1st part:
    ------------------------
    #include <fstream.h>
    #include <stdlib.h>
    void createstudent(char *, int);
    void main()
    {
    int number;
    char classnumber[7];
    cout << "Enter class: ";
    cin >> classnumber;
    cout << "Enter number of students: ";
    cin >> number;
    createstudent(classnumber, number);
    }

    void createstudent(char *classnumber, int number)
    {
    ofstream outFile;
    outFile.open("student.dat");
    char name[20], ID[7];
    int telno;
    if (outFile.fail())
    {
    cerr << "Error! File could not be created!";
    exit(1);
    }
    outFile << classnumber << " " << number << endl;
    for (int i = 0 ; i < number ; i++)
    {
    cin.ignore(255, '\n');
    cout << "Enter ID: ";
    cin >> ID;
    outFile << ID << " ";
    cin.ignore(255, '\n');
    cout << "Enter name: ";
    cin.get(name, 20, '\n');
    outFile << name << " ";
    cin.ignore(255, '\n');
    cout << "Enter telephone number: ";
    cin >> telno;
    outFile << telno << endl;
    }
    outFile.close();
    }

    Code for the second part:
    ----------------------------
    #include <fstream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <iomanip.h>
    void displaystudent();
    void main()
    {
    displaystudent();
    }

    void displaystudent()
    {
    ifstream inFile;
    char name[19][20], ID[19][7], classnumber[7];
    int i, telno[19], number;
    inFile.open("student.dat");
    if (inFile.fail())
    {
    cerr << "Error! File could not be opened!";
    exit(1);
    }
    while (!inFile.eof())
    {
    inFile >> classnumber;
    inFile >> number;
    for (i = 0 ; i < number ; i++)
    {
    inFile >> ID;
    inFile >> name;
    inFile >> telno;
    inFile.ignore(255, '\n');
    }
    }
    cout << "Class Name: " << classnumber << endl;
    cout << "#Students: " << number << endl << endl;
    cout << "Student's ID" << setw(6) << "Name" << setw(20) << "Tel No" <<
    endl;
    for (int j = 0 ; j < 40 ; j++)
    cout << "-";
    cout << endl;
    for (i = 0 ; i < number ; i++)
    cout << ID << setw(14) << name << setw(14) << telno << endl;
    inFile.close();
    getch();
    }

    Regards,
    Wei Dong
     
Thread Status:
Not open for further replies.

Share This Page