Please register or login. There are 0 registered and 1082 anonymous users currently online. Current bandwidth usage: 326.30 kbit/s December 18 - 07:32am EST 
Hardware Analysis
      
Forums Product Prices
  Contents 
 
 

  Latest Topics 
 

More >>
 

    
 
 

  You Are Here: 
 
/ Forums / Programming /
 

  C++ column and inputting file help? 
 
 Author 
 Date Written 
 Tools 
kayliu liu Apr 26, 2009, 02:15am EDT Reply - Quote - Report Abuse
Private Message - Add to Buddy List

Edited: Apr 26, 2009, 01:29pm EDT

Replies: 0 - Views: 1252
I need to write a program that can read integer from a file, display the data, and also display the largest and smallest integers in the data set. Then display each integer in a field width of 5, with a new line after each of 5 integers.
Example:
Enter name of data file containing integers: number1.dat
Data:

0 1 2 3 4
5 6 7 8 9
10 11 12 13

The largest integer is 13.
The smallest integer is 0.

Would you like to read another file (y/n)? n
Goodbye.

File number1.dat contains: 0 1 2 3 4 5 6 7 8 9 10 11 12 13

I do not know the codes that ask the users to input the file and how to put the number in a field width of 5 and with a new line after each of 5 integers.

Please share the codes, thanks.

Here is what I have done so far, I know is not correct

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

int main()
{
const int ARRAY_SIZE = 10;
int numbers[ARRAY_SIZE];
int count;
int small = INT_MAX;
int large = INT_MIN;

ifstream inputFile;

string GetFileName;
string line;

cout << "System >> Enter file to read: \n";
cout << "User >> ";
cin >> GetFileName;


for (count = 0; count < ARRAY_SIZE; count++)
inputFile >> numbers[count];


for (int count=0; count<10; count++)
{
if (numbers[count] < small)
small = numbers[count];
if (numbers[count] > large)
large = numbers[count];
}

cout << "The number are: ";
for (count = 0; count < ARRAY_SIZE; count++)
cout << " " << numbers[count] << endl;
cout <<"The smallest is: " << small << endl;
cout <<"The largest is: " << large << endl;

inputFile.close();

return 0;
}



Write a Reply >>


 

    
 
 

  Topic Tools 
 
RSS UpdatesRSS Updates
 

  Related Articles 
 
 

  Newsletter 
 
A weekly newsletter featuring an editorial and a roundup of the latest articles, news and other interesting topics.

Please enter your email address below and click Subscribe.