Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,187 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,293 people online right now. Registration is fast and FREE... Join Now!




fstream

 
Reply to this topicStart new topic

fstream, how write a loop that iterates six times

centraspike
1 Jun, 2008 - 08:36 PM
Post #1

New D.I.C Head
*

Joined: 1 Jun, 2008
Posts: 3


My Contributions
// test.cpp : Defines the entry point for the console application.
// i am using this code to read a text file of six lines with three integers on each line but when i execute the code it wont display on the screen.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
cout << setiosflags(ios::fixed|ios::showpoint);
cout << setprecision(3);


int num1;
int num2;
int num3;

int sum;
double average;
int product;
int smallest;
int largest;

string line;
ifstream infile("c:\\input.txt"); // note the double backslash in the filename path
if ( ! infile ) {
cerr << "Input file could not be opened" << endl;
exit(1); } // need header file stdlib.h for this function
ofstream outfile("c:\\output.txt");
if ( ! outfile ) {
cerr << "Output file could not be opened" << endl;
exit(1);
}

outfile << setiosflags(ios::fixed|ios::showpoint);
outfile << setprecision(3);

while (infile >> num1 >> num2 >> num3);// use the c++ idiom: while(infile>>a>>b>>c)
string line;

while (!infile.eof())
{
while (infile>>num1>>num2>>num3);
cout << num1 << num2 << num3 << endl;

outfile << setw(47)<< "For the integers: " << setw(5) << num1 << setw(5) << num2 << setw(5) << num3 << endl;
cout << setw(47) << "For the integers: " << setw(5)<< num1 << setw(5) << num2 << setw(5) << num3 << endl;


sum = (num1 + num2 + num3);
outfile << setw(36) << "Sum is " << sum << endl;
cout << setw(36) << "Sum is " << sum << endl;

average = static_cast<double>(sum)/3;
outfile << setw(40)<< "Average is " << average << endl;
cout << setw(40) << "Average is " << average << endl;

product = num1 * num2 * num3;
outfile << setw(40) << "Product is " << product << endl;
cout << setw(40) << "Product is " << product << endl;

smallest = num1;
if (num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
outfile << setw(41) << "Smallest is " << smallest << endl;
cout << setw(41) << "Smallest is " << smallest << endl;

largest = num3;
if (num1 > largest)
largest = num1;
if (num2 > largest)
largest = num2;
outfile << setw(40) << "Largest is " << largest << endl << endl;
outfile << & quot;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~" << endl;
cout << setw(40) << "Largest is " << largest << endl << endl;
cout << & quot;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~" << endl;


}


system("PAUSE");

infile.close();
outfile.close();
return 0;
}

This post has been edited by centraspike: 1 Jun, 2008 - 09:05 PM
User is offlineProfile CardPM
+Quote Post

bodom658
RE: Fstream
2 Jun, 2008 - 07:53 PM
Post #2

D.I.C Head
**

Joined: 22 Feb, 2008
Posts: 154



Thanked: 5 times
My Contributions
I would use a for loop, not a while loop. It is much easier if you know how many times you need to read through the file. It is less versatile, if say you wanted to read a file with 7 lines. however, it gets the job done.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 11:39AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month