July 25th, 2003, 10:03 AM
string and numbers problem
i need to retrieve a file called (lab3in.txt) from disk and display it in a certain format and do calculations
the file has the following data (ssn, full name, 5 grades)
467-089-7654 Vanna White 95 23 87 34 53
there a total of 10 students
i need to be able to display this info in the same format, in addition to calculating the average score and assigning a letter grade. including a loop
i am having a problem with info being all in the same line, strings and numbers. i have no problem when every entry is on separate line. so far i have this program for 1 student:
#include <fstream> // used for input and outpu files
using namespace std;
string ssn,name; // Social security and name
infile.open("a:\lab3A.txt", ios::in); // retrieving data from file
outputfile.open("lab3in.txt", ios::out); // printing data to file
getline(infile, ssn); //command to retrieve data from disk
infile>>num3; // retrieving the 5 tests scores
average=(num1+num2+num3+num4+num5) / 5.0;
else if (average<70)
else if (average<80)
else if (average<90)
else if (average<=100)
SSN Name Test Scores Average Grade
467-89-7654 Dave Wannstedt 77 67 72 69 71 -858993460,-858993460
,-858993460,-858993460,-858993460 -0.8 F
Press any key to continue
July 25th, 2003, 03:36 PM
Off hand i would suggest read the entire line in character array and read the array from last converting the character numbers like 75 into integer and break once u read the first character that is not a number.
999-999-999 xyz 54 45 65 74 84
read the array till u see z.
hope this logic helps. I dont have a machine that has C++ facility to try the logic.
I am sure there are better logic then this.
Hope this helps
July 29th, 2003, 11:18 AM
Control of input format?
If you have control of the input file I suggest you change it for easier processing. If you can't control the format, you will have to write a lot of ugly input processing code to attempt to handle each and every possible screwed up format. Presuming you have some influence on the input file, I suggest something relatively strait forward like this:
This gives you an easy to parse file that allows for simple modification. You can easily make the number of students and/or grades variable by simply reading and processing them until you get to the double blank line. Another formatting method is the fixed with record, but that wastes space and probably does not lend itself to C++ input processing.
<grade><linebreak>(five times, one on each line)
<grade><space or tab>(five times, all on the same line)<linebreak>
BTW: You should be using an array for the grades (really, you should use a structure/class for all the data).
July 29th, 2003, 12:53 PM
The problem is your use of the getline() function. getline() reads in data until a newline(\n) character is read in, which occurs at the end of each line of your input data, so this line of your code:
reads in not only the social security number, but also the name, and the five scores into the string variable ssn.
On the other hand, the >> operator will read in data until it encounters a white space character, and you should take advantage of that since each piece of your data is separated by a space:
string ssn, first_name, last_name;
int score1, score2, score3, score4, score5;