This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Help with printf, gets

Can someone help me get this to work? I want to be able to receive an input on the serial window and then display that input. The problem is the first text repeats forever with a while loop, and without a loop the program displays text but doesn't respond at all. What am I doing wrong? It seems like this should be simple.

#include<stdio.h>
#include <reg51.h> //Used for serial interface

int main () {
     char string [50];

         //Required for Serial Interface
     SCON = 0x52;       // serial port configuration
     TMOD = 0x20;
     TCON = 0x40;
     TH1 = 0xf3;        // 2403 baudrate @12mhz

         //while (1) {


  printf ("Insert sentence: ");
  gets (string);
  printf ("Your sentence is: %s",string);
  //return 0;
  //}
  }

Parents
  • Nifty. On the man page for gets the example code is practically the inverse of what I need.

    void tst_gets (void) {
      char buf [21];
    
      do {
        gets (buf, sizeof(buf)-1);
        printf ("Input string \"%s\"", buf);
      } while (buf [0] != '\0');
    }
    


    So I try

    while(1){
    
    char msg[60];
    
    printf("Type a sentence and press ENTER\n");
    
    while (msg [0] != '\0')
    {gets (msg, sizeof(msg)-1);
    
    //puts(msg);
    printf("%s\n",msg);
    }
    }
    


    Which again just loops the "type a sentence" text. Frustrating.

Reply
  • Nifty. On the man page for gets the example code is practically the inverse of what I need.

    void tst_gets (void) {
      char buf [21];
    
      do {
        gets (buf, sizeof(buf)-1);
        printf ("Input string \"%s\"", buf);
      } while (buf [0] != '\0');
    }
    


    So I try

    while(1){
    
    char msg[60];
    
    printf("Type a sentence and press ENTER\n");
    
    while (msg [0] != '\0')
    {gets (msg, sizeof(msg)-1);
    
    //puts(msg);
    printf("%s\n",msg);
    }
    }
    


    Which again just loops the "type a sentence" text. Frustrating.

Children