Search code examples
c++c++14fgetsgets

C++: error in reading character input using fgets()


I have tried a simple code for using fgets(), as gets() is no more used and don't know anything better to read character input from keyboard. My code:

#include<iostream>
#include<cstdio>

using namespace std;



int main()
{
char a;
fgets(a, 100, stdin);
cout<<a;
return 0;
}

I got this error:

cpp:13:20: error: invalid conversion from 'char' to 'char*' [-fpermissive]
 fgets(a, 100, stdin);
                    ^
In file included from /usr/include/c++/7.2.0/cstdio:42:0,
                 from /usr/include/c++/7.2.0/ext/string_conversions.h:43,
                 from /usr/include/c++/7.2.0/bits/basic_string.h:6159,
                 from /usr/include/c++/7.2.0/string:52,
                 from /usr/include/c++/7.2.0/bits/locale_classes.h:40,
                 from /usr/include/c++/7.2.0/bits/ios_base.h:41,
                 from /usr/include/c++/7.2.0/ios:42,
                 from /usr/include/c++/7.2.0/ostream:38,
                 from /usr/include/c++/7.2.0/iostream:39,
                 from jdoodle.cpp:1:
/usr/include/stdio.h:564:14: note:   initializing argument 1 of 'char* fgets(char*, int, FILE*)'
 extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
              ^~~~~

Then, I have tried

#include<iostream>    
#include<cstdio>

using namespace std;



int main()
{
char *a;
fgets(a, 100, stdin);
cout<<a;
return 0;
}

But incurred another error.

It will be appreciated if anyone shows a better method other than using fgets() or solve the above matter.


Solution

  • You are using char *fgets(char *str, int n, FILE *stream) wrong. It is intended to read multiple characters from file, actually up to n-1 characters, and the last character would be null terminator.

    You could use int getc(FILE *stream) to read single character like:

    int a;
    if((a = getc(stdin)) != EOF) {
      // use a 
      char c = a; // convert to char explicitly
    }
    

    As you are using c++ an even better way is to use cin stream:

    char a;
    // formatted read(skips whitespace)
    cin >> a;
    
    // non-formated read
    a = cin.get();
    

    and don't forget to check if the operation has been successful after each read:

    if(cin) {
      // success -> stream is ok
    } else {
      // handle read error
    }
    

    If you want to read multiple characters:

    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    int main() {
      char a[100]; // allocate static buffer
      fgets(a, 100, stdin); // read in the buffer
      cout << a;
      return 0;
    }
    

    Also the c++ way is:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
      string s; // string that automatically manages memory
      cin >> s; // reads non-whitespace sequence of characters
      cout << s;
      return 0;
    }
    

    The other option is to read one line of characters, up to \n including whitespace.

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main () {
      string s;
    
      getline(cin, s);
      cout << s;
    
      return 0;
    }