Code not continuing after void while function

Hi,
I'm having trouble as to why my code continues after my while loop when the loop is placed in main, but when I place the loop in a void function, if the while loop fails and is then successful in a subsequent loop, my code terminates?

The while loop is meant to be used as a input limiter

Here is a snippet of 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

#include <iostream>
#include <iomanip>

using namespace std;

 void validFive(int x)
	{
		while (!(x == 1 || x == 2 || x == 3 || x == 4 || x == 5))
		{
			cout << "Incorrect input, please pick from the provided options \n";
			cin.clear();
			cin.ignore(10000, '\n');
			cin >> x;
		}
	} 

int main()
{
	int firstOption;
        int selection;
	
        cout << "Enter a number";
        cin >> selection;
        cout << "Enter a number between 1 & 5";
	cin >> firstOption;
	validFive(firstOption);

if (selection == 1) // Metric to Imperial
	{
		switch (firstOption)
		{
		case 1: // Length
                cout << "Yes";
                }
        }
return 0;
}


Yet this works fine
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>
#include <iomanip>

using namespace std;

int main()
{
	int firstOption;
        int selection;

	cout << "Enter a number";
        cin >> selection;
        cout << "Enter a number between 1 & 5";
	cin >> firstOption;

while (!(firstOption == 1 || firstOption == 2 || firstOption == 3 ||firstOption == 4 || firstOption== 5))
		{
			cout << "Incorrect input, please pick from the provided options \n";
			cin.clear();
			cin.ignore(10000, '\n');
			cin >> firstOption;
		}

if (selection == 1) // Metric to Imperial
	{
		switch (firstOption)
		{
		case 1: // Length
                cout << "Yes";
                }
        }
return 0;
}
Last edited on
If you exited the loop, your function is over, and there are no more instructions in your program, so it's also over. Why would it do anything but that?

Perhaps show an example of code that does what you expect, and code that doesn't do what you expect.

Edit: you edited your OP after I made this post.
Edit: You also edited your OP again after Repeater's post. Just in case other readers are confused.
Last edited on
There is an 'if' statement I have left out after the call for the function which should be executed, but doesn't


Well there's the problem. If you leave code out, then it can't be run. You have to write all the code you want into your program.
Perhaps show an example of code that does what you expect
You have to write all the code you want into your program.


I intentionally left out the code (if statement) as I would have to include several other lines of code for comprehension but I have now edited it to briefly show what I'm trying to get at.

The second block of code continues to run the if statement after the while loop has exited, while the first block of code exits completely after the while loop.

It's a snippet of a Unit conversion program
Last edited on
You are getting data input in your function, you need to return that back to main
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
#include <iostream>

int func()
{
   std::cout << "Input a number between 1 & 5: ";

   int input { };

   std::cin >> input;

   while (input < 1 || input > 5)
   {
      std::cout << "Incorrect input, re-enter: ";
      std::cin.ignore(10000, '\n');
      std::cin >> input;
   }

   return input;
}

int main()
{
   int selection = func();

   if (selection == 1) // Metric to Imperial
   {
      switch (selection)
      {
      case 1: // Length
         std::cout << "Yes\n";
      }
   }
}
In the code where you have an extra function, x is a copy of the variable passed from main.

If you need to the change to be reflected back to main, you need to pass by reference.

1
2
3
4
void validFive(int& x)
{
	// ...
} 


Also, please use consistent indentation. It make it so much harder to read when you don't.
You are getting data input in your function, you need to return that back to main


If you need to the change to be reflected back to main, you need to pass by reference.


Thank you, that seems to have fixed it

please use consistent indentation.


Will do!
Topic archived. No new replies allowed.