Whats wrong in the below program? I get a Time Limit Exceeded error. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Whats wrong in the below program? I get a Time Limit Exceeded error.

#include <iostream> using namespace std; int main() { char a=0; while(a<256) { cout<<a<<" "; a++; } return 0; }

7th Mar 2017, 7:36 AM
Surya Vamsi
Surya Vamsi - avatar
2 Answers
+ 12
SoloLearn can't handle loops with iterate for too long, will cause it to timeout. Your code should work fine on desktop compilers though.
7th Mar 2017, 7:46 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
This loop will never end. The 'a' variable will never reach the 256 value because its overflow the ascii table whose values goes up to 127. If you define the 'a' variable as 128 it will return back to -128, and after increment it will be set to -127. This will continue until it reaches 127 and so it will back again to -128. Set the a while variable to 127 and will have the all ascii characters printed out on the screen. That is why the sololearn gives you a time limit exceeded error.
7th Mar 2017, 1:46 PM
Kleber Leal
Kleber Leal - avatar