1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ column and inputting file help?

Discussion in 'C++' started by kayliu, Apr 26, 2009.

  1. kayliu

    kayliu New Member

    Joined:
    Apr 26, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    How can I be able to read integers from more than one file? and how to display each integer in a field width of 5, with a new line after each of 5 integers? For example,
    0 1 2 3 4
    5 6 7 8 9
    10 11 12 13.

    My mission is to write a program that can read integers from user input files, display the data, and display the largest and smallest integers in the data set.

    Please share the codes, thanks.

    Here is what I have done so far
    Code:
    #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;
    }
     
    Last edited by a moderator: Apr 26, 2009
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What does the program not do that you want it to?
    What input did you give, what did the file contain, and what was the result?
    How did the result differ from what you wanted?
     
  3. kayliu

    kayliu New Member

    Joined:
    Apr 26, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    > I do not know the codes that ask the users to input the file

    What's this then?
    Code:
    	cout << "System >> Enter file to read: \n";
    	cout  << "User >> ";
    	cin >> GetFileName;
    
    To put a line break after each 5 numbers you could use a pair of nested for loops, or you could use a separate variable that is incremented each time round the loop, and when it exceeds a certain value display an endl and reset the variable, i.e.
    Code:
    r=0
    for i=0..15
      display number[i]
      r++
      if (r>4)
        display endl
        r=0
      end if
    end for
    
     

Share This Page