Cant do that. One at a time sir. Or do it like this cin >> a >> b;
Also, change it to int main()
But the main reason you're getting that error is this -
1 2 3 4
if(c==1)
int a,b;
elseif(c==2)
float a,b;
If you're variable is created within if statements. You cant just do add(a,b); Because they're not defined. It makes no sense that you want to create variables only IF something happens. Declare them at the very top.
int c;
cout<<"1.Add integers\n2.Add real numbers\n3.Add double integer\nEnter your choice: ";
cin>>c;
Thats all good. Then, do this
You can create variables, and make them enter 2 numbers. Then you can do if statements like yours. And if c == 1, then call this function. If c == 2, call that function etc.
void add(int a, int b);
void add(float a, float b);
void add(double a, double b);
int main() // int main buddy
{
int a, b;
float c, d;
double e, f;
cout << "1.Add integers\n2.Add real numbers\n3.Add double integer\nEnter your choice: ";
cin >> c;
if (c == 1) {
cout << "enter two numbers to add";
cin >> a >> b;
cout << "the sum is ";
add(a, b);
}
elseif (c == 2){
cout << "enter two numbers to add";
cin >> c >> d;
cout << "the sum is ";
add(c, d);
}
elseif (c == 3){
cout << "enter two numbers to add";
cin >> e >> f;
cout << "the sum is ";
add(e, f);
}
system("pause");
}
void add(int a, int b)
{
cout << a + b << endl;
}
void add(float a, float b)
{
cout << a + b << endl;
}
void add(double a, double b)
{
cout << a + b << endl;
}
Variables cease to exist when they leave their scope. Usually this is the enclosing {braces}, but in your case it is the if statements.
1 2 3 4 5 6 7 8 9 10
// This code:
if(c==1)
int a,b;
// Is 100% identical to this code:
if(c==1)
{
int a,b;
}
And this highlights the scope of these variables:
1 2 3 4 5
if(c==1)
{
int a,b; // < a,b have scope only in their enclosed braces
} // <- a,b go out of scope here
//< a,b no longer exist here
Therefore:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if(c==1)
int a,b;
// <- a,b goes out of scope and no longer exist here
elseif(c==2)
float a,b; //<- this creates DIFFERENT variables named a,b
// <- ..which promptly go out of scope and no longer exist
elseif(c==3)
double a,b; //<- yet another set of vars
// <- ..which go out of scope
// <- at this point in the program, all variables named a,b have gone out of scope
// and therefore none of them exist
cout<<"enter two numbers to add";
cin>>a,b; // <- so this line gives you an error, because a,b don't exist
If you want a,b to exist throughout main, then you will have to give them scope inside of main.
Note that you cannot change their type based on user input. IE: you can't make 'a' an int if the user inputs 1 and a float if they input 2. Once you decide to make 'a' an int, it is always an int.
This means you will likely need to have a different set of variables (possibly with different names) for each type. Or use templates -- but that's probably more advanced than you need.
EDIT:
Also... cin>>a,b;
This is wrong anyway. General rule: don't use the comma operator for anything other than argument/parameter lists -- it never does what you think it does. It's the single most confusing and useless operator in C++.
You meant to do this: cin >> a >> b;
EDIT: holy crap did I get ninja'd. I didn't see any replies before. Maybe I left this window open way too long before responding. =x
I don't think inline is needed for a simple program like this. People use inline when they want to save memory. When you make a bulky program, which consumes lot of speed and memory, maybe using 'inline' functions will help you.
But not this time.
Function inlining is an optimization technique that compilers sometimes use. The idea is, instead of the traditional function call where your program jumps to another part of the program, an inlined function will effectively be copy-pasted into whatever code called it.
This has some benefits:
- It saves the overhead involved with calling a new function
- It allows for further optimization of the function in the context of the calling code
But also some drawbacks:
- It can create larger code, since you are duplicating the function body everywhere that calls it
The inline keyword is sort of a hint to the compiler that you want this function to be inlined when called. However, it does not guarantee that it will be. In fact, compilers may outright ignore this keyword and simply use their best judgement when determining whether or not the function will be inlined.
In practice, the inline keyword has a different meaning altogether, and more closely relates to some linking rules - and has little to do with whether or not a function is actually inlined.
In short: don't bother with the inline keyword unless you need it. Let the optimizer optimize for you. In this case, you do not need to mark anything as inline.