I want as said in 'cout' to input 10 or more letters in this statement but when i try to run it when I enter more then 10 letters i get error that says:: Run-Time Check Failure #2 - Stack around the variable 's' was corrupted" so is there any way to solve this problem urgent please??

int main()
     char s[10];
     cout << "ENTER a few letters not crossing 10" << " ";

     if(strlen(s) < 10)
          cout << "It's OK...\n";
     else if(strlen(s) >= 10)
          cout << "This shit doesn't work";


     return 0;

Edited by fplgenius: n/a

7 Years
Discussion Span
Last Post by NathanOliver

When you enter 10 characters using gets() you overrun the buffer you are inputting into by at least one character, wiping out data you should not be touching. See this about gets() Create a loop and use getchar() instead. getchar() is designed to input 1 character at a time as you want. Or look up fgets() as mentioned in the link.


Or, since you appear to be using C++, use std::getline and std::string instead of those messy C-style char arrays.


the problem is you are using gets() for your input and when you enter more than 10 elements the function gets() writes pass the end of the array and is casing your problem. you can make s bigger then see if s is larger than 10 and if it is than reset s to 10. a better function would be fgets() where you tell the function the max amount of characters to be read. http://www.opengroup.org/onlinepubs/009695399/functions/fgets.html

^^^ oops walt bet me to it

Edited by NathanOliver: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.