Thread: How to take user input return key?

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    2

    How to take user input return key?

    So i am trying to make a talk program with vim. And i want to know how to take the return key input from the user and then let the program continue. I am doing this on a mac in Terminal btw.

    Code:
    #include <stdio.h>
    
    main int()
    
    {
      char name
    
     prinft("What is your name?");
     scanf("%c", &name);
    
     printf("Hello %c, how are you?", name);
    
     //then here i want to let the program only continue when the user has input the return key
    
     //for example the user can say: I am doing fine "return key"
    
     //and then the program continues
    
     printf("I am happy you are doing well, goodbye");
     return 0;
    
    }
    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    15
    Use getch(0) to pause your program. It will continue when you hit any key.
    If you want it to be the 'return' key' its a little more involved.

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    309
    I'd advise you to look through some online examples about C-strings if you aren't using a textbook that teaches the language.

    "%c" is the format specifier for characters. What you want is "%s", the format specifier for C-strings. Also, "char name;" is just one single character (like 'a', 'b' or 'p', etc). What you want is a character array (aka C-string). Something like "char name [5];" which could then be used to store 4 characters (say 'Z', 'e', 'u', 's', '\0') where '\0' is the string terminator.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  4. #4
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    309
    No, do not use getch(). It's a non-standard function (and I think it's not supported by modern compilers). Also, getch() doesn't accept any parameters and so getch(0) won't compile.

    The OP doesn't need getch() to "wait" for the user to press the enter key. Once the user types their name, they'd anyway have to hit enter so as to "submit" the input for the program to continue running.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    2
    Thanks guy's! The %s did the trick.

    For the once's interested in this, here is my first talkbot .

    Code:
    #include <stdio.h>
    
    
    int main()
    {
    
    
    	char name[31];
    	char lastname[31];
    	char nothing[100];
    	char feeling[100];
    	char nothings[100];
    	char ch[100];
    
    
    	printf("Hi, what is your name?\n");
    	scanf("%s", name); 
    
    
    	printf("Hello %s.\nHow are you doing?\n", name);
    			
        			scanf("%s", ch);
    				
    			printf("I see, how would you describe that in one word?\n");
    			scanf("%s", feeling);
    
    
    			printf("Feeling %s is one of the many feeling we all have sometimes.\nIs there anyhting else you want to share with me?\n", feeling);
    			
    			scanf("%s", nothings);
    	
    			printf("Thank you for sharing %s. I hope the rest of your day will be very nice.\n", name);
    			
    	return 0;
    }
    If you see some incorrect stuff im doing please let me know.

  6. #6
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    198
    To be safe, you should limit the maximum number of characters in the scanf call, ie:

    Code:
    scanf("%30s", name);
    It's one less than the size of the actual buffer to ensure room for the null terminator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to return user input in C
    By tuxdeep in forum C Programming
    Replies: 4
    Last Post: 02-26-2013, 11:41 PM
  2. scanf to return after user presses enter?
    By sp2 in forum C Programming
    Replies: 9
    Last Post: 04-23-2008, 05:33 PM
  3. Replies: 9
    Last Post: 03-27-2006, 05:28 AM
  4. User Input
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2003, 01:03 AM
  5. using user input as a var.
    By nubi in forum C++ Programming
    Replies: 13
    Last Post: 05-06-2003, 12:27 PM

Tags for this Thread