when i run my code suppose with input 1 then from 0 to 10 output is correct but i get 11 th value as garbage value! why so?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

when i run my code suppose with input 1 then from 0 to 10 output is correct but i get 11 th value as garbage value! why so??

#include <iostream> using namespace std; int mult(int a,int b,int c) { if(a>=0) { cin>>a; for(b=0;b<=10;b++) { c=a*b; cout<<c<<endl; } } } int main() { int a,b,c; cout <<mult(a,b,c); return 0; }

10th Jan 2017, 7:40 AM
Md Shaifan Shahid
Md Shaifan Shahid - avatar
7 Answers
+ 1
i tried that @rosen.. its not working.. The same problem persists..
10th Jan 2017, 7:50 AM
Md Shaifan Shahid
Md Shaifan Shahid - avatar
+ 1
thanks @ASNM.. when i wrote just mult(a, b, c) in the main function instead of cout <<mult(a, b, c) it worked.i am no longer getting a garbage value. But why is it so?? can anyone explain it?
10th Jan 2017, 8:08 AM
Md Shaifan Shahid
Md Shaifan Shahid - avatar
0
Your first if statement has to be after cin>>a;
10th Jan 2017, 7:44 AM
Rosen
Rosen - avatar
0
mult return c; if you want no output int main() { int a,b,c; mult(a,b,c); return 0; }
10th Jan 2017, 7:51 AM
ASNM
ASNM - avatar
0
I think it is because you are counting the 0 too. Try b=1 in the for loop.
10th Jan 2017, 7:54 AM
Rosen
Rosen - avatar
0
#include <iostream> using namespace std; int mult(int a,int b,int c) { if(a>=0) { cin>>a; for(b=0;b<=10;b++) { c=a*b; cout<<c<<endl; } } return c; } int main() { int a,b,c; cout<< mult(a,b,c); return 0; }
10th Jan 2017, 8:19 AM
ASNM
ASNM - avatar
0
can you please give me your WhatsApp number for more contact
10th Jan 2017, 11:49 AM
Kalema Bashir Ibrahim
Kalema Bashir Ibrahim - avatar