Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to terminate the loop containing scanf?

how to terminate the loop containing scanf?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jcute
    wrote on last edited by
    #1

    int i; char a[10][10]; for ( i= 0; ; i++) { if (scanf("%s",a[i]) == -1) break; } //ctrl-d or z do not work!:confused:

    H 1 Reply Last reply
    0
    • J jcute

      int i; char a[10][10]; for ( i= 0; ; i++) { if (scanf("%s",a[i]) == -1) break; } //ctrl-d or z do not work!:confused:

      H Offline
      H Offline
      HJo
      wrote on last edited by
      #2

      Does it have to be scanf()? Otherwise you could gets(), which terminates at Ctrl+Z: int i = 0; char a[10][10]; while (gets(a[i]) != NULL) { i++; }

      J 1 Reply Last reply
      0
      • H HJo

        Does it have to be scanf()? Otherwise you could gets(), which terminates at Ctrl+Z: int i = 0; char a[10][10]; while (gets(a[i]) != NULL) { i++; }

        J Offline
        J Offline
        jcute
        wrote on last edited by
        #3

        After ctrl+z, I have to use enter. Is it expected?:omg:

        H 1 Reply Last reply
        0
        • J jcute

          After ctrl+z, I have to use enter. Is it expected?:omg:

          H Offline
          H Offline
          HJo
          wrote on last edited by
          #4

          Yes. If you don't want to wait for enter, you should read one character at a time, e.g. getchar(). Example from MSDN: /* GETC.C: This program uses getchar to read a single line * of input from stdin, places this input in buffer, then * terminates the string before printing it to the screen. */ #include void main( void ) { char buffer[81]; int i, ch; printf( "Enter a line: " ); /* Read in single line from "stdin": */ for( i = 0; (i < 80) && ((ch = getchar()) != EOF) && (ch != '\n'); i++ ) buffer[i] = (char)ch; /* Terminate string with null character: */ buffer[i] = '\0'; printf( "%s\n", buffer ); }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups