Home | Forums | Articles | F@H
Help | Search | Members | Calendar | Forum Map | Cybervillage | Archive
Welcome Guest ( Log In | Register | Resend Validation Email )


>  IceTeks Forums -> Technology made cool -> Programming


  Archived - No replies allowed - start new thread insteadStart new topicStart Poll

>  c++ (sstream)..
Track this topic | Email this topic | Print this topic
jakskal
Posted: Oct 26 2006, 04:03 PM
Quote Post

Icicle

Group Icon

Group: IceTeks Newbies
Posts: 17
Member No.: 1 048
Joined: 24-October 05
Ice Cubes: 51



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;
}
PMEmail Poster Top
jakskal
Posted: Oct 26 2006, 08:04 PM
Quote Post

Icicle

Group Icon

Group: IceTeks Newbies
Posts: 17
Member No.: 1 048
Joined: 24-October 05
Ice Cubes: 51



nevermind..got it.. wink.gif
PMEmail Poster Top
Red Squirrel
Posted: Oct 26 2006, 08:43 PM
Quote Post

ZOMG!

Group Icon

Group: Admins
Posts: 13 964
Member No.: 1
Joined: 18-December 02
Ice Cubes: 163183



Good to hear, since I've never used sstream class before or dont recall, so could not see what was wrong. tongue.gif


--------------------
user posted image
http://www.redsquirrel.me my blog
http://www.uovalor.com AoS/ML Ultima Online Shard
PMEmail PosterUsers Website Top
jakskal
Posted: Oct 26 2006, 10:50 PM
Quote Post

Icicle

Group Icon

Group: IceTeks Newbies
Posts: 17
Member No.: 1 048
Joined: 24-October 05
Ice Cubes: 51



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
PMEmail Poster Top
Red Squirrel
Posted: Oct 27 2006, 03:19 AM
Quote Post

ZOMG!

Group Icon

Group: Admins
Posts: 13 964
Member No.: 1
Joined: 18-December 02
Ice Cubes: 163183



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


--------------------
user posted image
http://www.redsquirrel.me my blog
http://www.uovalor.com AoS/ML Ultima Online Shard
PMEmail PosterUsers Website Top
jakskal
Posted: Oct 29 2006, 06:52 PM
Quote Post

Icicle

Group Icon

Group: IceTeks Newbies
Posts: 17
Member No.: 1 048
Joined: 24-October 05
Ice Cubes: 51



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?
PMEmail Poster Top
jakskal
Posted: Oct 29 2006, 08:38 PM
Quote Post

Icicle

Group Icon

Group: IceTeks Newbies
Posts: 17
Member No.: 1 048
Joined: 24-October 05
Ice Cubes: 51



nvm..got it! em320.gif
PMEmail Poster Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:
« Next Oldest | Programming | Next Newest »

Topic Options Archived - No replies allowed - start new thread insteadStart new topicStart Poll

 



[ Script Execution time: 0.1109 ]   [ 13 queries used ]   [ GZIP Enabled ]

< Home | Forums | Contact >