Output from File I\O

I have followed all of the steps and completed my work, but I don't know how to get the sample output for this. My code successfully takes the file from the user selection reads in the numbers and returns both recursively and non-recursively the Fib. number in the sequence. My sample output needs to be in form:

Recursive
N Fib(N)
3 2
5 5
1 1
-5 0
6 8

Non-Recursive
N Fib(N)
3 2
5 5
1 1
-5 0
6 8

I have no clue how to format the data in the output to follow this form, thanks for any and all help ahead of time. I will post my code thus 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <exception>

using namespace std;

int recursiveFib(int n)
{
	if (n <= 1)
	{
		return n;
	}
	return recursiveFib(n - 1) + recursiveFib(n - 2);
}

int nonRecursiveFib(int n)
{
	int a = 0;
	int b = 1;
	int c;
	int i;

	if (n == 0)
	{
		return a;
	}

	for (i = 2; i <= n; i++)
	{
		c = a + b;
		a = b;
		b = c;
	}
	return b;
}

int main()
{
	string fileName;
	string line;
	int number;

	while (true)
	{
		cout << "Enter a file name <in the same file as your executable>: ";
		cin >> fileName;
		ifstream dataFile(fileName.c_str());
		if (dataFile.good())
		{
			while (getline(dataFile, line))
			{
				stringstream sstr(line);

				if (!(sstr >> number))
				{
					cout << "\nUnable to parse input data!";
					cout << endl;
					system("PAUSE");
					return -1;
				}
				else
				{
					cout << "\nInteger N:		" << number << "\n";
					int recNum = recursiveFib(number);
					int nonRecNum = nonRecursiveFib(number);
					cout << "Recursive Fib(N):	" << recNum << endl;
					cout << "Non-Recursive Fib(N):	" << nonRecNum << "\n" << endl;
				}
			}
			dataFile.close();
			break;
		}
		if (!dataFile.good())
		{
			cout << "\nNo file exists with that name, please enter a valid file name.";
		}
		cout << endl;
	}
	system("PAUSE");
	return 0;
}
Last edited on
I have tried something like this

1
2
3
4
5
6
					cout << "Recursive" << "\n";
					cout << "N" << "	Fib(N)" << "\n";
					cout << number << "		" << recNum << "\n";
					cout << "Non-Recursive" << "\n";
					cout << "N" << "	Fib(N)" << "\n";
					cout << number << "		" << nonRecNum << "\n";


But this just gives me everything on a repeated line instead of just displaying the list.
To get the exact output style you described I had to restructure the code a bit. Since you're displaying results for both functions in different groups you can't interleave the output on the fly.

You could store all the output as strings and manipulate it later on, but I chose not to try that.

I've instead stored the input in a vector and applied both functions one at a time, to the entire vector contents.

I had to add the header for vector, and restructure slightly so the program doesn't exit prematurely

For this output:
Recursive
N F(N)
3 2
5 5
1 1
-5 -5
6 8

Non-Recursive
N F(N)
3 2
5 5
1 1
-5 1
6 8

The complete code for main() is below:

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
65
66
67
68
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <exception>
#include <vector>

using namespace std;

int main()
{
	string fileName;
	string line;
	int number;

	while (true)
	{
		cout << "Enter a file name <in the same file as your executable>: ";
		cin >> fileName;
		ifstream dataFile(fileName.c_str());
		if (dataFile.good())
		{
			vector<int> numbers;
			while (getline(dataFile, line))
			{
				stringstream sstr(line);

				if (!(sstr >> number))
				{
					cout << "\nUnable to parse input data!";
					cout << endl;
					system("PAUSE");
					break;
				}
				else
				{
					numbers.push_back(number);
				}
			}

			cout << endl << "Recursive" << endl;
			cout << setw(5) << "N" << setw(5) << "F(N)" << endl;
			for (auto num : numbers)
			{
				cout << setw(5) << num << setw(5) << recursiveFib(num) << endl;
			}

			cout << endl << "Non-Recursive" << endl;
			cout << setw(5) << "N" << setw(5) << "F(N)" << endl;
			for (auto num : numbers)
			{
				cout << setw(5) << num << setw(5) << nonRecursiveFib(num) << endl;
			}

			dataFile.close();
			break;
		}
		if (!dataFile.good())
		{
			cout << "\nNo file exists with that name, please enter a valid file name.";
		}
		cout << endl;
	}
	system("PAUSE");
	return 0;
}


Alternatively, consider a smaller change for a different kind of output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
		if (dataFile.good())
		{
			cout << endl << setw(16) << "Integer N" << setw(16) << "recursiveFib" << setw(16) << "nonRecursiveFib" << endl;
			while (getline(dataFile, line))
			{
				stringstream sstr(line);

				if (!(sstr >> number))
				{
					cout << "\nUnable to parse input data!";
					cout << endl;
					system("PAUSE");
					return -1;
				}
				else
				{
					cout << setw(16) << number << setw(16) << recursiveFib(number) << setw(16) << nonRecursiveFib(number) << endl;
				}
			}
			dataFile.close();
			break;
		}


You get the idea...
Topic archived. No new replies allowed.