getting paragraph without using gets() function

  • This is my code…
  • This will executing infinte times…
  • can any help me to getting the paragraph without gets()

char str[100];int i=0;
for(cin>>str[i];str[i]!=’\n’:wink:
{++i;
cin>>str[i];
}

  • If i use gets()function in cstring, iam getting the error like gets() function is out of scope…

hi @prashanthth not sure what exactly you mean by a “paragraph” but clearly your code doesn’t seem to be a good one. You don’t need to use a for loop.

Assumption:
I assume that you basically want to input a string which has spaces, and since you do not want to use a gets() you can use an alternative ie getline(). For that you can use this in c++:


int main()
{
    char a[100];
    cin.getline(a,100);
    cout << a;
}
</pre>

If that doesn’t answer your question, i would request you be a little more specific as to what your requirement is, or even post the problem link which you are solving. :slight_smile:

1 Like

I dnt understnd y u can’t use gets man

agree with @yo_baby. You just need to include the proper header.

Don’t understand what you are asking but

  1. if you want to use gets() then include stdio.h header.

    #include<stdio.h>
    int main(){
     char a[100];
     gets(a);
     return 0;
    }
    
  2. If you don’t want to use gets() then you can use getline.

  • gets()

    If you are using headers of both c and c++ in c++ then you need namespacing.

      #include<stdio.h>
      #include<iostream>
      #include<cstring>
      using namespace std; // add this line
      int main()
      {
       char a[100];
       gets(a);
       puts(a);
       return 0;
      }
    
  • getline()

     #include<iostream>
     #include<string>
     int main ()
     {
       string name;    
       cout << "Please, enter your full name: ";
       getline(cin,name);
       cout << "Hello, " << name << "!\n";
       return 0;
     }
2 Likes