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" << " ";
     gets(s);

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

     system("pause");

     return 0;
}

Recommended Answers

All 3 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.