Can Anyone explain why this happens? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Can Anyone explain why this happens?

I have been making a code for print a square with some functions I have made. Can anyone explain why 5 is printing after the product. https://code.sololearn.com/cU80To9kZjYG/?ref=app

27th Feb 2018, 2:22 PM
Akash Pal
Akash Pal - avatar
3 Answers
+ 2
The function rf returns an integer. When you not return a value it produces undefined behaviour. Seems like the compiler used one sololearn returns either a 1 of nothing gets returned or the last variable used in a for loop, if the for loop has a range that is determined by an given integer argument. As you can see this behaviour is really strange, and to add complexity to this: another compiler will most likely behave totally different. You need to change rf to void and call the rf function before you cout << endl.
27th Feb 2018, 2:41 PM
Alex
Alex - avatar
+ 4
@Alex and @Timon thanks for the help and I have figured it with both of answers provided by you two.
27th Feb 2018, 3:34 PM
Akash Pal
Akash Pal - avatar
+ 3
//not sure, but now it works. #include <iostream> using namespace std; void rf(int x){ for(int y=0;y<x;y++){ cout << "*"; } } void yy(int y){ for(int t=0;t<y;t++){ rf(y); cout << endl; } } int main() { int a; cin >> a; yy(a); return 0; }
27th Feb 2018, 2:37 PM
Timon PaƟlick