Can I ???!

Hello everyone

I have a question for the experienced C++ members.

Can I jump from a function (exm: int getInt()) to any place in the program?


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
int getInt()
{
	int n;
	char ch;

	while (true)
	{
		cin  >> n;

		if (cin.good())
		{
			cin.ignore(20, '\n');
			break;
		}// break and return n value

		cin.clear();
		
		do // if the input is not a number
		{
			cin.ignore(20, '\n');
			cout <<"\n!!!Incorrect input."<< endl;
			cout <<"Enter Y to continue or N to exit: ";
			cin  >> ch;
			cout << endl;
			if (ch == 'N' || ch == 'n')
			{
				break;// !!?? Here I wonder if i can jump to anothe function 
                                // like the main. or i have a menu display function void menuChoice()
			}

			cout << "TEST";
			cin  >> n;
			return n;
			
		} while (ch == 'Y' || ch =='y');
		cin.ignore(20, '\n');	
	}

	return n;
}


I hope it is somewhat clear.

Last edited on
your code seems to complex for what you are trying to do:

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

int getInt()
{

   int number;
   int character;

   do
   {
          character = (char)0;
          
          cout << "Please Enter a number: ";

          cin >> number;
       
          if(!cin.good())
          {
                 cout << "You didnt' enter a number!!" << endl;
          }
          else
          {
                 cout << "Would you like to continue?"
                 cin >> character;
          }

     } while( (character != 'N") && (character != 'n') )
    
     return n;
          
}
 


hope this helps!
Last edited on
closed account (D80DSL3A)
Did you mean "Can I return from any place I like in a function?". It looks like you would like to return n at line 13 when it is found that the value entered is good.
Just replace line 13 with return n; instead of break; You can return from multiple points in a function, not just at the end.
Azagaros (196)

When the answer is N, I have to leave this function and go to another function that displays the menu.
P.S. Even though this function getInt() was not called there.

So can I leave a function and go to any other function in the program ather then the main.

exit(main()// I know if you do this it will take you main
exit(main()// I know if you do this it will take you main


That is incorrect.

http://www.cplusplus.com/reference/clibrary/cstdlib/exit/
Simply Call that function over there and set a return <something_to_how_non_regular_exit> ; after it?
Last edited on
1
2
3
4
5
6
7
8
Moschops (1384) // If the user enters no I leave the function

if (something)
{
    //smth .....
   exit (main());// this takes me to the main and this function is not called there.

}


1
2
3
Nisheeth (474) 

return <something_to_how_non_regular_exit> ;// I am not sure what this means 
Moschops (1384) // If the user enters no I leave the function


exit(main()) should not even compile. What compiler are you using?
I mean like this:
1
2
3
4
5
6
7
8
9
10
int func()
{
      //...    
      if (x = 'N')
      {
            func2(); //The function you want to go to (Don't call main!!!)
            return 0; // You can change 0 to something other value which can't be returned by your function
      }      
      //...
}


If the function is main, don't call any function at all!
Last edited on
Thanks everyone for the time and help.

Moschops: I do not know what compiler it is. I am using C++ on VISUAL STUDIO 2008.
Moschops: I do not know what compiler it is. I am using C++ on VISUAL STUDIO 2008.

You're using some version of the Microsoft C++ compiler. If anyone ever asks again, just saying you're using VS 2008 will do.
Even though you can't invoke main in the C++ standard land, I found that Visual Studios C++ will let you. So:
1
2
3
4
exit(main()); //is pretty much like
return main(); //if your in main
//That is "exit(main());" is non-standard/non-supported recursion (direct or indirect)
// that may not (should not) compile on all C++ compilers, and has undefined behavior if it does. 
(Don't do it!)
Last edited on
Topic archived. No new replies allowed.