Beginner question- user input termination

This is a discussion on Beginner question- user input termination within the C Programming forums, part of the General Programming Boards category; I'm a beginner to C, so please try not to laugh at the following question... I am writing a short ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Beginner question- user input termination

    I'm a beginner to C, so please try not to laugh at the following question...

    I am writing a short program that asks the user to input a string, then a small function manipulates the string. It then loops back to scanf, waiting for more user input. I understand how to end the loop if the user enters a particular string, but I want to end it when the user enters ctrl+d without having to press enter. What am I missing? Thanks.

  2. #2
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    Add an If statement kinda like this i would assume.

    if (strstr(argument, 'whatever the value for cntrl-d is')
    {
    exit(1);
    }

    I think, i'm a novice to, but thats where i'd start.
    To error is human, to really foul things up requires a computer

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    15
    Thanks for the response, Twiggy. However, if I'm not mistaken, I think that solution would require the user to hit enter to terminate the string, which, unfortunately, is not an option. I want the program to end immediately if the user hits ctrl+d and not wait for the user to hit enter.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I'm a bit unsure, but...
    Code:
    char string [100];
    setbuf (stdin, NULL);
    for (;;)
    {
     for (i = 0, c = getchar(); c != CTRL_D && c != '\n'; c = getchar())
     {
       string[i++] = c;
     }
     if (c == CTRL_D) break;
     string[i] = '\0';
     doStringStuff(string);
    }
    Of course, this all rests on setbuf (stdin, NULL). If it doesn't work, then you'll have to look into the compiler dependent BIOS functions for your compiler.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Truncating user input
    By CS_Student8337 in forum C Programming
    Replies: 10
    Last Post: 03-19-2009, 12:34 AM
  2. Defining classes following user input. Inheritance.
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2009, 09:21 AM
  3. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 11:59 AM
  4. Ending user input with # character
    By jowatkins in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2004, 10:41 AM
  5. Replies: 4
    Last Post: 04-21-2004, 04:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21