-3

After reading several answers I have corrected my code to as follows;

int main()
{
    // a pointer to char is initialized with a string literal

    char Buffer[100];

    cout << "Enter an initial string: " << endl;
    cin >> Buffer;
    cout << "Original content of Buffer:--> " << Buffer << endl;


    cout << "Enter a sentence: " << endl;
    cin >> Buffer;



    gets(Buffer);
    cout << "Now the Buffer contains:--> " << Buffer << endl;

    return 0;
}

I know longer have the warning code, but now the program doesnt execute as I would like. The last part does not output my new sentance.

I know people mentioned not to use gets, but I tried using getline, obviously I cant use it as a direct replacement so I was a bit lost.

Any suggestions

FrankWhite
  • 97
  • 1
  • 3
  • 11
  • 1
    It's a good idea to ask the compiler to treat that warning as an error. – Cheers and hth. - Alf Feb 09 '16 at 10:52
  • Possible duplicate of [How to get rid of \`deprecated conversion from string constant to ‘char\*’\` warnings in GCC?](http://stackoverflow.com/questions/59670/how-to-get-rid-of-deprecated-conversion-from-string-constant-to-char-warnin) – LogicStuff Feb 09 '16 at 10:52
  • 3
    You'll need either `std::strcpy` (to make a deep copy) or `std::string`. You cannot modify a string literal. `char Buffer[] = "Dummy content.";` is also a way. – LogicStuff Feb 09 '16 at 10:53
  • @Logicstuff, sorry when you say I need it, where would I put it in the code? – FrankWhite Feb 09 '16 at 10:54
  • You are in C++, don't use `char *`, use strings; don't use `gets`, use `cin >>` or streams functions. Choose the world your in, don't mix C I/Os with C++'s. – Jean-Baptiste Yunès Feb 09 '16 at 10:56
  • Don't use `std::endl` unless you need the extra stuff that it does. `'\n`' starts a new line. – Pete Becker Feb 09 '16 at 13:29
  • Re: "use gets() to get the whole line" -- use `std::getline()` to get the whole line. – Pete Becker Feb 09 '16 at 13:31

4 Answers4

1

You cannot read into a memory which contains string constant. Often those string constants are stored in read-only memory and even if not, they can share the constants so you would override one string for all parts of your code.

You need to copy the string into some buffer and then do whatever you want. For example:

const char *myOrigBuffer = "Dummy string";
char buffer[1024];
strcpy(buff, myOrigBuffer);
....
gets(buff);
Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
  • Thanks Zbynek, I have edited my program to try and incorporate your suggestions but it is still not working.... how do I add code in a comment lol – FrankWhite Feb 09 '16 at 11:01
  • @FrankWhite: You don't. You update the question. (Probably leaving the old code in place, and adding the new code. You must also clarify what "not working" means: Hangs, error, what? Also, learn to use a debugger. You will find the problem very quickly. – Martin Bonner supports Monica Feb 09 '16 at 12:29
0

You cannot modify string literral. Your way of coding is too much "C style". If the original buffer content doesn't matter and you must use gets(), don't initialize your buffer :

char Buffer[100];
cout << "Enter a sentence: " << endl;

gets(Buffer);
cout << "Now the Buffer contains:--> " << endl;
cout << Buffer << endl;

Don't forget that if you input more than 100 characters (as the size of the buffer), this will also crash.

As gets is deprecated, fgets must be encouraged : it protects you from overflows. You should code this way in C-Style :

char buffer[10];
printf("Your sentence : \n");
fgets(buffer, sizeof buffer, stdin);
printf("Your sentence : %s\n", buffer);

Ouputs :

Your sentence :
1 34 6789012345
Your sentence : 1 34 6789

Nonetheless, you should consider using std::cin with std::string to make correct C++ inputs :

std::string sentence;
std::cout << "Enter a sentence (with spaces): ";
std::getline(std::cin, sentence);
std::cout << "Your sentence (with spaces): " << sentence << std::endl;

Outputs :

Enter a sentence (with spaces): My super sentence
Your sentence (with spaces): My super sentence

A. Ocannaille
  • 306
  • 4
  • 14
  • Thanks, but it is still not entirely working. When it prompts me to enter a sentance, I do so, but then the output is just 'Now the buffer contains :-->' and my string is gone – FrankWhite Feb 09 '16 at 11:09
  • I am trying to show you my now modified code so you can see, but having difficulties putting it into a comment without it being a block of text... – FrankWhite Feb 09 '16 at 11:28
  • Don't try to put code in comment, but add it at the end of your question instead. Put a title like "Edit after A. Ocannaille answer" Or something like that. Then your new code, then an explication of your new problem. – A. Ocannaille Feb 09 '16 at 13:02
0

A string literal like "Dummy content." is logically const, since any operation that attempts to change its contents results in undefined behaviour.

The definition/initialisation

char *Buffer = "Dummy content.";

however, makes Buffer a non-const pointer to (the first character of) a string literal. That involves a conversion (from array of const char to a char *). That conversion exists in C for historical reasons so is still in C++. However, subsequently using Buffer to modify the string literal - which is what gets(Buffer) does unless the user enters no data - still gives undefined behaviour.

Your "stopped working" error is one manifestation of undefined behaviour.

Giving undefined behaviour is the reason the conversion is deprecated.

Note: gets() is more than deprecated. It has been removed from the C standard, from where it originated, completely because it is so dangerous (no way to prevent it overwriting arbitrary memory). In C++, use getline() instead. It is often not a good idea to mix C I/O function and C++ stream functions on the same input or output device (or file) anyway.

Peter
  • 35,646
  • 4
  • 32
  • 74
0

char *Buffer = "Dummy content.";

You should use pointer on const char here because "Dummy content." is not a buffer but pointer on string literal that has type "array of n const char" and static storage duration, so cannot be changed through pointer. Correct variant is:

char const* Literal = "Dummy content.";

But you cannot use it as parameter for gets

gets(Buffer);

It is bad idea and should cause write access exception or memory corruption on writing. You should pass to gets a pointer to a block of memory where received string will be stored. This block should have enough length to store whole string, so in general gets is unsafe, check https://stackoverflow.com/a/4309845/2139056 for more info. But as temporary test solution you can use buffer on stack:

char Buffer[256];
gets(Buffer);

or dynamic allocated buffer:

char* Buffer= new char[256];
gets(Buffer);
//and do not forget to free memory after your output operations
delete [] Buffer;
Community
  • 1
  • 1
mooncheese
  • 124
  • 1
  • 1
  • 4