reading an input line using scanf() with %c

This is a discussion on reading an input line using scanf() with %c within the C Programming forums, part of the General Programming Boards category; Hi. I'm new to programming in C and I was trying to figure out how to make a program to ...

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    reading an input line using scanf() with %c

    Hi. I'm new to programming in C and I was trying to figure out how to make a program to read in a line of input that ends only when you press enter, but it has to use scanf() with %c. I know how to use %s but I need to know how to do it with %c as well. After the program reads in the input it's supposed to display what you entered backward. This is the code I have come up with, it works sometimes, but it tends to add extra characters to the array when you input longer strings:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
         char input[100];
         int count = 0, length = 0;
    
         printf("Input your favorite phrase.\n");
    
         while (     ((scanf("%c", &input[count]) - 1) + input[count]) != '\n')
         {
              count++;
         }
    
         length = strlen(input) - 1;
    
         for (count = length; count >= 0; count--)
         {
              printf("%c", input[count]);
         }
    
         getchar();
         return 0;
    }
    I don't know how I figured out that while loop thing...I just messed with it until it worked... can anyone help me? I'm sure there's a simpler way to do this and maybe explain to me what's happening with this code? Thanks.

    -Eric
    Last edited by linucksrox; 04-04-2004 at 04:33 PM.

  2. #2
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    > while ( ((scanf("%c", &input[count]) - 1) + input[count]) != '\n')

    that, my friend, is downright fancy

    edit:

    count = 99;
    input[count--] = 0;

    ...

    while( ((scanf("%c", &input[count]) - 1) + input[count]) != '\n') {
    ...
    --count;
    };

    ...
    printf("%s", &input[count+1]);
    Last edited by ggs; 04-04-2004 at 02:58 PM.
    .sect signature

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >that, my friend, is downright fancy
    Not exactly the wording I would have chosen though. A slightly better approach would be
    Code:
    while ( count < 9 && scanf ( "%c", &input[count] ) == 1 && input[count] != '\n' )
      count++;
    input[count] = '\0';
    My best code is written with the delete key.

  4. #4
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    Ahh, I understand now.

    I didn't quite understand what ggs was trying to tell me, but just from the last example of code I think my problem has been solved. The main issue was that my output was showing a bunch of garbage that I didn't input, and I think (correct me if I'm wrong) that assigning '\0' to the last character in the array clears up that problem. Also the while loop cleared things up a bit. Mine was a little jumbled... Thank you very much!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think (correct me if I'm wrong) that assigning '\0' to the last character in the array clears up that problem
    Usually. Any functions that work on C-style strings will use '\0' as a terminating character. If there isn't one in your string then they will walk off into oblivion until they find one. The result is usually values larger than comfort allows and garbage being printed (if not crashes and other bad things).

    >Mine was a little jumbled...
    Just a little.
    My best code is written with the delete key.

  6. #6
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Haha, yea that's definitely what was happening. Also that while statement really makes a lot more sense to me and is a lot easier to read. Technically though I shouldn't be using the && operators yet, I haven't officially learned that from the book I'm reading, and this program was one of the "programming excercises." But otherwise I could've just used nested while statements, right?

    while (scanf("%c", &input[count] == 1)
    { while (count < 99)
    { while (input[count] != '\n')
    count++
    }
    }
    something like that...

    by the way how do you put the code into a nice neat little box like you did?
    Last edited by linucksrox; 04-04-2004 at 04:01 PM.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But otherwise I could've just used nested while statements, right?
    Nested if statements you mean.
    Code:
    while (scanf("%c", &input[count]) == 1)
    { 
      if (count >= 9)
        break;
      if (input[count] == '\n')
        break;
      count++;
    }
    input[count] = '\0';
    >by the way how do you put the code into a nice neat little box like you did?
    << !! Posting Code? Read this First !! >>
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading text file by line number
    By whiskedaway in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2009, 11:09 AM
  2. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 11:31 AM
  3. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  5. Reading input with scanf
    By dat in forum C Programming
    Replies: 3
    Last Post: 05-29-2003, 04:54 PM

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