Thread: Inputting Strings

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    15

    Inputting Strings

    I have a conceptually easy task however I am running into all kinds of problems. The idea is to have a C program give an menu option to the user and then repeatedly ask for strings that will be converted. (i.e. Hex to Dec). I really don't care about the conversion portion right now I am just trying to get over the inputting of strings hurdle. Here are the current problems:
    1. Upon running the code the initial selection is read properly however the input buffer or something is causing the fgets() to read that same input and process once before repeating.
    2. I need to input a string....do calcs/output... and then prompt the user again for another string. Over and over again. How can I exit the fgets()?? In other words what does the user do to exit the program?? Ctrl-C works but there must be a more graceful way.

    Thank you in advance.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    // Function Prototypes
    
    
    
    
    // Main Program
    int main()
    {
        char str[25];
        unsigned strl, type = 2;
        printf("Please select the type of conversion you want.\n");
        printf("     1. Convert signed Hex to Dec\n");
        printf("     2. Convert signed Dec to Hex\n");
        scanf_s("%u", &type);
        if (type == 1)
        {
            // Converting from signed Hexadecimal to Decimal
            strl = 0;
        }
        else if (type == 2)
        {
            // Converting from signed Decimal to Hexadecimal
            while(printf("=============================\nPlease enter a signed decimal number: "), fgets(str, sizeof(str), stdin) != NULL)
                {
                strl = strlen(str);
                printf("The string input is: %sThe string length is: %u\n", str, strl);
                }
        }
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Something like this, perhaps?

    Code:
    #include <stdio.h>
    
    int main(void) {
       char s[50];
       
       do {
          printf("Please enter a string or hit enter to quit \n");
          fgets(s, sizeof(s), stdin);
       }while(s[0] != '\n');
    
       printf("Goodbye\n");
       return 0;
    }
    And welcome to the forum!

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    Thank you Adak that works perfectly. I think I also figured out how to clear that one character in buffer. I just used getchar(); and it seems to work properly now.

    All the best!! =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems inputting char and strings
    By Greek_89 in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2011, 08:45 AM
  2. Replies: 5
    Last Post: 03-28-2011, 07:14 PM
  3. why its not inputting as before..
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 06:11 AM
  4. Inputting strings from a file
    By Mystic_Skies in forum C Programming
    Replies: 7
    Last Post: 11-17-2004, 04:08 PM
  5. Inputting Strings from a file
    By Drealoth in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 11:11 PM