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
  • That still just loops the "Insert sentence" text.

    I got it to work this way but its not what I want:

    while(1){
    
    char msg[60];
    char ch;
    int i=0;
    
    printf("Type a sentence and press ENTER\n");
    
    while ((ch=getchar()) != '\n')
            msg[i++]=ch;
    
    msg[i]='\0';
    
    i=0;
    while(msg[i] != '\0')
            putchar(msg[i++]);
    printf("\n");
    }
    

    Can someone help a novice translate this into gets and puts form? I want to be able to manipulate strings later.

Reply
  • That still just loops the "Insert sentence" text.

    I got it to work this way but its not what I want:

    while(1){
    
    char msg[60];
    char ch;
    int i=0;
    
    printf("Type a sentence and press ENTER\n");
    
    while ((ch=getchar()) != '\n')
            msg[i++]=ch;
    
    msg[i]='\0';
    
    i=0;
    while(msg[i] != '\0')
            putchar(msg[i++]);
    printf("\n");
    }
    

    Can someone help a novice translate this into gets and puts form? I want to be able to manipulate strings later.

Children