calculator hel p please

i'm completely new to c++, let alone used to using multiple functions.. my friend reccomended i make a calculator using switches and multiple funcions for practice so i tried, and have been failing miserable, anyone mind helping me out please? heres my current source code:

#include <iostream>
using namespace std;

float a, b;

float addition(float a,float b)
{
return(a+b);
}
float subtraction(float a,float b)
{
return(a-b);
}
float division(float a,float b)
{
return(a/b);
}
float multiplication(float a,float b)
{
return(a*b);
}
float get_numbers()
{
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
}
int main()
{
int option;

do
{
do
{
cout<<"Please select an option:"<<endl
<<"0 : Quit"<<endl
<<"1 : Add two numbers"<<endl
<<"2 : Subtract one number from another"<<endl
<<"3 : Multiply two numbers"<<endl
<<"4 : Divide one number by another"<<endl;
cin>>option;
}
while (option<0);
}
while (option>4);

switch (option)
{
case 0 :
case 1 : float get_numbers();
float addition(float a,float b);
break;
case 2 : float get_numbers();
float subtraction(float a,float b);
break;
case 3 : float get_numbers();
float multiplication(float a,float b);
break;
case 4 : float get_numbers();
float division(float a,float b);
break;
}
}
- you have case 0-4: float get_numbers(); all over the place
you have to remove "float"; case 0-4: get_numbers();

- the same goes for the lines that come after, remove the 3 "float"s division(a, b);

- case 0: (Quit) does nothing, does not break, and thus goes on to case 1: (Addition)

- The addition substraction multiplication and division functions all return a float, im guessing you want to print out what they return, so you should do something like this
1
2
3
4
case 4 :
    get_numbers();
    cout << division(a, b);
    break;

- You can turn the two do-while loops into one, using && (and)
while(option<0 && option>4)

Its also not really smart to name the parameters of your functions after global variables

And lastly, on the internet, use [code] tags.
Last edited on
yea sorry, i'm really new to this, i know it's messy and bad, but tyvm! your advice helped very much and i've got it in working condition.. about the case 0 quit being empty, i wasnt sure what to put there to just terminate the program
probably break;, return 0;, or just, dont write a case 0
Last edited on
Topic archived. No new replies allowed.