View Full Version : C++ files
Snydaleid
04-30-04, 01:46 AM
I need to take these numbers in an input file
3 4 5
3.2 5.4 10.1
3 3 3
10 2 6
3.65 4.275 8.25
8.5 12.25 6.75
Calculate the square root of s(s-a)(s-b)(s-c) where s= (a+b+c)/2.
a, b, c refer to the numbers above. First column is a, second is b, third is c
after it's calculated it is stored in the output file.
This is my first assignment with files and I'm way lost.
Thanks in advance for any help you can give.
hyjacked
05-02-04, 02:48 PM
hey, what have you got so far as far as code goes? If you post what you have then we are more than happy to point out problems and help you when you are stuck. If you just post generally, few people will reply as most people don't have time to write up all the code. And besides, you would learn less that way.
gopinath_alr
05-03-04, 07:57 AM
the following code is working and u can use it for your purpopse. Before that you have to make sure that the input file is copied into the folder which contains cpp file.
#include <stdio.h>
#include <string.h>
#include <fstream.h>
#include <iostream.h>
#include <math.h>
void main()
{
//file stream objects.
ifstream fin;
ofstream fout;
char inStr[100],outStr[100];
char *token;
double numarray[4],result,s;
int index=0;
fin.open("inpfile.txt",ios::in,filebuf::sh_read);
fout.open("outfile.txt",ios::out,filebuf::sh_write);
fin.getline(inStr,100,'\n');
while(fin.eof() == 0)
{
index=0;
token=strtok(inStr," ");
while(token != NULL)
{
numarray[index++]=atof(token);
cout<<token<<endl;
token=strtok(NULL," ");
}
s=(numarray[0]+numarray[1]+numarray[2])/2;
result=s*(s-numarray[0])*(s-numarray[1])*(s-numarray[2]);
cout<<result<<endl;
sprintf(outStr,"%f\n",pow(result,0.5));
cout<<outStr<<endl;
fout.write(outStr,strlen(outStr));
fin.getline(inStr,100,'\n');
}
if(fin.is_open()) fin.close();
if(fout.is_open()) fout.close();
}
regards,
gobinath.
vBulletin® v3.6.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.