Hi,

First of all, I'm a newbeginner in C++.
Ok, basically I'm tring to make a calculator with functions. Every function calculates either addition, subtraction, division or multiplication, pretty simple. Though I can't get it to call these functions! Here, take a look at my code:

#include <iostream>
using namespace std;

// Prototyping the functions
int addition(int n1, int n2);
int subtract(int n1, int n2);
int division(int n1, int n2);
int multiply(int n1, int n2);

// Main
int main()
{
    // Input, choice
    cout << "What do you want to calculate?\n" << endl;
    cout << "Addition       (1)" << endl;
    cout << "Subtraction    (2)" << endl;
    cout << "Division       (3)" << endl;
    cout << "Multiplication (4)" << endl;

    int choice;
    cin  >> choice;

        // If choice is...
        if (choice == 1)
        {
        addition(int n1, int n2);
        }
        else if (choice == 2)
        {
        subtract(int n1, int n2);
        }
        else if (choice == 3)
        {
        division(int n1, int n2);
        }
        else if (choice == 4)
        {
        multiply(int n1, int n2);
        }
    }

int addition(int n1, int n2)
{
    cout << "Please enter the first number: " << endl;
    cin  >> n1;
    cout << "Now enter the second number: "   << endl;
    cin  >> n2;

    cout << n1 << " + " << n2 << " is equal to " << n1 + n2;
    return 0;
}

// (I'm gonna add the missing functions here, don't worry! :P)

Yeah. The compiler says "expected primary expression before 'int'". Can anyone help here?Errors are on line 26, 30, 34 and 38.
Thanks!

Chris

When calling a function you do not specify the data type of the parameters, just the names of the variables.

Line 26 should be: addition(n1,n2);

@Ancient-Dragon

Thanks,

I changed the code from line 26 to the following:

// If choice is...
        if (choice == 1)
        {
        addition(n1, n2);
        }
        else if (choice == 2)
        {
        subtract(n1, n2);
        }
        else if (choice == 3)
        {
        division(n1, n2);
        }
        else if (choice == 4)
        {
        multiply(n1, n2);
        }

Now, it throws me this error:
'n1' was not declared in this scope
'n2' was not declared in this scope
...until line 38 again

Any ideas

Thanks again
Chris

you have to declare the variables somewhere. Function prototypes do not declare variables, they merly tell the compiler what types of variables are to be in the function parmerter. The actual variable names can be anything you want. You can name them "Joe" and "Moe" if you want.

