2 Replies - 250 Views - Last Post: 19 April 2013 - 09:25 AM Rate Topic: -----

#1 muin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-April 13

Display from input file using the same file as output.

Posted 19 April 2013 - 05:24 AM

i'm having a problem to display the value when i read from input file. The input file come from the same file from the output. It displayed 13xxxxe.xxxx kind of numbers. first of all, i tried to use file.open ("doc.txt",ios::in | ios::out); but it didn't work same as now.


#include <iostream>
#include <fstream>
#include <iomanip>
#include<cstdlib>
using namespace std;

struct Quarter
{
float first,
second,
third,
fourth;
};

struct Division
{
Quarter q;

};

int main ()
{
long double quarter1_total=0.0,quarter2_total=0.0,quarter3_total=0.0,
quarter4_total=0.0,
easttotal=0.0,westtotal=0.0,northtotal=0.0,southtotal=0.0,
annual_east=0.0, annual_west=0.0,annual_north=0.0,
annual_south=0.0,total_all=0.0,
east_ave=0.0,west_ave=0.0,north_ave=0.0,south_ave=0.0,
highest,lowest,
east_1st,east_2nd,east_3rd,east_4th,
west_1st,west_2nd,west_3rd,west_4th,
north_1st,north_2nd,north_3rd,north_4th,
south_1st,south_2nd,south_3rd,south_4th;
//array for quart
float quart[]={quarter1_total,quarter2_total,quarter3_total,quarter4_total};

ofstream file;


file.open("Sales Data.txt");

Division c[4];
cout<<"Enter data as prompt.\n";

for (int y=0;y<4;y++)
{
if(y==0)
cout<<"Data for East\n\n";

else if(y==1)
cout<<"\nData for West\n\n";

else if(y==2)
cout<<"\nData for North\n\n";

else
cout<<"\nData for South\n\n";

for (int x=0;x<4;x++)
{
if (x==0)
{
cout <<x+1<<"st quarter : ";
cin >>c[y].q.first;
file << c[y].q.first<<" ";;
}

else if (x==1)
{
cout <<x+1<<"nd quarter : ";
cin >>c[y].q.second;
file << c[y].q.second<<" ";;
}

else if (x==2)
{
cout <<x+1<<"rd quarter : ";
cin >>c[y].q.third;
file << c[y].q.third<<" ";
}

else
{
cout <<x+1<<"th quarter : ";
cin >>c[y].q.fourth;
file << c[y].q.fourth<<endl;
}
}
}
file.close();

ifstream infile;
infile.open("Sales Data");

//question b
//read files

// int a;

infile >> east_1st;
infile >> east_2nd;
infile >> east_3rd;
infile >> east_4th;
// cout << " " << east_1st << " " << east_2nd << " " << east_3rd << " " << east_4th << endl;

infile >> west_1st;
infile >> west_2nd;
infile >> west_3rd;
infile >> west_4th;

infile >> north_1st;
infile >> north_2nd;
infile >> north_3rd;
infile >> north_4th;

infile >> south_1st;
infile >> south_2nd;
infile >> south_3rd;
infile >> south_4th;

quarter1_total=east_1st + west_1st + north_1st + south_1st;
quarter2_total=east_2nd + west_2nd + north_2nd + south_2nd;
quarter3_total=east_3rd + west_3rd + north_3rd + south_3rd;
quarter4_total=east_4th + west_4th + north_4th + south_4th;


annual_east=east_1st + east_2nd + east_3rd + east_4th;
annual_west=west_1st + west_2nd + west_3rd + west_4th;
annual_north=north_1st + north_2nd + north_3rd + north_4th;
annual_south=south_1st + south_2nd + south_3rd + south_4th;

east_ave=annual_east/4;
west_ave=annual_west/4;
north_ave=annual_north/4;
south_ave=annual_south/4;

total_all=annual_east + annual_west + annual_north + annual_south;

highest=0.0;
lowest=0.0;
for(int x=0;x<4;x++)
{

if(highest<quart[x])
highest=quart[x];
if(lowest>quart[x])
lowest=quart[x];

}


cout << setw(10) << "Division" << setw(18) << "\tQuarterly Average\n"
<< setw(14) << "\tAnnual Total"
<< setw(18) << "\tAnnual Average\n";

cout << setw(10) << "\t\nEast \t" << setw(10) << east_ave << "\t"
<< setw(18) << annual_east << endl;

cout << setw(10) << "\t\nWest \t" << setw(10) << west_ave << "\t"
<< setw(18) << annual_west << endl;

cout << setw(10) << "\t\nNorth \t" << setw(10) << north_ave << "\t"
<< setw(18) << annual_north << endl;

cout << setw(10) << "\t\nSouth \t" << setw(10) << south_ave << "\t"
<< setw(18) << annual_south << endl ;

cout << setw(10) << "\t\nTOTAL \t" << setw(30) << total_all << endl ;

infile.close();


return 0;
} 




Is This A Good Question/Topic? 0
  • +

Replies To: Display from input file using the same file as output.

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,287
  • Joined: 25-December 09

Re: Display from input file using the same file as output.

Posted 19 April 2013 - 08:02 AM

Before I even try to read your code you need to find an indentation style you like and use it consistently. This will make your program much easier to read and make it possible to follow the logic of your program.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 muin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-April 13

Re: Display from input file using the same file as output.

Posted 19 April 2013 - 09:25 AM

actually, in my codes, there is indentation. I don't know what happened in here. Sorry for my mistakes.

I already managed to do it. Thanks by the way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1