adding a loop into a function and checking numbers

So I want to add my loop inside a function but I'm confused, is it right to place the variables localy inside the function or globally ?
I just need to use them within the program but I want to know what's right
also I wanted to check If the number 0 is typed in, an error message should appear. Also if the number 48 or greater is typed in, an error message should appear.

Also, The user should be able to display, correctly, the first 15 numbers in the series which I think the only thing I got it to work.

however, when I checked the program wouldn't return anything when I added
1
2
3
4
5
6
       if (num == 0 && num => 48)
				{
					cout << "Please enter a number between 1 and 48. ";

				}
	
continue;

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
#include <iostream>

using namespace std;
void myFunc(); // prototype 

int main()
{
    myFunc();
    return 0;
}


void myFunc()
{

    int n, t1 = 0, t2 = 1, nextTerm = 0; // do I place those varibles localy or globally 


    cout << "How Many Numbers You want to Display : "<<endl;

    cin >> n;


    cout << "The sequence is : ";


    for (int i = 1; i <= n; ++i)

    {
// I want to check if the number 0 is typed in, an error message should appear.
// also If the number 48 or greater is typed in, an error message should appear.
//Also The user should be able to display, correctly, the first 15 numbers //in the series.

    if(i == 1)

    {

    cout << endl << t1<<endl;

    continue;

    }

    if(i == 2)

    {

    cout << t2 <<endl;

    continue;

    }

    nextTerm = t1 + t2;

    t1 = t2;

    t2 = nextTerm;


    cout << nextTerm << endl;

    }

    return 0;

    }
Last edited on
Note I solved it using
1
2
3
4
5
6
 while(num == 0 || num >= 48){
        cout << "Please enter the right number in range\n";

        cin >> num;
    }


do you think the variables are in correct place?

Lastly I need help to rename the function

Create a function called, nextFibo(int, int), which will contain two arguments, Fn-1and Fn-2. The function will generate the next Fibonacci number in the sequence based on the two previous Fibonacci numbers given as arguments

.•The first two numbers in the sequence are: F0= 0 and F1= 1

•Hint: If you make the arguments in nextFibo() reference parameter variables, then you’ll shorten your program a bit.

any ideas?
Last edited on
Given Fn-1 and Fn-2, then Fn is simply (Fn-1) + (Fn-2). So

1
2
3
4
int nextFibo(int a, int b)
{
    return a + b;
}


but I suspect this is not what you really want. What are you really being asked to do? Generate a Fibonacci sequence?
hey seelplus now I get
Process returned 0 (0x0) execution time : 0.080 s
Press any key to continue.

nothing is wokring perty much !
I will explain everything
my task :
•Create a function called, nextFibo(int, int), which will contain two arguments, Fn-1and Fn-2. The function will generate the next Fibonacci number in the sequence based on the two previous Fibonacci numbers given as arguments

•The first two numbers in the sequence are: F0= 0 and F1= 1

•Hint: If you make the arguments in nextFibo() reference parameter variables, then you’ll shorten your program a bit.

•If the number 0 is typed in, an error message should appear.

•If the number 48 or greater is typed in, an error message should appear.

•The user should be able to display, correctly, the first 15 Fibonacci numbers in the Fibonacci series.


my code


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

//This program is a Fibonacci sequence program
#include <iostream>
using namespace std;
void nextFibo(int , int ); //prototype function 


int main()
{

    void nextFibo(int , int ); // declaring
    return 0;
}
void nextFibo() // this is the function
{
        int num1, t1 = 0, t2 = 1, newT = 0;

    cout << "How many num1bers in the  Sequence do you want to display? ";
    cin >> num1;
    cout << "The  sequence is : ";


    for (int i = 1; i <= num1; ++i)
    {

        if(i == 1)
      
        {
            cout << endl << t1<<endl;
            continue;
        }

        if(i == 2)

        {
            cout << t2 <<endl;
            continue;
        }
        newT = t1+ t2;
        t1 = t2;
        t2 = newT;
  
        cout << newT << endl;
    }
}




Last edited on
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
#include <iostream>

int nextFibo(int& x, int& y)
{
	const int z = x + y;

	x = y;
	y = z;

	return z;
}

int main() {
	int num;

	while ((std::cout << "Enter how many numbers are required in the sequence (1 - 47): ") && (!(std::cin >> num) || ((num < 1) || num >= 48))) {
		std::cout << "Invalid number\n";
		std::cin.clear();
		std::cin.ignore(1000, '\n');
	}

	std::cout << "\nThe Fibonacci series : ";

	for (int i = 0, x = 0, y = 1; i < num; ++i)
		std::cout << (i < 2 ? i : nextFibo(x, y)) << " ";
}



Enter how many numbers are required in the sequence (2 - 47): 15

The Fibonacci series : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Last edited on
Your code and solution is great!! , how ever I need to have a function nextFibo() to be like the format I did the function has to hold most of the code while the main() has few, lastly the output has to be vertical "\n"

I don't want to use std::
as well as cont int

Topic archived. No new replies allowed.