Thread: prompt user for input and display character in every line

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    1

    prompt user for input and display character in every line

    hi im having problems printing "->" on the beginning of each line, im trying to do it as a loop and ending it when the user types "q". also i would like to know how to ignore text from the user when the input begings with a specific character. heres what ive done so far, and not currrently working as expected.

    Code:
    #include <stdio.h>
    int main (void)
    {
    char prompt;
    printf("~~~ FRACTION CALCULATOR ~~~\n");
    printf(">");
    prompt; getchar();
    while (prompt !='q')
    {
    printf(">");
    }
    return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Consider something like
    Code:
    char *doPrompt ( const char *prompt, char *buff, size_t buffsize ) {
        printf("%s",prompt);
        fflush(stdout);
        return fgets( buff, buffsize, stdin );
    }
    
    int main ( ) {
        char buff[BUFSIZ];
        while ( doPrompt("> ", buff, BUFSIZ ) ) {
            printf("Response=%s", buff);
        }
        return 0;
    }
    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. Print character x times based on user's input
    By Kecupochren in forum C Programming
    Replies: 5
    Last Post: 12-21-2012, 12:03 PM
  2. getting input from user(character) converting to binary
    By saravana in forum C Programming
    Replies: 4
    Last Post: 09-24-2009, 12:47 PM
  3. Replies: 3
    Last Post: 07-07-2006, 08:53 PM
  4. Replies: 5
    Last Post: 05-06-2004, 09:14 AM
  5. Ending user input with # character
    By jowatkins in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2004, 10:41 AM