-3

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.

  • 1
    That's not an error message, please give the exact output from the compiler. – Richard Critten Sep 11 '17 at 14:19
  • There's no error message it's just dont working @RichardCritten – Sefa Kalkan Sep 11 '17 at 14:20
  • Don't usegets, it's deprecated: https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – R2RT Sep 11 '17 at 14:22
  • @R2RT - the author is using gets_s which is one of the two recommended replacements by cppreference (http://en.cppreference.com/w/c/io/gets). – Kevin K Sep 11 '17 at 14:23
  • Just a hint: how long is a sentence you can fit into `char sentence[2]` ? – Maciek Sep 11 '17 at 14:23
  • A 1 character sentence will not be that interesting. – drescherjm Sep 11 '17 at 14:24
  • @R2RT thanks for advice i'm newbie btw – Sefa Kalkan Sep 11 '17 at 14:24
  • @Maciek 2 sentence? – Sefa Kalkan Sep 11 '17 at 14:25
  • @SefaKalkan Do you mean that you code works when array length is greater than one or less than 1? Your code is probably failing at runtime due to one of the constraints described at http://en.cppreference.com/w/c/io/gets – Kevin K Sep 11 '17 at 14:25
  • @KevinK edited thanks for the finding mistake – Sefa Kalkan Sep 11 '17 at 14:26
  • @SefaKalkan - No, you get two individual characters - not lines. – Kevin K Sep 11 '17 at 14:28
  • @KevinK Oh, is this about index? – Sefa Kalkan Sep 11 '17 at 14:28
  • 2
    Not really. char sentence[2] is an array of two characters. It can fit two, and only two, characters. If you want to fit more, you have to increase the size of the array. Using get_s will cause the last character in the array to be an end of line character (`\n`). That means you really only get one character (which is not a sentence). You are probably encountering the runtime error with sentence[1] because of the condition, 'endline or eof not encountered after storing n-1 characters to the buffer.' If you want to store a 200 character sentence, then @Ron has an option for you below. – Kevin K Sep 11 '17 at 14:34
  • actualy i need to do a sentence exact opposite and write it back this is why i'm using char – Sefa Kalkan Sep 11 '17 at 14:37
  • 2
    Mixing the C and C++ is not recommended. – Ron Sep 11 '17 at 14:41
  • 2
    If you are using the C language I/O, like `gets`, use the C language output like `printf`. If you are using the C++ language I/O like `cout`, use the C++ language input like `cin`. Mixing the two is not a good idea. – Thomas Matthews Sep 11 '17 at 14:48
  • @ThomasMatthews it works thanks – Sefa Kalkan Sep 11 '17 at 14:51

1 Answers1

1

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.

Ron
  • 14,674
  • 4
  • 34
  • 47