Thread: File Input

  1. #1
    Registered User
    Join Date
    Feb 2003

    File Input

    I am writing a program that will read information from a file and then output the screen. The problem is when is reads the name in the sile. It is only outputing the 1st 2 characters of the last name. The grade calculations are not correct as well. When I take out the name, the grade calculations are correct and output correctly as well. Do I need to use char strings for the name?. Any suggestions would be appreciated.



    /************************************************** ***************
    This program will load a input file which consists of the students
    last name, first name, 5 quiz scores, midterm score, final score,
    and 16 program scores. The scores will then be averaged, where
    needed as well as weigthed with 30 percent for the quizes,
    30 percent for the programs, 20 percent for the mid-term, and
    20 percent for the final. The program will then output the student
    name as well as the calculated final grade.
    ************************************************** ****************/

    #include <fstream>
    using std::ifstream;
    using std:fstream;
    using std::endl;
    using std::cout;

    int main()

    ifstream inStream;
    ofstream outStream;"A:\\gradefile.txt");"A:\\outfile.txt");

    cout<<"Input file opening failed.\n";

    char lastName, firstName;
    char gradeFinal;
    int quiz, quiz1, quiz2, quiz3, quiz4, quiz5;
    int midTerm, final, grade;
    int prog, prog1, prog2, prog3, prog4, prog5, prog6, prog7, prog8, prog9, prog10;
    int prog11, prog12, prog13, prog14, prog15, prog16;

    inStream>>prog1>>prog2>>prog3>>prog4>>prog5>>prog6 >>prog7>>prog8>>prog9>>prog10>>prog11>>prog12>>pro g13>>prog14>>prog15>>prog16;

    prog=(prog1+prog2+prog3+prog4+prog5+prog6+prog7+pr og8+prog9+prog10+prog11+prog12+prog13+prog14+prog1 5+prog16)/16;
    grade=(prog*.30)+(quiz*.30)+(midTerm*.20)+(final*. 20);

    else if(grade>=80 && grade<90)
    else if(grade>=70 && grade <80)
    else if(grade>=60 && grade<70)
    else if(grade<60)

    cout<<lastName<<firstName<<" "<<gradeFinal<<endl;


    return 0;


    The .txt file looks like:
    Doe John
    91 90 100 95 90
    84 95
    90 91 92 94 95 96 97 98 99 100 99 98 97 96 95 94

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Ontario, Canada
    char variables only hold one character. You will need an array of chars to hold a name.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    you're only using char variables for names. Use a char array or a string.

    Use an int array and a int temp to read in the grades as well since it will save you from having to code so many variables.

    [edit] doh..... beaten to the punch [/edit]
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    When you do this:
    char c;
    cin >> c;
    You are asking for a single character of input to be placed in the single character "c". You need to input a string. The easy'est way to fix your problem is to declare first/last name as:
    char lastName[100], firstName[100];
    This will give you 100 characters for both the first and last name - more than enough. There are other possible improvements, but you'll learn those in time.

    Your grade calculations look ok. Since you are doing integer math, if a grade calculates to 89.9, the end result will be 89. You can round to the nearest whole number by adding 0.5 to your calculations like so:
    ...)/16.0 + 0.5;
    ...)/5.0 +0.5;
    ...*.20) + 0.5;
    The reason you want to use "16.0" and "5.0" is so the copiler will generate code that performs a floating point division and not an integer division. Remember that in integer division, 5/3 = 1.

    Give another post if you have any other questions. Use code tags next time you post code.


  5. #5
    Registered User
    Join Date
    Feb 2003
    My last question would be how to read the characters from my .txt file into the char string. Would I need to use either a for or while loop and look for EOF?.



  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    What you have is fine for the format you posted.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM