Need help sorting file to arrays
Apr 11, 2015 at 2:25am UTC
HI, I am having a bit of trouble, I am doing an assignment on statistical hypothesis testing. I can do all the calculations on paper but am having trouble with reading from a file to an array.
Here are the numbers I need.
14
0.5923 0.58 0.6008 0.6117 0.6295 0.6088 0.6148 0.6369 0.6372 0.6241 0.6034 0.6108 0.591 0.601
109.31 110.13 108.9 108.96 108.86 109.17 109.29 108.62 109.06 108.61 109.83 110.48 111.53 111.02
I want to define the array size to be 14 from the first line then two seperate arrays for the first 14 digits and the second 14. IF that makes sense.
At the moment I can only get it to read in all the data to end of file. I need it to read each to end of line I think?
Any help would be much appreciated.
Here is what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include "std_lib_facilities.h"
#include <cctype>
#include <iostream> // library that contain basic input/output functions
#include <fstream> // library that contains file input/output functions
using namespace std;
int main()
{
int array_size = 1024;
char * array = new char [array_size];
int position = 0;
ifstream fin("input1.txt" ); //opening an input stream for file
if (fin.is_open())
{
cout << "File opened" << endl;
while (!fin.eof() && position < array_size)
{
fin.get(array[position]); //reading one character from file to array
position++;
}
array[position-1] = '\0' ; //placing character array terminating character
cout << "Array is below" << endl << endl;
//this loop display all the characters in array till \0
for (int i = 0; array[i] != '\0' ; i++)
{
cout << array[i];
}
}
else
{
cout << "File could not be opened." << endl;
}
return 0;
}
Apr 11, 2015 at 2:34pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
int main()
{
int array_size; //don't know what this is yet
double * array1; //because the numbers you're reading are doubles
double * array2;
ifstream fin("input.txt" ); //opening an input stream for file
if (fin.is_open())
{
cout << "File opened" << endl;
cin >> array_size;
array1 = new double [array_size]; //allocate memory for arrays
array2 = new double [array_size];
for (int i = 0; i < array_size; ++i) //fill array1
cin >> array1[i];
for (int i = 0; i < array_size; ++i) //fill array2
cin >> array2[i];
cout << "Arrays are below" << endl << endl;
for (int i = 0; i < array_size; ++i)
cout << array1[i] << ' ' ;
cout << endl;
for (int i = 0; i < array_size; ++i)
cout << array2[i] << ' ' ;
}
else
{
cout << "File could not be opened." << endl;
}
return 0;
}
Apr 11, 2015 at 6:29pm UTC
Thank you so much, This really helped!!! :)
Apr 11, 2015 at 7:58pm UTC
The numbers are already contained in the text file, this program seems to be looking for the user to enter the array details manually.
I want it to read directly from the input1.txt to fill the data for the array size and array1 and array2?
Is it a case of adding in a getline command?
Apr 12, 2015 at 4:17am UTC
I accidentally used cin
instead of fin
. It's a very easy mistake to spot. Did you even look at the code?
Topic archived. No new replies allowed.