### 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133`` `````` #include #include 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(); } } ``````
 ``123456789101112131415`` ``````int main() { int menuchoice; menu(); menuchoice = menu(); if(menuchoice == 1) { calc(); } if else(menuchoice == 2) { beer(); } }``````

Needs to be:

 ``123456789101112131415`` ``````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.