Thread: How to input a char or int

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    16

    How to input a char or int

    Hello I am trying to figure out how I can get an input that matches this printf..

    Code:
    printf("Enter an integer or character you would like to DELETE from the file:");
    I tried this code..
    Code:
    scanf("%c", deleteMe);
    	printf("\n\n");
    	deleteMe2 = (int)deleteMe - 48;
    	printf("%c", deleteMe);
    	printf("%i", deleteMe2);
    But using %c only accepts one input.

    I've tried using %s but I'm having a harder time converting it to an int.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    So just to clarify - Say you have a string that looks like this

    Code:
    {'a', 'b', 'c', 'd', 'e', '\0'}
    Then the user entered 3 - You would want the string to become
    Code:
    {'a', 'b', 'd', 'e', '\0'}
    
    ('c' deleted)
    Is that what you want?
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Try using %s and convert it to double yourself. Roughly,

    Code:
     
    scan string
    For each char in string
           sum  += pow(10,char_position)*atoi()
    You need to perform extra checks
    to validate user input. (For example if the char represents a valid int)

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    What do you want to do if the user enters an invalid input (like "foo", or "42c", or "<BUNCH OF SPACES>")?
    Whatever you want to do, you have to detect the invalid input.
    Code the invalid input detection and what is left is a valid input :-)

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try reading the input as a line (using fgets(), not gets()). Then interpret the contents of that string to work out if it contains a single character, a digit, or is invalid.

    You can't expect scanf() to do that for you, at least not alone, and not all that well.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i=0;
        int a;
        char name[]="Hello!",k;
        printf("Enter the nth charecter you want to delete\n");
        scanf("%d", &a);
        while (i<=5)
        {
            
            if (i!=a-1)
            printf("%c", name[i]);
            if(i==a-1)
            k=name[i];
            i++;
        }
        printf("\n");
        printf("%c is deleted\n", k);    
        return 0;
    }

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Enter the nth charecter you want to delete
    4
    Helo!
    l is deleted
    no, it's not: Hello!

    Generally answers are expected to work, if you are just going to hand them out. But don't hand them out, explain instead. Spoonfeeding encourages people to violate the homework policy.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    @whiteflags
    I was just practicing myself by answering questions.
    Is there any error in my programe please let me know .
    I will take that as a guidline. If it is not allowed here, then sorry.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> Is there any error in my programe please let me know .

    What you showed gives the appearance of working, but it doesn't actually remove any elements from the string. There are lots of ways to go about doing it properly but I think there are two general needs. If you want to delete a single character from a string, you will end up shifting every character after the character to delete to the left once (closer to the 0 index). When you shift, you want to loop in the opposite direction of shifting so we will loop until we reach the rightmost index.

    I created a helpful program so you can see the characters moving around.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int getint(int len)
    {
        int x;
    
        do {
            printf("Enter the index you want to remove.\n");
            if (scanf("%d", &x) != 1) {
                int ch;
                while ((ch = getchar()) != '\n' && ch != EOF) // ignore garbage
                    ;
                clearerr(stdin);
                continue;
            }
        } while (x >= len || 0 > x);
    
        return x;
    }
    
    void shift(char *body, int x, int len)
    // Shift body to the left one space, starting at x.
    {
        if (body[x] == '\0') {
            return; // Nothing to be done.
        }
    
        if (x == 0) {
            x++;
        }
    
        for ( ; x < len; x++) {
            printf("* index %d, new index %d ", x, x - 1);
            if (!isprint(body[x]))
                printf("character %d\n", body[x]);
            else
                printf("character %c\n", body[x]);
            body[x - 1] = body[x];
        }
        body[x - 1] = '\0';
    }
    
    int main()
    {
        int x;
        int i;
        char hello[] = "Hello, world.";
        int len = (int)strlen(hello);
    
        for (i = 0; i < 3; i++) {
            printf("\"%s\"\n", hello);
            x = getint(len);
            shift(hello, x, len);
            printf("\"%s\"\n\n", hello);
        }
    
        return 0;
    }
    Of course, you can remove characters based on almost any criteria. More generally, you will want to focus on what you need to keep in the string, rather than deleting what you need to remove. A good way to look at an implementation of this is to look at the code for remove_if() from C++. That is the general idea. The implementation they show you merely requires understanding pointers.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Thanks for the helpfull info whiteflags, greatly appreciated.
    To newbees like me you and others are doing a great help.

    Sincerely Justine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with input char
    By drewtoby in forum C Programming
    Replies: 4
    Last Post: 04-29-2011, 05:52 PM
  2. How can terminate input when input specifed char?
    By Mathsniper in forum C Programming
    Replies: 5
    Last Post: 12-11-2006, 09:59 AM
  3. Function input may char or char[] ..?
    By GSalah in forum C++ Programming
    Replies: 2
    Last Post: 11-15-2006, 04:07 AM
  4. while loop char input
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 04-09-2006, 05:19 PM
  5. Char input
    By mkeef in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 03:54 PM