A little assitance..

Im trying to get this program to work, but instead ive gotten at least 14 errors after i complied it...and idk why! its supposed to continuosly show the menu and run those functions based on user input.



#include <iostream>
using namespace std;

void displayMenu();
void performAction();

double getSum(double num1, double num2);
double getDif(double num1, double num2);
double getPro(double num1, double num2, double x);
double getQuo(double num1, double num2, double q);

int main()
{
int code;

cout<<"Operation Main Menu"<<endl;
cout<<"---------------------"<<endl;
cout<<"1.Addition "<<endl;
cout<<"2.Subtract "<<endl;
cout<<"3.Multiplication "<<endl;
cout<<"4.Division "<<endl;
cout<<"5.Exit "<<endl;

cin>>code;
cout<<endl;

return 0;
}


if(code=1)

double getSum(double num1, double num2)
{
double sum;
sum=num1+num2;
return sum;
}



if(code=2)

double getDif(double num1, double num2)
{
double dif;
dif=num1-num2;
return dif;
}





if(code=3)

double getPro(double num1, double num2, double x)
{
double x;
x=num1*num2;
return x;
}



if(code=4)

double getQuo(double num1, double num2, double q)
{
double q;
q=num1/num2;
return q;
}




if(code=5)
cout<<"Program Closing..."<<endl;

else
cout<<"Input a code 1-5"<<endl;
a) '=' is the assignment operator. You're looking for '==', the comparison operator.

b) Your ifs are... nowhere. They should be in a function (in this case: in your main).

c) In getQuo (getPro): your passing double x (q), then redeclaring x (q). Drop the "double x" ("double q") either from the function body, or the function argument list. I suggest the latter, because it serves no point here.

[edit]

By the way, here's the "correct"/"standard" organization of a file:
1
2
3
4
5
6
7
// Includes 
// Pre-declaration of functions
main() {
  // Variable declaration
  // Function calls
}
// Function definitions 

As an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Includes
#include <iostream>
using namespace std;
// Function pre-declarations
double getSum(double num1, double num2);
// Main
int main() {
   // Variable declaration
   int a = 6; 
   int b = 7;
   // Function calls
   cout << "The sum of " << a << " and " << b << " equals: " << getSum(a,b) << "!" << endl;
   return 1; // End of program.
}
// Function definition
double getSum(double num1, double num2) {
   double sum;
   sum = num1 + num2;
   return sum 
}

That's just one way to organize your files, but it's the standard [in the classroom]. Also, take a good look at where functions are called (in the main!) and how they are called.
Last edited on
ok but if my 'ifs' are in my main how would i get them to actually make my user-defined functions run?
ok i got a few questions;

1)how exactly would I get my menu to continuosly run while the program runs?
2)this program is for user input, so i dont think i can declare variables that are pre defined with numbers
3)and where would i put my "cin" so that the user can input and make these user-defined fucntions run
4)sorry, im just all confuzzled 0_o
1) Wrap the necessary code from your main in a while loop.
2) Replace
int a = 7;
by int a; cin >> a;.
3) At the start of your while loop you ask for input; then you call your functions.
4) Aren't we all?
ok, I put my menu in a do..while loop, but the freaking loop wont stop


#include <iostream>
using namespace std;

void displayMenu();
void performAction();

double getSum(double num1, double num2);
double getDif(double num1, double num2);
double getPro(double num1, double num2, double x);
double getQuo(double num1, double num2, double q);

int main()
{
int code;
double a; cin>>a;
double b; cin>>b;



cout<<"Enter a value 1-5 to begin the program"<<endl;
cin>>code;
do
{


cout<<"Operation Main Menu"<<endl;
cout<<"---------------------"<<endl;
cout<<"1.Addition "<<endl;
cout<<"2.Subtract "<<endl;
cout<<"3.Multiplication "<<endl;
cout<<"4.Division "<<endl;
cout<<"5.Exit "<<endl;
}
while (code<=5);

cin>>code;
cout<<endl;




return 0;

}


double getSum(double num1, double num2)
{


double sum;
sum=num1+num2;
return sum;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
do
{


cout<<"Operation Main Menu"<<endl;
cout<<"---------------------"<<endl;
cout<<"1.Addition "<<endl;
cout<<"2.Subtract "<<endl;
cout<<"3.Multiplication "<<endl;
cout<<"4.Division "<<endl;
cout<<"5.Exit "<<endl;
}
while (code<=5);


This loop will repeat as long as code has a value of 5 or less. Tell me, when exactly is code going to get a value above 5? This loop doesn't even change the value of code, so whatever value it has at the start, it will have forever. Looping round and round and round forever, waiting for something to change the value of code...
Last edited on
Now I feel like an idiot, what would be the proper way to correct this do..while loop?i def. get why its been looping forever
1. Use code < 5, obviously you want to be able to exit when it == 5.
2. Ask the user to input to code during the loop.
3. Test it, then write code in the loop to do the function specified by the user. ;)
This is how the program is supposed to be ran>>>>>


An Example: Operation Main Menu
====================
1. Addition
2. Subtract
3. Multiplication
4. Division
5. Exit
--------------------
Enter your operation code (1, 2, 3, 4 or 5): 1
*************************************************
Please enter two double numbers: 2.5 3.6
The sum is 6.10
*************************************************

Operation Main Menu
====================
1. Addition
2. Subtract
3. Multiplication
4. Division
5. Exit
--------------------
Enter your operation code (1, 2, 3, 4 or 5): 5
Last edited on
ive gotten my loop to stop, but i need it to continue after everytime the user gets the answer to each input. like stated above>>>>
#include <iostream>
using namespace std;

void displayMenu();
void performAction();

double getSum(double num1, double num2);
double getDif(double num1, double num2);
double getPro(double num1, double num2, double x);
double getQuo(double num1, double num2, double q);

int main()
{
int code;
double a,b;


cout<<"Operation Main Menu"<<endl;
cout<<"---------------------"<<endl;
cout<<"1.Addition "<<endl;
cout<<"2.Subtract "<<endl;
cout<<"3.Multiplication "<<endl;
cout<<"4.Division "<<endl;
cout<<"5.Exit "<<endl;
cout<<"======================"<<endl;

cout<<"Enter a value 1-5 to begin the program"<<endl;
cin>>code;
do
{


cout<<"Operation Main Menu"<<endl;
cout<<"---------------------"<<endl;
cout<<"1.Addition "<<endl;
cout<<"2.Subtract "<<endl;
cout<<"3.Multiplication "<<endl;
cout<<"4.Division "<<endl;
cout<<"5.Exit "<<endl;
}
while (code==5);

cin>>code;
cout<<endl;




return 0;

}


double getSum(double num1, double num2)
{


double sum;
sum=num1+num2;
return sum;

}
Topic archived. No new replies allowed.