Function Help

Good Evening,

I am brand new to C++, I decided to start learning it because I have always had an interest in programming and now that my BS is done I have some spare time. I am having some trouble with functions and have been beating my head against the wall for a few days trying to figure out where I am making my mistakes. I completely understand the purpose of functions I just can't seem to grasp how to use them properly.

This program is a really basic program just to test my use of functions. It has a calculator, and a loop which sings 99 bottles of beer. I've looked through a bunch of different books, websites, and forums, and as far as I can tell it feels like I am doing the functions right, but apparently I am not because I can't get this to compile.

Any advice anyone could give would be greatly appreciated. Thanks in advance!

-Steve

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  #include <iostream>
#include <string>

using namespace std;

void calc()
{
    float x;
    float y;
    int funchoice;

    for (int i = 1; i > 0; i++)
    {

    cout << "Please Select a Function" << endl << "1. Addition, 2. Subtraction, 3. Multiplication, 4. Division, 5. Exit" << endl;
    cin >> funchoice;
    cout << "Please Enter Two Arguments" << endl;
    cin >> x >> y;


    if (funchoice == 1)
    {
            cout << x << " + " << y << " = " << x + y << endl;
            i = 1;

    }

    else if (funchoice == 2)
    {
        cout << x << " - " << y << " = " << x - y << endl;
        i = 1;
    }
    else if (funchoice == 3)
    {
        cout << x << " * " << y << " = " << x * y << endl;
        i = 1;

    }
    else if (funchoice == 4)
    {
        if (x != 0)
        {
        cout << x << " / " << y << " = " << x / y << endl;
        i = 1;

        }
        else if (x == 0)
        {
            cout << "Division By Zero Will End The Universe" << endl;
            i = 1;

        }
    }

    else if (funchoice == 5)
    {
        cout << "You have chosen to exit" << endl;
        i = 0;

    }
}
}

void beer()
{
    for (int i = 99; i > 0; )
    {
        cout << i << " Bottles of beer on the wall " << i << " Bottles of beer! \nTake one down, pass it around, " << i << " bottles of beer on the wall!" << endl;
        i = i - 1;
    }
}

int menu()
{
     for (int i = 2; i > 1; i++)
    {
        int userinput;
        cout << "Welcome to the Main Menu \nPlease Select an Option" << endl;
        cout << "1. Calculator" << endl << "2. Beer Song" << endl << "3. Password Loop" << endl << "4. Exit" << endl;
        cin >> userinput;

        if (userinput == 1)
        {
            cout << "You Have Chosen to Run the Calculator!" << endl;
            return 1;

        }
        else if (userinput == 2)
        {
            cout << "You Have Chosen to Sing 99 Bottles of Beer on the Wall!" << endl;
            return 2;

        }
       
        //These next two options were going to be implemented just to test my 
        //knowledge of different forms of loops but I choose not to implement it
        //until I could figure out why the functions weren't working properly
        else if (userinput == 3)
        {
            cout << "You Have Chosen to Run the Password Loop" << endl;

        }
        else if (userinput == 4)
        {
            cout << "You Have Chosen to Exit the Menu" << endl;
            i = 0;

        }

        else if(userinput < 1 || userinput > 4)
        {
            cout << "Invalid Entry" << endl;

        }
    }
}

int main()
{
    int menuchoice;
    menu();
    menuchoice = menu();

    if(menuchoice == 1)
    {
        calc();
    }
    if else(menuchoice == 2)
    {
        beer();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    int menuchoice;
    menu();
    menuchoice = menu();

    if(menuchoice == 1)
    {
        calc();
    }
    if else(menuchoice == 2)
    {
        beer();
    }
}


Needs to be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    int menuchoice;
    menu();
    menuchoice = menu();

    if(menuchoice == 1)
    {
        calc();
    }
    else if(menuchoice == 2)
    {
        beer();
    }
}
Look into switch case statements, they will make menu options easier.
Thanks so much for the response Elite Zero, I feel like a dummy now.

Switch case statements is the next chapter of the book I am reading but I didn't want to move on until I was sure that I understood what the book discussed so far.

That little fix worked, and just had some minor loop bugs to work through. I feel confident enough to move on now though!

-Steve
Topic archived. No new replies allowed.