c++ unable to get proper string input
<p>I am new to programming just wrote this simple function to calculate length of a string but I am unable to take input string from the user properly(have tried my alternatives).</p>

<pre><code>/////////Function to calculate length of a string////////////////////
void str_length(){
    char str[30];
    int counter=0;
    cout &lt;&lt; "Enter string: ";
    gets(str);
    //cin.getline(str,256);
    //cin &gt;&gt; str;
    for(int i=0;str[i] != '\0';i++){
        counter++;
    }
    cout &lt;&lt; "String length is: " &lt;&lt; counter &lt;&lt; endl;
}
///////////////////////////////////////////////////////////////
</code></pre>

<p>of all possible ways the program either exits abruptly or with 'cin' I can only get partial string till the first space.</p>
