Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7062015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:32:37+00:00 2026-05-28T04:32:37+00:00

Possible Duplicate: enhancing a program – complete failure im asked to write a program

  • 0

Possible Duplicate:
enhancing a program – complete failure

im asked to write a program to read the content of a text file in the form of:

Jones Tom 94 99 96 74 56 33 65 89 87 85  
Thompson Frank 67 58 86 95 47 86 79 64 76 45  
Jackson Tom 95 97 94 87 67 84 99 45 99 87  
Jackson Michael 43 23 34 77 64 35 89 56 75 85  
Johnson Sara 84 93 64 57 89 99 74 64 75 35 91

and output the average of each student into another file in the form of:

Jones Tom 94 99 96 74 56 33 65 89 87 85 77.8
Thompson Frank 67 58 86 95 47 86 79 64 76 45 70.3
Jackson Tom 95 97 94 87 67 84 99 45 99 87 85.4
Jackson Michael 43 23 34 77 64 35 89 56 75 85 58.1
Johnson Sara 84 93 64 57 89 99 74 64 75 35 73.4

i successfully managed to do that using the code below:

#include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cstring>
    using namespace std;

    int main()
    {
        fstream infile("grades.txt",ios::in);
        if(!infile){cerr<<"file could not be found!";exit(1);}

        fstream outfile("average.txt",ios::out);
        if(!outfile){cerr<<"file could not be created!";exit(1);}


        char fname[20];
        char lname[20];
        int grades;
        char c;
        int lines=1;
        double avg=0;

        while(infile.get(c))
        {if(c=='\n') lines++;}
        infile.clear();
        infile.seekg(0);

        for(int k=0;k<lines;k++)
            {
                infile>>fname;
                infile>>lname;
                outfile<<fname<<" "<<lname<<" ";
                int sum=0;
                for(int i=0;i<10;i++)
                {
                    if(infile>>grades)
                    {sum+=grades;
                    outfile<<grades<<" ";}
                }

                outfile<<(double)sum/10.0<<endl;
            }


        system("pause");
        return 0;
    }

but i have a problem.
when the first line of the text file contains grades less than 10. for example:

Jones Tom 94 99 96 74 56 33 65 89 87

i get a messed up output, which is ruining everything. im getting the output below:

Jones Tom 94 99 96 74 56 33 65 89 87 69.3
  0
  0
  0
  0

How can i fix this problem? and how can i make the program to output zeros if i have less than ten grades on each line?
so that for example if i have on the first line:

Jones Tom 94 99 96 74 56 33 65 89

i want the program to calculate the average and output the following to the other file.

Jones Tom 94 99 96 74 56 33 65 89 0 0 64.3

note that 64.3 is the average of the student.

thank you.
kind regards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:32:38+00:00Added an answer on May 28, 2026 at 4:32 am

    Well, the solution is really simple. First, separate the code into two parts, the first to read the grades, do the averaging and write the grades to the output file and the second to write the zeros and the average.

    Have the part that reads the scores accumulate them in sum. Now, you can use the sum and the counter to compute the average.

    Let us look at how the code to read the scores would work

    //Repeat till all grades are read and then move on to the next student
    num_grades_read = 0;
    while //grades are available on the line
    {
        //Read in a new grade
        sum += grades;
        //Write the read grade to the output file.
        outfile << grades;
        num_grades_read++;
    }
    

    Finally, in the second part writing to the output file, you’ll want to do something like this:

    int limit = 10 - num_grades_read;
    for(int j = 0; j < num_grades_read; ++j)
    {
        //Write zero to the file
        outfile << 0 << " ";
    }
    //Write the computed average to the file
    outfile << (double)(sum) / num_grades_read << endl;
    

    Now, the above pseudo-code isn’t very good since it doesn’t have checks and such. You’ll want to ensure you haven’t read in more than 10 grades for instance.

    Since you had trouble with the condition in the first part, here’s a bit of code to help:

    while(!infile.fail())
    {
        infile >> grade;
        sum += grade;
        outfile<<grade<<" ";
        num_grades_read++;
    }
    infile.clear();
    

    I hope this helps out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: Learning to write a compiler I looked around trying to find out
Possible Duplicate: Can you write object oriented code in C? Hi, can someone point
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: git - removing a file from source control (but not from the
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: Best way to determine if two path reference to same file in
Possible Duplicate: Qt equivalent of PathAppend? Is there a class that handles file paths
Possible Duplicate: css rule to disable text selection highlighting On my homepage i have
Possible Duplicate: Space bar not working in form fields Has anyone run into an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.