Store Multiple result into a text file using fstream

This is a discussion on Store Multiple result into a text file using fstream within the C++ Programming forums, part of the General Programming Boards category; Hi all im stuck with a problem im ask to enter 8 student marks and calculate the average mark and ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    4

    Store Multiple result into a text file using fstream

    Hi all im stuck with a problem im ask to enter 8 student marks and calculate the average mark and store them into a text file but i cant seem to storm the 8 student marks but got no problem with the average this is what i type in my codeblock C++



    Code:
    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
               int i,sum, marks[8];
               double average;
    
                ofstream outfile("outfile1.txt");
                for (i=0;i<8;i++)
                {
                cout << "Enter Next Student: " << endl;
                cin >> marks[i];
                }
                outfile<<"Student"<<i+1<<
                marks[8]<<endl;
    
    
                sum=0;
                for (i=0;i<8;i++)
                {
                    sum=sum+marks[i];
                }
                average=sum/(i);
                cout<<"The Result is printed in outfile1.txt";
                outfile<<"The average score is : "<<
                average<<endl;
    
                outfile.close();
                return(0);
    
    }
    and this is wat i get from my outfile1.txt

    Student92009291924
    The average score is : 2

    i need help thx

    or do i have to do the stupid and long way which is
    Code:
    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
               int a,b,c,d,e,f,g,h;
               double average;
    
                ofstream outfile("outfile1.txt");
    
                {
                cout << "Enter Next Student: " << endl;
                cin >> a;
                }
    
                outfile<<"Student: "<<1<<" "<<
                a<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> b;
                }
                outfile<<"Student: "<<2<<" "<<
                b<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> c;
                }
                outfile<<"Student: "<<3<<" "<<
                c<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> d;
                }
                outfile<<"Student: "<<4<<" "<<
                d<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> e;
                }
                outfile<<"Student: "<<5<<" "<<
                e<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> f;
                }
                outfile<<"Student: "<<6<<" "<<
                f<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> g;
                }
                outfile<<"Student: "<<7<<" "<<
                g<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> h;
                }
                outfile<<"Student: "<<8<<" "<<
                h<<endl;
    
    
    
                average=(a+b+c+d+e+f+g+h)/8;
                cout<<"The Result is printed in outfile1.txt";
                outfile<<"The average score is : "<<
                average<<endl;
    
                outfile.close();
                return(0);
    
    }
    Last edited by yappy; 07-30-2010 at 11:23 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    You are accessing past the last element of an array:

    Quote Originally Posted by yappy View Post
    Hi all im stuck with a problem im ask to enter 8 student marks and calculate the average mark and store them into a text file but i cant seem to storm the 8 student marks but got no problem with the average this is what i type in my codeblock C++



    Code:
    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
               int i,sum, marks[8];
               double average;
    
                ofstream outfile("outfile1.txt");
                for (i=0;i<8;i++)
                {
                cout << "Enter Next Student: " << endl;
                cin >> marks[i];
                }
                outfile<<"Student"<<i+1<<
                marks[8]<<endl;
    
    
                sum=0;
                for (i=0;i<8;i++)
                {
                    sum=sum+marks[i];
                }
                average=sum/(i);
                cout<<"The Result is printed in outfile1.txt";
                outfile<<"The average score is : "<<
                average<<endl;
    
                outfile.close();
                return(0);
    
    }
    and this is wat i get from my outfile1.txt

    Student92009291924
    The average score is : 2

    i need help thx

    or do i have to do the stupid and long way which is
    Code:
    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
               int a,b,c,d,e,f,g,h;
               double average;
    
                ofstream outfile("outfile1.txt");
    
                {
                cout << "Enter Next Student: " << endl;
                cin >> a;
                }
    
                outfile<<"Student: "<<1<<" "<<
                a<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> b;
                }
                outfile<<"Student: "<<2<<" "<<
                b<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> c;
                }
                outfile<<"Student: "<<3<<" "<<
                c<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> d;
                }
                outfile<<"Student: "<<4<<" "<<
                d<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> e;
                }
                outfile<<"Student: "<<5<<" "<<
                e<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> f;
                }
                outfile<<"Student: "<<6<<" "<<
                f<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> g;
                }
                outfile<<"Student: "<<7<<" "<<
                g<<endl;
                {
                cout << "Enter Next Student: " << endl;
                cin >> h;
                }
                outfile<<"Student: "<<8<<" "<<
                h<<endl;
    
    
    
                average=(a+b+c+d+e+f+g+h)/8;
                cout<<"The Result is printed in outfile1.txt";
                outfile<<"The average score is : "<<
                average<<endl;
    
                outfile.close();
                return(0);
    
    }

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    4

    Store Multiple result into a text file using fstream

    Hmm ok then wat should i change in my coding to get the solution

  4. #4
    Registered User
    Join Date
    Jul 2010
    Location
    Sandnes, Norway
    Posts
    2
    outfile<<"Student"<<i+1<< marks[8]<<endl;

    what are you trying to do here? marks[8] is outside of your array, so it will contain garbage. Do a for loop from i=0 to i=7 and output marks[i] to the file, for example.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Thx i did it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21