Printable Version of Topic
Click here to view this topic in its original format
IceTeks Forums > Programming > c++ (sstream)..


Posted by: jakskal Oct 26 2006, 04:03 PM
ok..i'm working on the following problem:

Write a program that reads up to 10 numbers per line to be
stored in an array. The program calculates and displays the sum
of the numbers, the mean (arithmetic average) of the numbers,
and the largest and smallest values entered for each line.

Here's the file it reads from:

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


i've got every except the part where it skips the line when it reaches the end of line..i know it has something to do with the sstream function. any help?

CODE

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

int main()
{
string sBuf;
int x, i, arnNumber[10][10], nCounter=0, nSum=0, nMax, nMin=99999;
double dMean;
ifstream fin;
ofstream fout;
fin.open("71dIN.txt");
istringstream istr(sBuf);
while(!fin.fail()){
 for(x=0; x<10; x++)
 {
  fin>>arnNumber[nCounter][x];
 }
nCounter++;
}

for(x=0; x<nCounter; x++)
{
 for(i=0; i<10; i++)
 {
  nSum += arnNumber[nCounter][i];
  if(arnNumber[nCounter][i]>nMax)
  {
   nMax = arnNumber[nCounter][i];
  }
  if(arnNumber[nCounter][i] < nMin)
  {
   nMin = arnNumber[nCounter][i];
  }
 }
 dMean = nSum/10;
 cout<<"The sum of line "<<nCounter+1<<" is "<<nSum<<endl;
 cout<<"The average of line "<<nCounter+1<<" is "<<dMean<<endl;
 cout<<"The largest value of line "<<nCounter+1<<" is "<<nMax<<endl;
 cout<<"The smallest value of line "<<nCounter+1<<" is "<<nMin<<endl;
 nSum = 0;
}
fin.close();
return 0;
}

Posted by: jakskal Oct 26 2006, 08:04 PM
nevermind..got it.. wink.gif

Posted by: Red Squirrel Oct 26 2006, 08:43 PM
Good to hear, since I've never used sstream class before or dont recall, so could not see what was wrong. tongue.gif

Posted by: jakskal Oct 26 2006, 10:50 PM
QUOTE (Red Squirrel @ Oct 26 2006, 08:43 PM)
Good to hear, since I've never used sstream class before or dont recall, so could not see what was wrong. tongue.gif

i didn't use sstream...i ended up using something simple!

CODE

if (fin.peek() == '\n')  // checks for new line
     {
         fin.ignore(1);       // ignores the new line and moves fin position to next line
         break;               // exits the for loop
     }


it basically checks the file for the end of line and jumps onto the next line..

how'd i miss it?! laugh.gif

Posted by: Red Squirrel Oct 27 2006, 03:19 AM
Yeah thats how I'd normally do it.

Keep in mind that some files also use \r\n (*nix? , or is it windows? I always get confused) for return sequence. But searching for \n should do the trick in most cases. But if you start seeing weird chars its probably the \r

Posted by: jakskal Oct 29 2006, 06:52 PM
hey...i need to check for a blank line which contains nothing and skip it..kinda similar to above..but the above code doesn't do the trick this time...anyone?

Posted by: jakskal Oct 29 2006, 08:38 PM
nvm..got it! em320.gif

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)