Some Problem in my program of calculating Tables of a number using functions.

Hi, there is a problem in the program, apparently it only gives me tab*1 = value ,
while I want table value upto 10, please help what I got wrong with the program?

#include<iostream>
using namespace std;
int table(int);

main()
{
int n,res;
cout<<"Please enter the value for table:=";
cin>>n;
table(n);
}


int table(int n)
{
for(int c=1;c<=10;c++)
{
int res=n*c;
cout<<n<<"*"<<c<<"="<<res<<endl;
return res;


}

}
You have return inside your for loop, so the first time you go through the loop, the function table will end.
Last edited on
Oh,I used return in the main body and the program works !!


But in my book , they've used return in the function body and the loops too!with different programs, can you tell me the difference when to use return in the main body and when in the Function body?? Thanks!
Last edited on
Can you help me with this program too and tell me what and where to put the return thingy?? Its very confusing to me,.

#include<iostream>
using namespace std;
float calculator(int,char,int);

main()
{ int a,b,op;
cout<<"Calculator"<<endl;
cout<<"Please enter the first no."<<endl;
cin>>a;
cout<<"Please enter the operator"<<endl;
cin>>op;
cout<<"Please enter the 2nd no."<<endl;
cin>>b;
calculator(a,op,b);


}


float calculator(int a,char op,int b)
{
switch(op)
{
case '+':
{
cout<<a+b<<endl;
break;

}
case '-':
{
cout<<a-b<<endl;
break;

}
case'*':
{
cout<<a*b<<endl;
break;
}
case'/':
{
cout<<a/b<<endl;
break;
}

default:
cout<<"Invalid Input";
}
}




My first program worked btw, while this one is not working .
Last edited on
You should use return in every function. main is a function. Your function table is a function.

Your function table claims that it returns an int. But it doesn't. What you're trying to do here (writing a function) is beyond your current knowledge level. You need to go back and learn about functions.

You're also using an incorrect form of main. main() is wrong. It should be int main()

---

Note for other people; yes, yes, I know that there are cases where you don't have to explicitly write return. But for a beginner, at this level of knowledge, "always" is a pretty solid answer that can't go wrong.
Hmmm, can you correct the 2nd program i.e Calculator one for me , so I'd know what I'm doing wrong .

And I have basic idea of functions just having problem with this return thing .

Also I always use main() in my compiler and it works for me.
Last edited on
The reason why it doesn't work is in this case op. You let the user enter a numeric (int) value. But you pass it as a textual (char) value. That doesn't work -> Make op char op;

The simpliest way to deal with return is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
float calculator(int a,char op,int b)
{
float result = 0;
switch(op)
{
case '+':
{
cout<<a+b<<endl;
break;

}
case '-':
{
cout<<a-b<<endl;
break;

}
case'*':
{
cout<<a*b<<endl;
break;
}
case'/':
{
cout<<a/b<<endl;
break;
}

default:
cout<<"Invalid Input";
}
return result;
}
This way the return value is always valid. In this case though you don't want to return anything, hence use void ->

void calculator(int,char,int);
Also I always use main() in my compiler and it works for me.


It's still wrong. If your compiler doesn't object, then your compiler is wrong too. If you want to learn C++, you should really learn correct C++.
@coder777, thank you sir, you're a great dude.
Topic archived. No new replies allowed.