How to cancel function and return to main.

I've tried searching the web first, but I can't find out the proper way to return to my main and repeat the program.

I know I have to use return but I don't know what to put after that. So far I've just been typing things like return (function name), return main() which has an error, and using just return ends the program. This function is in it's own header file if that means anything.

The 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

void Checkout()
{
	int NumberofDaystoRent;
	double RentalPrice = 1.50;
	double PrintTotal; 
	
	cout << "For how many days would you like to rent this game? " << endl;
	cout << "($1.50 per day, maximum of 7 days). Input (0) to cancel!" << endl; 
	cout << string( 2, '\n' );

	cin >> NumberofDaystoRent; 
	cout << string( 2, '\n' );

	if (NumberofDaystoRent == 0)
	{
		return; // WHAT GOES HERE?
	}

	while (NumberofDaystoRent < 1 || NumberofDaystoRent > 7)
	{
		cout << "ERROR: Invalid entry of days, please input 1-7" << endl; 
		cin.clear();
		cin.ignore(100, '\n');
		cin >> NumberofDaystoRent;
		cout << string( 2, '\n' );
	}

	PrintTotal = RentalPrice * NumberofDaystoRent;

		cout << "Subtotal: $" << PrintTotal; 
		cout << string( 2, '\n' );
}


Explanation: If the user decided to cancel (not rent a game) then I would like to cancel the function (prevent calculation) and return to the top of main / repeat the program.
Last edited on
nothing goes there. return returns a value, and if your function returns nothing (ie void) you just write return
But then my program just ends? I would like the user to return to main in case of wanting to rent / select something else.
Last edited on
no. the function ends.
Lies!

If I debug the program and press 0 to cancel, the next line is press any key to continue thus ending the program which I do not want to happen. )=
Are you using a loop in your main function?
I have a do while loop that repeats the program, a few while loops for input validation within my main, and a for loop counter that gets called to from a header file to main that outputs an array of class objects. Why?

From the function, I would like to return the user to here if possible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	
system("COLOR 70"); // CHANGE THE COLOR SCHEME OF THE CONSOLE.

int UserSelection; // TO HOLD GAME SELECTION DECISION WITHIN THE SWITCH MENU.
char YesorNo( '\0' ); // TO HOLD YES OR NO USER INPUT.

// LEAVE THE FUNCTION AND RETURN TO ABOUT HERE. 

do{ 
	PrintMenu(); // PRINT THE MENU HEADER.
	PrintArrayofClassObjects(); // DECLARE AND PRINT ARRAY OF CLASS OBJECTS.
	cout << "\nUse this menu to select a game to rent!" << endl;
	cout << "The available selections are 1-9. (0) to EXIT. \n" << endl; 

// REST OF PROGRAM CODE BELOW.....  


Screenshot: Bring the user here ->>> http://i.imgur.com/OhWUcum.png
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
27
#include <iostream>

void f()
{
    std::cout << "This is the start of f" << std::endl;
    int x;
    do
    {
        std::cout << "Blah blah blah 1... f" << std::endl;
        if((std::cout << "Type 1 to go back to main early: ") && (std::cin >> x) && x == 1)
        {
            return;
        }
        std::cout << "Blah blah blah 2... f" << std::endl;
    } while((std::cout << "Type 0 to go back to main: ") && (std::cin >> x) && x != 0);
    std::cout << "This is the end of f" << std::endl;
}

int main()
{
    std::cout << "This is the start of main" << std::endl;
    do
    {
        f();
    } while(std::cout << "This is the loop condition in main" << std::endl);
    std::cout << "This is the end of main" << std::endl;
}
Last edited on
Nevermind guys, thanks for the replies though. When I was returning from the function it was in fact going back to the main, but I thought it was ending because it was returning to a spot right before break; in a switch menu. Now I just have to set up some code to get the user back to the top of the program from this return point.

For anyone else that comes along this topic return; was in fact the simple / correct answer to leave a function. Just don't make the derp mistake I did and return right before something that stops your program such as break; unless that is your intention.
Last edited on
Topic archived. No new replies allowed.