File IO arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fir3Bat
    New Member
    • Oct 2006
    • 19

    File IO arrays

    Ok fellow programmers
    i got this program
    file is say (71dIN.txt)
    inside file is
    Code:
    45 98 35 23 67 84
    65 91 20 54 62 37 65 84 32
    21 54 95 87 35 62 54 78
    56
    95 62 35 54 78
    now the only problem is is that when i try to find average and total of each line it adds all lines up :S
    this is my code
    Code:
    #include <iostream>
    #include <sstream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	string sBuf;
    	int arnNums[50], nSize, i, nHighest = 0, nLowest = 0, nAverage = 0, nTotal = 0, arn[5], n = 0;
    	ifstream fin("71dIN.txt");
    	istringstream istr(sBuf);
    	while(1)
    	{
    		getline(fin, sBuf);
    		istr.clear();
    		istr.str(sBuf);
    		if(fin.eof()) break;
    		i = 0;
    		while(1)
    		{
    			istr >> arnNums[i];
    			i++;
    			if(istr.eof()) break;
    		}	
    		nSize = i;
    		for(i = 0; i<nSize; i++)
    		{
    			nTotal += arnNums[i];
    			nAverage = nTotal / 4;
    			cout << arnNums[i] << " ";
    		}
    		cout << endl;
    		for(i = 0; i < 1; i++)
    		{
    			cout << "Average is " << nAverage << " and Total is " << nTotal << endl;
    			cout << endl << endl;
    		}
    	}
    	return 0;
    }
    can you guys help if u know the problem... thanks alot
  • Fir3Bat
    New Member
    • Oct 2006
    • 19

    #2
    This is my output
    Code:
    45 98 35 23 67 84
    average is 88 and total is 352
    
    65 91 20 54 62 37 65 84 32
    average is 215 and total is 862
    
    21 54 95 87 35 62 54 78
    average is 337 and total is 1348
    
    56
    average is 351 and total is 1404
    
    95 62 35 54 78
    average is 432 and total is 1728
    Last edited by Fir3Bat; Oct 29 '06, 12:20 AM. Reason: no reason

    Comment

    • Fir3Bat
      New Member
      • Oct 2006
      • 19

      #3
      none wants to help :(

      Comment

      • vninja
        New Member
        • Oct 2006
        • 40

        #4
        looks like your logic is wrong

        nSize = i;
        for(i = 0; i<nSize; i++)
        {
        nTotal += arnNums[i];
        nAverage = nTotal / 4;
        cout << arnNums[i] << " ";
        }

        try this instead

        nSize = i;
        for(i = 0; i<nSize; i++)
        {
        nTotal += arnNums[i];
        nAverage = nTotal / i; // <- average is
        // sum/num
        cout << arnNums[i] << " ";
        }

        Comment

        • vninja
          New Member
          • Oct 2006
          • 40

          #5
          also i don't see where you're reinitializing the values of totals and average back to zero. (i might be blind) but if i'm not then find a place to reinitialize them on the way back.

          Comment

          • vninja
            New Member
            • Oct 2006
            • 40

            #6
            try this



            #include <iostream>
            #include <sstream>
            #include <fstream>
            using namespace std;

            int main()
            {

            ifstream fin("71dIN.txt" );
            istringstream istr(sBuf);
            while(1)
            {

            string sBuf;
            int arnNums[50], nSize, i, nHighest = 0, nLowest = 0, nAverage = 0, nTotal = 0, arn[5], n = 0;


            getline(fin, sBuf);
            istr.clear();
            istr.str(sBuf);
            if(fin.eof()) break;
            i = 0;
            while(1)
            {
            istr >> arnNums[i];
            i++;
            if(istr.eof()) break;
            }
            nSize = i;
            for(i = 0; i<nSize; i++)
            {
            nTotal += arnNums[i];
            nAverage = nTotal / 4;
            cout << arnNums[i] << " ";
            }
            cout << endl;
            for(i = 0; i < 1; i++)
            {
            cout << "Average is " << nAverage << " and Total is " << nTotal << endl;
            cout << endl << endl;
            }
            }
            return 0;
            }

            Comment

            Working...