How to go to another function

Hello,

I want to run another function after an if statement. The problem is the function I want to go to (main() ) is declared after it. Is there a possible work around?

1
2
3
4
5
6
7
8
    cout << "Would you like to complete another transaction? (y/n)" << endl;
    cin >> choice2;
        if (choice2=="y")

        // THIS iS WHERE I WANT TO START MAIN() 
        
        if (choice2=="n")
            return(0);


Thanks in advance.



Here is the full code for reference.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>

using namespace std;
double b;
string choice2;

int withdraw() {
    double w=0;
    cout << "How much would you like to withdraw?" << endl;
    cout << "Withdraw: ";
    cin >> w;
    if (w>b) {
        cout << "Insuffecient funds" << endl;
        cout << "Try again" << endl;
        cin >> w;
    }
    b=b-w;
    cout << "Your balance is now $" << b << endl;
    cout << endl;

    cout << "Would you like to complete another transaction? (y/n)" << endl;
    cin >> choice2;
        if (choice2=="y")

        // THIS iS WHERE I WANT TO START MAIN() 
        
        if (choice2=="n")
            return(0);

}

int deposit() {
    double d=0;
    cout << "How much would you like to deposit?" << endl;
    cout << "Deposit: ";
    cin >> d;
    if (d<0) {
        cout << "Can't be negatives!" << endl;
        cout << "Try again!" << endl;
        d=d*-1+d;
        cout << "Deposit: ";
        cin >> d;
    }
    b=d+b;
    cout << "Your balance is now $" << b << endl;
    cout << endl;
    cout << "Would you like to complete another transaction? (y/n)" << endl;
    cin >> choice2;
        if (choice2=="y")

        // THIS IS WHERE I WANT TO START MAIN

        if (choice2=="n")
            return(0);
}
int main() {
    string choice;
    cout << "Hello and welcome to CS Bank!" << endl;
    cout << "What is your balance?" << endl;
    cin >> b;
        if (b<0) {
            cout << "Can't be a negative!" << endl;
            cout << "Try again!" << endl;
            cin >> b;
        }

        if (!( b > 0 || b < 0)) {
            cout << "Not a valid number" << endl;
            cout << "Balance: " << endl;
            cin >> b;

        }



    cout << "Would you like to withdraw, deposit or view balance?" << endl;
    cin >> choice;

    if (choice== "withdraw")
        withdraw();
    if (choice=="deposit")
        deposit();
    if (choice=="balance")
        cout << "Balance: $" << b << endl;
    if (choice!="withdraw" && choice!="deposit" && choice != "balance") {
        cout << "Incorrect input!" << endl;
    
    }
}
First of all you should always avoid using global variables. Second you can pass the balance by reference to the functions in order that they sustain the changes. Next you can do what you want in the main with a character and a do-while loop. The code will be something like this.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>

using namespace std;

void withdraw(double& b) {
    double w=0;
    cout << "How much would you like to withdraw?" << endl;
    cout << "Withdraw: ";
    cin >> w;
    if (w>b) {
        cout << "Insuffecient funds" << endl;
        cout << "Try again" << endl;
        cin >> w;
    }
    b=b-w;
    cout << "Your balance is now $" << b << endl;
    cout << endl;
}

void deposit(double& b) {
    double d=0;
    cout << "How much would you like to deposit?" << endl;
    cout << "Deposit: ";
    cin >> d;
    if (d<0) {
        cout << "Can't be negatives!" << endl;
        cout << "Try again!" << endl;
        d=d*-1+d;
        cout << "Deposit: ";
        cin >> d;
    }
    b=d+b;
    cout << "Your balance is now $" << b << endl;
    cout << endl;
}


int main() {
    string choice;
    double b;
    char again;
    cout << "Hello and welcome to CS Bank!" << endl;
    cout << "What is your balance?" << endl;
    cin >> b;
        if (b<0) {
            cout << "Can't be a negative!" << endl;
            cout << "Try again!" << endl;
            cin >> b;
        }

        if (!( b > 0 || b < 0)) {
            cout << "Not a valid number" << endl;
            cout << "Balance: " << endl;
            cin >> b;

        }


    do
    {
    cout << "Would you like to withdraw, deposit or view balance?" << endl;
    cin >> choice;

    if (choice== "withdraw")
        withdraw(b);
    else if (choice=="deposit")
        deposit(b);
    else if (choice=="balance")
        cout << "Balance: $" << b << endl;
    else {
        cout << "Incorrect input!" << endl;
    cout << "Would you like to complete another transaction? (y/n)" << endl;
    cin >> again;
    while(!(again == 'Y' || again == 'N'))
    {
        cout << "Enter your choice again" << endl;
        cin >> again;
    }
    }
    }while (again == 'Y');
}
Last edited on
either that, or just call it recursively
Topic archived. No new replies allowed.