int main()
{
   int Joe = 0;
   int Moe = 0;
   ...
   ...
    addition(Joe,Moe);

@Ancient-Dragon, I don't think I understand. Can you explain to me what to do to make it work? I think it's easier for me to understand if I see it for myself. If you have time!

Chris

 addition(int n1, int n2);

 subtract(int n1, int n2);

 division(int n1, int n2);

  multiply(int n1, int n2);

don't call functions in that manner.
it should be addition(n1,n2); (as Ancient Dragon said).

you are passing variable here which are not declared. so either declare these variable and input values to those and then call these functions.

your function body shows that you are getting values from these functions so call function as addition();

hope this helps you . . .

@rishif2

I must be dumb, but again, I don't get it. Maybe you can explain it with not so advanced words? I feel my english is too bad to understand that correctly.

I did change my lines of code that called functions to addition(n1, n2) etc. Do I have to declare the n1 and n2 variables or? I don't really get it lol.

Sorry for asking a lot :P

Chris

Are you required to provide parameters to those functions? If not, then just declare the variables inside the functions and have no parameters.

#include <iostream>
using namespace std;

// Prototyping the functions
int addition();
int subtract();
int division();
int multiply();

// Main
int main()
{
    // Input, choice
    cout << "What do you want to calculate?\n" << endl;
    cout << "Addition       (1)" << endl;
    cout << "Subtraction    (2)" << endl;
    cout << "Division       (3)" << endl;
    cout << "Multiplication (4)" << endl;

    int choice;
    cin  >> choice;

        // If choice is...
        if (choice == 1)
        {
        addition();
        }
        else if (choice == 2)
        {
        subtract();
        }
        else if (choice == 3)
        {
        division();
        }
        else if (choice == 4)
        {
        multiply();
        }
    }

int addition()
{
    int n1,n2;
    cout << "Please enter the first number: " << endl;
    cin  >> n1;
    cout << "Now enter the second number: "   << endl;
    cin  >> n2;

    cout << n1 << " + " << n2 << " is equal to " << n1 + n2;
    return 0;
}

// (I'm gonna add the missing functions here, don't worry! :P)

@Ancient-Dragon lol I didn't even think about that! Thanks again. It works awesome

I know you have a working program as of now, but you'll still want to try and understand the difference between a function prototype and a function call, and between the formal parameters and actual arguments.

First for prototypes. These are, basically, declarations describing how the function's signature, which is to say, it's name, return type and parameter types. You need to have the prototypes ahead of any calls to the function, so that the compiler knows that there is in fact a function names so and so that takes such and such arguments.

If you look at a header file such as <iostream> or <cmath>, you will see a large number of function prototypes, for the standard library functions. These functions are, broadly speaking, the same as those you would write yourself, they just are part of the standard library. As I explain here, headers are used mainly as a way of getting large numbers of declarations and prototypes into programs without having to enter them in manually each time.

When you write a function prototype, you need to put the types of the return value and the parameters, and optionally, the names of the parameters as they are in the actual function implementation. The names of the parameters aren't really important to the prototype, but they do make it easier to understand.

When you then write the function's implementation, you need to have it's parameter types and return type match those of the prototype, or else it is a compiler error. The parameter names are important here, because they become the names of the variables which are used in the function body. These formal parameters are part of the function definition and implementation. The names of the parameters are local to the function;

When you call a function, you are passing it a set of values which then are put into the parameters. These arguments don't have to be variables with the same names when you call them, and in fact you don't need to use variables at all, but can pass any legal value of the appropriate type. For example, you can have a function foo() which takes an integer, and write:

int foo(int a);

int main() 
{
    int x, y, z;

    x = foo(23);
    y = foo(2 + 3);
    z = foo(x * y);

    cout << z;

    return 0;
}

All of these calls to foo() are legal, and return a (hopefully) useable value. The value of 23 (for example) is passed to the function, where it is put into the variable a and can then be used in the function.

@Schol-R-LEA

Thanks man! Really well explained.

Cheers
Chris

int multi(int a, int b)
{
    return (a*b);
}

int main()
{
    int a=0;
    a=multi(20,30);
    cout <<a;
    cin.get();
}

i hope these help you more;)

getch(); //conio.h

Bad suggestion using non-portable and non-standard functions, use cin.get() instead.

sorry for that. and thanks for correct me

what you are doing is that in protoypes of the funtions you are telling to the compiler that in the funtion i will pass two input parameters from the main() but you are not passing it from the main to the funtion..
1)intialze n1 and n2 in main(),take the values from user and then pass it to the funtion i will do an example for you here see..

#include <iostream>
using namespace std;
// Prototyping the functions
int addition(int n1, int n2);
int subtract(int n1, int n2);
int division(int n1, int n2);
int multiply(int n1, int n2);
// Main
void main()
{
      int n1=0;
      int n2=0;

    // Input, choice
    cout << "What do you want to calculate?\n" << endl;
    cout << "Addition       (1)" << endl;
    cout << "Subtraction    (2)" << endl;
    cout << "Division       (3)" << endl;
    cout << "Multiplication (4)" << endl;
    int choice;
    cin  >> choice;
        // If choice is...
        if (choice == 1)
        {
        cout<<"Enter your first number\n";
        cin>>n1;
        cout<<"Enter your second nunmber\n";
        cin>>n2;
        addition(n1,n2);   //dont mention the data types while the funtion call//
        }

int addition(int n1, int n2)  //now here you recieve the the n1 and n2 pass by main//
{
    cout << n1 << " + " << n2 << " is equal to " << n1 + n2;
    return 0;
}

same goes for all funtions :) Good Luck :)

drow a flowchart & then writ a c++ program that reads an integer with two or more digitd from keyboard and then prints the reverse of the digits?

Oh, my, we have a bit of a problem here. You are lucky if anyone responds to this at all, and if they do, it will be rather irate.

Why? Well, first off, you are reviving a long-dead old thread. Second, you are hijacking that thread for a question unrelated to the original question. Third, and worst of all, you posted a homework problem without demonstrating any evidence that you had made an effort to solve it yourself.

DON'T DO THOSE THINGS.

If you have a new question, start a new thread. Ask actual questions, rather than demanding an answer be given to you. And most of all, show your work. No one, and I mean NO ONE here is going to do your work for you. We will answer questions, and give advice, but we won't help you cheat.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.