Trouble with looping program for multiple user inputs.

Hello! I am having trouble figuring out how to loop the program so that the user may enter more Fibonacci values instead of ending the program after the first series. Where am I going wrong?

Thanks in advance!


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
  #include <iostream>
using namespace std;

bool repeat()
{
int n;
int y; 
int redo;

	if(redo==y)
	{
		return true;
	}
	if(redo==n)
	{
		return false;
	}
		
}
int main()
{
    int num3=0; 
	int num1=1; 
	int num2=2; 
	int fibnumber;
	int redo;
	
		cout<<"The 1st Fibonacci number is 1. ";
		cout<<"\nThe 2nd Fibonacci number is 2. ";
		cout <<"\nWhich other Fibonacci number would you like? ";	
		cin >> fibnumber;

	while (fibnumber>=1)
		{
			
		cout << num1 ;
		num3 = num1 + num2;
		num1 = num2;
		num2 = num3;
		(fibnumber--);
		cout <<" "<<endl;
		
		}
		
	cout<<"\nWould you like to know another? 'y' for yes, or 'n' for no."<<endl;
	cin>>redo;
	
	
    return 0;
}
You are close. All you need is to add an outer while loop to ask the question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char  redo = 'y';
while (redo == 'y')
{
    while (fibnumber>=1)
		{
			
		cout << num1 ;
		num3 = num1 + num2;
		num1 = num2;
		num2 = num3;
		(fibnumber--);
		cout <<" "<<endl;
		
		}
		
	cout<<"\nWould you like to know another? 'y' for yes, or 'n' for no."<<endl;
	cin>>redo;
}
Last edited on
The inner loop will not repeat tho because fibnumber will be less than 1

helpvas.cpp: In function ‘int main()’:
helpvas.cpp:35:7: error: conflicting declaration ‘char redo’
char redo = 'y';
^

helpvas.cpp:26:6: error: ‘redo’ has a previous declaration as ‘int redo’
int redo;
^
helpvas.cpp:55:1: error: expected ‘}’ at end of input
}
^
No dice trying to add an outer loop, i also tried changing the if (redo = n) statement and taking out the n variable, but still no progress.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool repeat()
{
char redo = 'y';
while (redo == 'y')
int n;
int y; 


	if(redo==y)
	{
		return true;
	}
	else;
	{
		return false;
	}
		
} 
Last edited on
Here's a working version:

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
#include <iostream>
using namespace std;

int main()
{
   char redo = 'y';
   while ('y' == redo)
   {
           unsigned long long int num3=0; 
	   unsigned long long num1=1; 
	   unsigned long long num2=2; 
	   short int fibnumber;
	
		   cout << "The 1st Fibonacci number is 1. " << endl
		        << "The 2nd Fibonacci number is 2. " << endl
		        << "Which other Fibonacci number would you like? ";	
		   cin >> fibnumber;

	   while (fibnumber>=1)
		{	
		   cout << num1 ;
		   num3 = num1 + num2;
		   num1 = num2;
		   num2 = num3;
		   fibnumber--;
		   cout <<" "<<endl;
		}
		
      cout << endl << "Would you like to know another? [y\\n]: ";
	   cin >> redo;
   }

   return 0;
}
Last edited on
Oh i get it now. Thank you so much you are the best!
you're welcome. You have to be careful of fib numbers. They get big very fast. That's why I changed from int to unsigned long long int. Even still, you need to set an upper limit. That is, if the user enters something greater than 60, ask the user to enter a value smaller than 60.
Yeah i noticed that, also the program makes much more sense now it's a lot less cluttered.

Thank you once again.
Topic archived. No new replies allowed.