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!
#include <iostream>
#include <string>
usingnamespace 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;
}
elseif (funchoice == 2)
{
cout << x << " - " << y << " = " << x - y << endl;
i = 1;
}
elseif (funchoice == 3)
{
cout << x << " * " << y << " = " << x * y << endl;
i = 1;
}
elseif (funchoice == 4)
{
if (x != 0)
{
cout << x << " / " << y << " = " << x / y << endl;
i = 1;
}
elseif (x == 0)
{
cout << "Division By Zero Will End The Universe" << endl;
i = 1;
}
}
elseif (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;
}
elseif (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
elseif (userinput == 3)
{
cout << "You Have Chosen to Run the Password Loop" << endl;
}
elseif (userinput == 4)
{
cout << "You Have Chosen to Exit the Menu" << endl;
i = 0;
}
elseif(userinput < 1 || userinput > 4)
{
cout << "Invalid Entry" << endl;
}
}
}
int main()
{
int menuchoice;
menu();
menuchoice = menu();
if(menuchoice == 1)
{
calc();
}
ifelse(menuchoice == 2)
{
beer();
}
}
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!