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;
}
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;
}
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.
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
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
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!
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.