Page 1 of 1

c++ (sstream)..

Posted: Thu Oct 26, 2006 4:03 pm
by jakskal
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: Select all

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
[code]

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;
}
[code] 

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:4580, old post ID:36475[/size][/color]

c++ (sstream)..

Posted: Thu Oct 26, 2006 8:04 pm
by jakskal
nevermind..got it.. ;)

Archived topic from Iceteks, old topic ID:4580, old post ID:36479

c++ (sstream)..

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

Archived topic from Iceteks, old topic ID:4580, old post ID:36480

c++ (sstream)..

Posted: Thu Oct 26, 2006 10:50 pm
by jakskal
Red Squirrel wrote: Good to hear, since I've never used sstream class before or dont recall, so could not see what was wrong. :P
i didn't use sstream...i ended up using something simple!

Code: Select all

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

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

how'd i miss it?!  :lol:  

[color=#888888][size=85]Archived topic from Iceteks,  old topic ID:4580, old post ID:36482[/size][/color]

c++ (sstream)..

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

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


Archived topic from Iceteks, old topic ID:4580, old post ID:36485

c++ (sstream)..

Posted: Sun Oct 29, 2006 5:52 pm
by jakskal
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?

Archived topic from Iceteks, old topic ID:4580, old post ID:36549

c++ (sstream)..

Posted: Sun Oct 29, 2006 7:38 pm
by jakskal
nvm..got it! :D

Archived topic from Iceteks, old topic ID:4580, old post ID:36550