Well i have finally sorted out my program so it will put numbers into an array, and also output them to a text file.
The problem is it only inputs 1 set of numbers!
Code:
#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;
bool getInputFilename(char fname[])
{
ifstream fin;
cout << "Please enter the filename for input : ";
cin >> fname;
fin.open(fname, ios::nocreate);
if (!fin.is_open())
return false;
fin.close();
return true;
}
bool getOutputFilename(char fname[])
{
ofstream fout;
cout << "Please enter the filename for output : ";
cin >> fname;
fout.open(fname);
if (fout.fail())
return false;
fout.close();
return true;
}
void firstArray(ifstream& fin, int marks [], int arraySize)
{
while (!fin.eof()) {
for (int i = 0; i < 10; i++) {
fin >> marks[i];
}
}
}
void main()
{
ifstream fin;
ofstream fout;
char IFname[20], OFname[20];
int marks [10];
while (!getInputFilename(IFname))
{
cout << "Invalid filename try again!\n\n";
}
while (!getOutputFilename(OFname))
{
cout << "Invalid filename try again!\n\n";
}
fout.open(OFname);
fin.open(IFname);
firstArray(fin,marks,10);
for(int i = 0; i < 10; i++)
fout << marks[i] << endl;
fout.close();
fin.close();
}
The test file i used i just contained 10 numbers, however the text file data i want to use contains:
name //can be 3 words seperated by spaces
10 numbers // all ints
so eg:
Bla Bla Bla
13 18 38 49 58 69 94 85 48 95
Bleh Bleh Bleh
83 47 38 49 03 27 45 48 04 58
How would i change my code so it can output data in the form of:
name - average of first 5 numbers - average of last 5 numbers
name - average of first 5 numbers - average of last 5 numbers