0

I am working on a homework that requires me to copy paste a given text, that consists of mulitiple lines each having data that I need to analyse, into the console. Anyway the problem is that when I select the text and paste it into the console, the console closes.The problem only occurs when I try to paste more than 1 line. And after some tests, any text that I try to paste that is on more than.
I am working in C, and I'm trying to store it into a string:

int main()
{   
  char text[1000];
  gets(text);
  printf("%s",text);

  return 0;
}

For example if I try to paste:

8C TS KC 9H 4S 7D 2S 5D 3S AC

it works normally
but if I try to paste:

8C TS KC 9H 4S 7D 2S 5D 3S AC

5C AD 5D AC 9C 7C 5H 8D TD KS

it closes the console.I am curious why it does this.

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
  • 2
    Does that also happen if you start the program from an already open commandline? If not you simply have the problem that the progam closes the console after finishing, you do not have a loop for reading more than one line... – Yunnosch Jan 06 '18 at 15:28
  • 1
    Don't use `gets`. Never ***ever*** use `gets`. It's a very *dangerous* function, and for that reason have been removed from the C specification. Use e.g. [`fgets`](http://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Jan 06 '18 at 15:30
  • 1
    As for your problem, the `gets` function (as well as `fgets`) reads *one line*. Stop just guessing and hoping for the best, [get a couple of good beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to read instead. – Some programmer dude Jan 06 '18 at 15:31

2 Answers2

0

First of all: do not use gets, this function is not safe, because it doesn't prevent buffer overflows. If you input more than 999 characters you will have a buffer overflow. Use fgets instead.

What you are describing is something concerning your terminal, not the C language. Which terminal are you using?

Depending on how much text was selected (for example you select more than one line), a new line ('\n') is also copied, if you paste that onto your terminal, the pasted new line acts as if you've pressed ENTER, the line is entered (gets() returns), printf prints the line on the terminal and then program ends. Depending how you open the terminal, the terminal may also close immediately after the program closes.

This has nothing to do with C but how you use your terminal.


EDIT

I've overseen from the title that you are using the Codeblock IDE, right? If you click on Run, then a console is opended and your program is executed. Most of the time, the standard setting is that the console closes immediately after the program ends. I don't know if you can change these settings in the Codeblock configuration (something like "don't close terminal on exit"). If you cannot change this configuration, you could end your program like this, so that terminal doesn't close immediately:

#include <stdio.h>
int main(void)
{
    // your progam here

    printf("Press ENTER to end");
    getchar();
    return 0;
}

I personally don't like this, a program on the console shouldn't do that (unless it's necessary for some reason). A better thing would be that you open the terminal, go to the directory of your compiled program and execute it yourself. Thus when the program ends, the terminal stays alive.

Pablo
  • 13,271
  • 4
  • 39
  • 59
0

As already mentioned, do not use gets since it will not prevent a buffer overflow attack.

Your terminal closes, because your program waits until the user sends a new line, once it does, gets reads the characters before the new line into your text array and printf prints that array and your program finishes and closes the console if it was opened by the program and not by you or another program.

If you want to input several lines and perform a task on the given input, just use a loop. Here is a quick example:

#include <stdio.h>

int main(int argc, char** argv) {
   char text[100];

   printf("Write something:\n");
   do {
       fgets(text, 100,stdin);
       /* do something with input */
       printf("%s", text);
      } while(text[0] != '!'); 
       /* this will run until user writes an exclamation mark in the 
       beginning of the text */

  return 0;
}