Christian Laverdure
Christian Laverdure

Reputation: 13

Loop Functions C++

I've written a code to use a for loop to display all the even numbers with in the range based on the parameters inputted by the user. However, I'm a little confused as to why the i < b in the for statement still outputs b as the final number in the loop being that it isn't and <= symbol. Also if a is an even number it also populates in the output. I've tried using if (i % 2 == 0) && (i > a) as well as if (i % 2 == 0) && (i > a) with no avail.

Here is the code:

#include<iostream>
#include<string>
using namespace std;

//Loop function to display even numbers within a range
int print_even(int a, int b) {

    int i;
    for (i = a; i < b; i++) {
        if (i % 2 == 0)
            cout << i << " ";
    }
    return i;
}

//main function 
int main() {
    int a, b;
    cout << "Write the initial number" << endl;
    cin >> a;
    cout << "Write the ending number" << endl;
    cin >> b;

    cout << "These are all the even integers between the initial and ending numbers" << endl;
    cout << print_even(a,b);    

    return 0;
}

Here is the output:

Write the initial number
2
Write the ending number
60
These are all the even integers between the initial and ending numbers
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60

Any feedback would be greatly appreciated!

Upvotes: 0

Views: 484

Answers (2)

Kfir Ventura
Kfir Ventura

Reputation: 272

The problem is that you print the return value of print_even, which is 60 when the loop exits.

Just call print_even(a,b); without cout.

Upvotes: 2

iSythnic
iSythnic

Reputation: 149

As some other answers have stated, the problem within your code is that you have printed out the return value of the function: print_even.

The function of a for loop is made so that at the end of every iteration, the variable within the incrementation part of the for loop is incremented. After the incrementation, the condition is checked. Using your code as an example, for the 59th iteration of the for loop the variable i will be incremented to 60 and the condition will be checked, i < b which will return false, therefore ending the loop.

You declared i outside the for loop scope therefore i will not be destroyed at the end of the for loop; storing 60 within the i variable. At the end of your function you have return i, which will return 60. If we look in int main(), you have done std::cout << print_even (a,b). This means when your function is finished 60 is returned and inserted into std::cout therefore printing 60 into console.

I suggest you should create the print_even function with the return type of void, which means that nothing will be returned if you don't plan on using the i variable anywhere else in your code.

The code below is a fixed up version of your code, which would fix up your problem:

#include<iostream>
#include<string>
using namespace std;

//Loop function to display even numbers within a range
void print_even(int a, int b) {

    for (int i {a}; i < b; i++) {
        if (i % 2 == 0)
            cout << i << " ";
    }
}

//main function 
int main() {


    int a, b;


        cout << "Write the initial number" << endl;
        cin >> a;
        cout << "Write the ending number" << endl;
        cin >> b;

        cout << "These are all the even integers between the initial and ending numbers" << endl;
        print_even(a,b);    

        return 0;

}

If you need any more details on functions and return types use the resources below:

https://en.cppreference.com/w/cpp/language/return - return statement

https://www.learncpp.com/cpp-tutorial/function-return-values/ - functions and return types

https://www.geeksforgeeks.org/functions-in-c/ - functions

Upvotes: 1

Related Questions