1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Rep Power

    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 <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream> // used for input and outpu files

    using namespace std;

    void main()

    ifstream infile;
    ofstream outputfile;

    cout<<right<<setw(7)<<"SSN"<<right<<setw(17)<<"Name"<<right<<setw(23)<<"Test Scores";

    string ssn,name; // Social security and name
    float average;
    int num1,num2,num3,num4,num5;
    char grade;

    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
    getline(infile, name);


    infile>>num3; // retrieving the 5 tests scores

    average=(num1+num2+num3+num4+num5) / 5.0;

    if (average<60)
    else if (average<70)
    else if (average<80)
    else if (average<90)
    else if (average<=100)



    the output:

    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

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Rep Power
    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.

    for eg
    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
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Rep Power

    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:

    then either
    <grade><linebreak>(five times, one on each line)
    <grade><space or tab>(five times, all on the same line)<linebreak>
    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.

    BTW: You should be using an array for the grades (really, you should use a structure/class for all the data).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Rep Power
    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:

    getline(infile, ssn);

    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;


IMN logo majestic logo threadwatch logo seochat tools logo