Number Calculations using file I/O, arrays, and strtok

Alright, so I have a text file titled 'Input.txt." The text file contains a variable number of lines of information (for the sake of this, lets say 10). I need to read each line from the file into individual lists or arrays.

Once all the numbers have been read in, I need to calculate the average, median, and mode of the numbers contained within each list or array. The averages, medians, and modes need to be printed out with the corresponding line to a text file called "Results.txt."

I'm attempting to use strtok with for loops and while loops. I'm not sure if this is the best way to do it. I've been able to read in all lines and output all lines, but where I'm having difficulty is doing operations on a single line. My code so far is as follows:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

#include <iostream>
#include <math.h>
#include <fstream>
#include <string>
using namespace std;

int choice;
string line;
int main()
{
	cout << "Would you like to import 'input.txt' ? (1)" << endl;
	cin >> choice;
	if (choice==1)
	{
		cout << "Attempting to import 'input.txt'" << endl;
		ifstream input ("input.txt"); //begins reading from input

		if(!input.is_open()) //if file could not be read
			{
			cout << "Import Failed!" << endl;
			cout << "File could not be opened" << endl;
			cin.get();
			cin.get();
		}
			cout << "Input file is open\n" << endl;
			ofstream results;
			results.open("results.txt"); //creates 'results.txt'
			char str[] = input;
			char * pch;
			pch=strtok (str, "\n");
			while (pch !=NULL)
			{
				while (input.good()) //while input file is open, do this
				{
			

				
					getline (input, line);
					cout << line << " " << endl;
					results << line << endl;
					pch = strtok (NULL, "\n");
				}

				

			//calculate mean
			//calculate median
			//calculate mode
				cout << "\n Closing 'input.txt'." << endl;
				input.close(); //closes inputs
				cout << "'input.txt' has been closed." << endl;
				cout << "Closing 'results.txt'." << endl;
				results.close(); //closes results
				cout << "'results.txt' has been closed." << endl;
				cin.get();
				cin.get();
			
	}else{ //from choice 
	cin.get();
	cin.get();
	return 0;
	}
}
I'm attempting to use strtok with for loops and while loops. I'm not sure if this is the best way to do it.
It's pretty much the worst

You can easily parse the line with stringstream:

http://www.cplusplus.com/reference/iostream/stringstream/stringstream/
Topic archived. No new replies allowed.