From files to vectors and arrays.

This is a discussion on From files to vectors and arrays. within the C++ Programming forums, part of the General Programming Boards category; Am I on the right track. I know there is not much to my code, but am I? assignment-------- CSCI ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    60

    From files to vectors and arrays.

    Am I on the right track. I know there is not much to my code, but am I?

    assignment--------
    CSCI 1010 Programming Assignment 9



    Write a C++ program that reads the following information from an external text file named students.dat, terminating with an end-of-file check, and stores the records in 2 vectors and 1 array. Assume that there are up to 35 students in the file.



    Social Security Number (Student Number) è Integer (store this data in a vector)

    Student Name è-- String (store this data in a vector)

    Grade Point Average è Double (store this data in an array)



    Then display to the monitor (in columns with column headings), Social Security Number, Name, and GPA in reverse order (first one listed is the last element in the array ore vector). At the end, give the total number of students and the average GPA.



    You can input test data with the vi editor and check that the output is correct using the editor. Warning: don’t forget that Name is a string (and hence must use getline). Since a hard return is entered after the socialsecurity number, you will also need the .ignore function before the name is entered.



    Use the file name exactly as stated above.



    Place each piece of information on a separate line in students.dat


    Code:
    Example students.dat
    
    111223333
    John Smith
    3.45
    222334444
    Mary Jones
    3.26
    444558888
    Bill Kats
    2.50
    666778888
    Nan Vulmer
    3.0
    
     
    
    Sample Output
    
    SS Number           Name                         GPA
    666778888      Nan Vulmer                    3.00
    444558888      Bill Kats                          2.50
    222334444      Mary Jones                     3.26
    111223333      John Smith                      3.45
    
     
    
    Total Number of students: 4
    
    Average GPA: 3.05
    my code-------
    Code:
    //Matthew Tyndall
    //Program 9
    //Write a program that reads info from a dat file and stores it in vectors and arrays.
    
    #include<iomanip>
    #include<vector>
    #include<ifstream>
    #include<string>
    
    int main ()
            {//int main open
                    vector <string> name (35);
                    ifstream fileout;
                    vector <int> ssn (35) ;
                    double gpa [35];
                    fileout.open("students.dat");
                            if (filout.fail())
                            {
                            cerr << "Unable to open file." << endl;
                            }
                    for (int i = 35; i<=35; i++)
                    {
                    fileout >> ssn >> name >> gpa;

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> Am I on the right track
    Does your code compile? That would be a good start. Don't add new code until you get the current code compiling.

    >> #include<ifstream>
    Should be <fstream>.

    >> for (int i = 35; i<=35; i++)
    Look at this again. You probably don't want to loop from 35 to 35. Remember that vectors and arrays are 0-based, and that the size is not a valid index (the highest valid index is size - 1).

    >> fileout >> ssn >> name >> gpa;
    You want to read into a single spot in the vectors and array, so you have to access the correct location, probably with operator[]. Also, your instructions suggest getline. Re-read those instructions and try and implement what it suggests.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    better?

    Code:
    #include<iomanip>
    #include<vector>
    #include<ifstream>
    #include<string>
    
    int main ()
            {//int main open
                    vector <string> name (35);
                    ifstream fileout;
                    vector <int> ssn (35) ;
                    double gpa [35];
                    fileout.open("students.dat");
                            if (filout.fail())
                            {
                            cerr << "Unable to open file." << endl;
                            ]
                    for (int i = 1; i<=35; i++)
                    {
                    fileout >> ssn >> endl;
                    cin.ignore (80, '\n');
                    getline (cin, name);
                    fileout >> name >> endl;
                    fileout >> gpa >> endl;
                    }
            }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> better?
    Does your code compile? That would be a good start.

    >> #include<ifstream>
    Should be <fstream>.

    >> for (int i = 1; i<=35; i++)
    Better, but still not correct. Remember that vectors and arrays are 0-based. That means valid indexes are from 0 to size-1.

    >> fileout >> ssn >> endl;
    Worse. You want to read into a single spot in the vectors and array, so you have to access the correct location, probably with operator[]. Also, why is endl in there? Do you know what endl is for?

    >> getline (cin, name);
    >> fileout >> name >> endl;
    You just used cin in the call to getline. Do you know how to use getline? The first argument is the stream. Since you are reading from a file stream, the first argument should be that file stream, not cin. And you don't want to read name twice.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    so far

    Code:
    #include<iomanip>
    #include<vector>
    #include<fstream>
    #include<string>
    
    int main ()
            {//int main open
                    vector <string> name (35);
                    ifstream fileout;
                    vector <int> ssn (35) ;
                    double gpa [35];
                    fileout.open("students.dat");
                            if (filout.fail())
                            {
                            cerr << "Unable to open file." << endl;
                            }
                    for (int i = 0; i<35; i++)
                    {
                    fileout >> ssn[i];
                    cin.ignore (80, '\n');
                    fileout >> name[i];
                    fileout >> gpa[i];
                    }
            }
    Last edited by WinterInChicago; 12-02-2006 at 12:13 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> so far
    Does your code compile? That would be a good start. BTW, I'm not asking this question rhetorically.

    >> #include<fstream>
    Good. Although I just realized you are missing <iostream>.

    >> for (int i = 0; i<35; i++)
    Good.

    >> fileout >> ssn[i];
    Good.

    >> cin.ignore (80, '\n');
    Don't forget which stream you are working on.

    >> fileout >> name[i];
    Oops, your instructions suggest getline.

    You're getting closer. Focus on getting the code to compile and getting these things I mentioned ironed out. Then test the code by running it, even if you don't know whether it read in the data or not, you will still be able to find any crashes or any failure to open the file.

    One other thing that you should look at after you get the stuff above done. The assignment says "Assume that there are up to 35 students in the file." When it says "up to" it probably means that you have to check for a failed read and break out of the loop if the read fails before 35 entries have been read. That is something you can add in later, just don't forget.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    if i use getline, will it still be taking it from the file?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    If you use it correctly, yes. The first parameter of getline is the stream you want to read from. You are used to passing in cin, because that is what you usually read from. In this case, you want to read from the file.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    so am i doing getline(fileout, name[i]);

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Looks good to me. Make sure you test it with the rest of your code as you continue on.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    this so far compiles

    Code:
    //Name
    //Program 9
    //Write a program that reads info from a dat file and stores it in vectors and arrays.
    
    #include<iostream>
    #include<vector>
    #include<fstream>
    #include<string>
    
    using namespace std;
    
    int main ()
            {//int main open
                    vector <string> name (35);
                    ifstream fileout;
                    vector <int> ssn (35) ;
                    double gpa [35];
                    fileout.open("students.dat");
                            if (fileout.fail())
                            {
                            cerr << "Unable to open file." << endl;
                            }
                    for (int i = 0; i<35; i++)
                    {
                    fileout >> ssn[i];
                    cin.ignore (80, '\n');
                    getline(fileout,name[i]);
                    fileout >> gpa[i];
                    }
            }
    not really sure where to go from here...
    Last edited by WinterInChicago; 12-02-2006 at 02:24 PM.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Pick something in the instructions and work on it. The instructions say that the loop terminates on an end-of-file check, but your loop just runs 35 times. So you could work on finding the right place to add the end-of-file check. (BTW, checking for end-of-file is not the best way to go, since a read error will not be noticed, and if you do it incorrectly you might store the last entry twice. However, since the assignment asks specifically for this, you should probably do it that way.)

    The assignment also asks you to output the data in reverse order. You could try to output the data in regular order first, then switch to reverse order after that is working. Then you could move on to formatting the columns correctly. Whatever you feel like working on next is fine.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    i would assume to put the end of file check at the end of the for loop. is that wrong? and im not sure how i store them in the arrays or vectors, or list them. im lost.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> i would assume to put the end of file check at the end of the for loop. is that wrong?
    It really depends on how you do it. I would check it after reading the first piece of data (the ssn). That way, you don't try to finish the loop if you've already reached end-of-file. Many people (including instructors) would test it at the end, but it doesn't always work correctly when you do that. If I explain in detail I will just confuse you. If there is a method the instructor has taught, go ahead and use that.

    >> im not sure how i store them in the arrays or vectors, or list them.
    If I understand you correctly, your code already does this. Your current code reads the file and stores the data in the vectors and array. Each piece of data for a particular student is stored at the same index in the vectors and array, so the first student's name is stored in name[0], the first student's ssn is in ssn[0] and the first student's gpa is in gpa[0]. The tenth student's name is in name[9], ssn at ssn[9] and gpa at gpa[9]. So to output them you'll want a loop and access all the data each time through the loop using the same index so that you get all the data for each student.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    so how do i list them in reverse... does it involve another for loop with an i-- in it

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays vs Vectors
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 02:06 AM
  2. vectors vs c style arrays
    By markucd in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2006, 11:11 AM
  3. byte arrays & vectors
    By kasun in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 08:10 AM
  4. arrays or vectors
    By Geo-Fry in forum C++ Programming
    Replies: 26
    Last Post: 04-17-2003, 07:08 PM
  5. arrays and vectors
    By volk in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2003, 02:45 PM

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