Question about process

I have an assignment to work on that I am not sure how to complete, and the instructor isn't giving me any help to explain what exactly he wants(frustrating). Essentially I am asking for some of your help to see how you would tackle this program. I need to write a program that reads an integer number from a data file, and calculates the Fibonacci number of that number, and prints the number and corresponding Fibonacci number, then repeat this process until it reaches the end of data file, also I need a recursive version and non recursive version for the program. I have written the code that takes the users filename and displays the contents of the file, I am just wondering how to do the recursion calculation for the Fib. numbers. Thanks for any and all help on this one. Here is the code I have for user input.

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <stdexcept>
#include <exception>

using namespace std;

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

	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))
			{
				cout << line << '\n';
			}
			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 now written the functions for the recursive and non recursive functions to return the corresponding Fib. numbers, my question now is how do I get the input to go into the functions and return the Fib. number?

Recursion
1
2
3
4
5
6
7
8
int recursiveFib(int n)
{
	if (n <= 1)
	{
		return n;
	}
	return recursiveFib(n - 1) + recursiveFib(n - 2);
}


NonRecursion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
}
Last edited on
I have figured that with the stringstream I am able to read in the information
Topic archived. No new replies allowed.