Thread: Detecting that enter has been hit

  1. #1
    Registered User
    Join Date
    Dec 2020

    Detecting that enter has been hit

    I've been stuck on this assignment for a while. Basically what it wants me to do is to have the user input a phrase, detect how many words are in the phrase and then to print out the words one by one. One of the issues I'm having is that it wants me to break the code whenever the enter key is hit. I'm just not entirely sure how to do this. This is for an introductory programming class, I don't know if there's a relatively simple way of doing it, but any help is appreciated. Here's my code so far.
    #include <stdio.h>
    void check(int a, char str[a]){
    	int i=0;
    	for(i=0; ;i++){
    		if(getchar() == '\n'){
    void white(int a, char str[a]){
    	int space=0;
    	for(int i=0; str[i]!='\0'; i++){
    		if(str[i] == ' '){
    	printf("%d", space);
    int main(){
    	int a;
    	printf("Please enter the estimated length of your phrase: ");
    	scanf("%d", &a);
    	char str[a];
    	printf("Please insert your phrase: ");
    	return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    The %s format only reads printable characters. It ignores all white space, and that includes new lines.

    To read an actual line, spaces and a newline at the end, use the fgets function.

    As in
    fgets( str, sizeof(str), stdin );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User enter text and save to text file (end enter text using -1)
    By DecoratorFawn82 in forum C Programming
    Replies: 7
    Last Post: 12-28-2017, 04:23 PM
  2. Detecting +/-1.#J
    By george7378 in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2010, 05:12 AM
  3. Replies: 9
    Last Post: 02-22-2009, 11:50 PM
  4. detecting enter w/o locking display
    By dgates in forum C Programming
    Replies: 9
    Last Post: 07-07-2004, 06:58 PM
  5. Detecting the OS
    By John.H in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2003, 09:50 AM

Tags for this Thread