Search code examples
c++arrayschargets

Making array (char) 1 gives error on stdio.h


I was trying to hold the text entered by user inside an Char array but it does not end up well. I tried this method but i think it deleted after c++ 11.

Here's my code :

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    char sentence[2];

    cout << "Enter your sentences : ";
    gets_s(sentence);

    cout << sentence << endl;

    system("PAUSE");
    return 0;
}

It gives overload error and doesnt works.


Solution

  • Chances are you are trying to get the string literal that is longer than 2 characters yet not being able to insert it into your buffer of:

    char sentence[2];
    

    Increase the buffer size to something more acceptable:

    char sentence[255];
    

    That being said in C++ you should prefer std::string to character array and std::getline to gets_s.