Thread: From files to vectors and arrays.

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> does it involve another for loop with an i-- in it
    That sounds like it could work.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    alright, i did some more coding and changed the for loop to a while loop:

    to store in the vectors and array:
    Code:
    int i=0;
    while (!fileout.eof())
    {
    fileout >> ssn[i];
    fileout.ignore(80,'\n');
    getline (fileout,name[i]);
    fileout >> gpa[i];
    i++;
    fileout >> ws;
    }
    to list them backwards: (excluding the setw, will add later)
    will the set precision below only effect the gpa?
    Code:
    for (int j = i; j >=0; j--)
    {
    cout << ssn[j] << name[j] << setiosflags (ios::fixed | ios::showpoint) << setprecision(2) << gpa[j] << endl;
    }
    to find the average gpa:
    but im confused, since it starts at 0, should i divide by i or (i+1)?
    Code:
    int tot = 0;
    int aver;
    for (int k = 0; k < i; k++)
    {
    tot += gpa [k];
    aver= tot/i;
    }

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. indexes are from 0 to i-1, so the number of values is i, and you should divide on i
    2. you can divide it outside the loop
    3. you should check that i >0
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i did some more coding and changed the for loop to a while loop
    That's fine, but now you have no check for going over the 35 maximum. I'm not sure if your assignment requires it, but if it does you should check i after incrementing and break the loop if it reaches 35.

    >> will the set precision below only effect the gpa?
    I would think so, since setprecision should not affect integer or string output.

    >> since it starts at 0, should i divide by i or (i+1)?
    The 0 start doesn't add anything to the sum, so it should not affect the average. You might consider doing the division after the loop, though, since it's useless to calculate that value over and over inside the loop.

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    I think this is right.... but i have errors:



    Code:
    aname9.cpp: In function `int main()':
    aname9.cpp:43: warning: assignment to `int' from `double'
    aname9.cpp:43: warning: argument to `int' from `double'
    
    
    
    
    
    
    
    //Name
    //Program 9
    //Write a program that reads info from a dat file and stores it in vectors and arrays.
    
    #include<iostream>
    #include<iomanip>
    #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;
                            }
                    int i=0;
                    while (!fileout.eof())
                            {
                            fileout >> ssn[i];
                            fileout.ignore(80,'\n');
                            getline (fileout,name[i]);
                            fileout >> gpa[i];
                            i++;
                            fileout >> ws;
                            }
                    for (int j = i; j >=0; j--)
                            {
                            cout << ssn[j] << setw(20) << name[j] << setw(10) << setiosflags (ios::fixed | ios::showpoint) << setprecision(2) << gpa[j] << endl;
                            }
                    cout << endl;
                    int tot = 0; 
                    int aver;
                    for (int k = 0; k < i; k++)
                            {
                            tot += gpa [k]; 
                            }
                    aver= tot/i;
                    cout << "Total number of students: " << i << endl;
                    cout << "Average GPA: " << aver;
            return 0;
            }//int main close

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int tot = 0;
    int aver;
    both should be double
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    getting there...

    my output is just looking odd... new code:
    Code:
    #include<iostream>
    #include<iomanip>
    #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;
                            }
                    int i=0;
                    while (!fileout.eof())
                            {
                            fileout >> ssn[i];
                            fileout.ignore(80,'\n');
                            getline (fileout,name[i]);
                            fileout >> gpa[i];
                            i++;
                            fileout >> ws;
                            }
                    cout << setw(9) << "SS Number" << setw(20) << "Name" << setw(10) << "GPA" << endl;
                    for (int j = i; j >=0; j--) 
                            {
                            cout << ssn[j] << setw(20) << name[j] << setw(10) << setiosflags (ios::fixed | ios::showpoint) << setprecision(2) << gpa[j] << endl;
                            }
                    cout << endl;
                    double tot = 0;
                    double aver;
                    for (int k = 0; k < i; k++)
                            {
                            tot += gpa [k];
                            }
                    aver= tot/i; 
                    cout << "Total number of students: " << i << endl;
                    cout << "Average GPA: " << aver;  
            return 0;
            }//int main close


    output:
    Code:
    SS Number                Name       GPA
    0                          0.00
    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

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    for (int j = i; j >=0; j--)
    there is not element with index i
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    Quote Originally Posted by vart
    there is not element with index i
    what does that mean?

  10. #25
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you starting your print out with i and continue till 0

    but valid indexes are from 0 to i-1

    could you figure out why is your output strange?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    im doing something wrong, because i tried changing it to

    for (int j = i; j >0; j--)

    and it made it worse... im not sure i understand.

  12. #27
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    the 0 is not a probllem - problem is other side of the interval
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    the int j= i?

  14. #29
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    should be j = i-1
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    60
    oh. crap. alright, thank you.

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, 09: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, 03:45 PM