>> does it involve another for loop with an i-- in it
That sounds like it could work.
>> does it involve another for loop with an i-- in it
That sounds like it could work.
alright, i did some more coding and changed the for loop to a while loop:
to store in the vectors and array:
to list them backwards: (excluding the setw, will add later)Code:int i=0; while (!fileout.eof()) { fileout >> ssn[i]; fileout.ignore(80,'\n'); getline (fileout,name[i]); fileout >> gpa[i]; i++; fileout >> ws; }
will the set precision below only effect the gpa?
to find the average gpa:Code:for (int j = i; j >=0; j--) { cout << ssn[j] << name[j] << setiosflags (ios::fixed | ios::showpoint) << setprecision(2) << gpa[j] << endl; }
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; }
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
>> 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.
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
both should be doubleint tot = 0;
int aver;
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
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
there is not element with index ifor (int j = i; j >=0; j--)
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
what does that mean?Originally Posted by vart
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
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.
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
the int j= i?
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
oh. crap. alright, thank you.