Thread: Replace scanf() and fgets() with getchar()

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    18

    Replace scanf() and fgets() with getchar()

    I'm new to C programming and I am currently trying to alphabetize a list of words. As of now I have a working program (posted below), however I am trying to run it on a microprocessor that does not allow the use of scanf() or fgets(). I was wondering if there is a way around this by using getchar() and putchar() but still use the same logic to alphabetize the list.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    void main() {
       int i, j, n;
       char s[10][20], t[20];
    
    
       scanf("%d",&n);                           //reads user input
       for (i = 0; i <= n; i++)
          fgets(s[i],20,stdin);
    
    
       for (i = 1; i < n; i++) {                 //sort through and compare words
          for (j = 1; j < n; j++) {
             if (strcmp(s[j - 1], s[j]) > 0) {
                strcpy(t, s[j - 1]);
                strcpy(s[j - 1], s[j]);
                strcpy(s[j], t);
             }
          }
       }
    
    
       printf("\nSorted List");                //print sorted words
       for (i = 0; i <= n; i++)
          printf("%s", s[i]);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zeldac
    I was wondering if there is a way around this by using getchar() and putchar() but still use the same logic to alphabetize the list.
    Yes. Instead of fgets with stdin you can use getchar to keep reading characters until the array is filled, or a newline character, EOF or a read error is encountered. Instead of using scanf with %d you can use getchar to read characters into a string until the array is filled, or whitespace, EOF or a read error is encountered, then convert the string using say, strtol.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    18
    Quote Originally Posted by laserlight View Post
    Instead of fgets with stdin you can use getchar to keep reading characters until the array is filled, or a newline character, EOF or a read error is encountered.
    Thank you for your response. But I have two questions.

    1) With fgets, I am reading stdin into the array s[i]. However when I try to read getchar into the array s[i], I get an error "incompatible types when assigning char[20] and char." I'm assuming I get this error because of the double array, so how would I fix this?

    2)I want the input to to be read in line by line. From your response you say I understand i can read getchar into an array until a new line character. So I'm trying to understand that once I hit new line will it stop reading from stdin? Or will it read line by line? I'm just a little confused by that

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Try using a loop to read in a line of input.
    I suggest writing a function that contains this loop.
    I would pass the function an char array and its size.
    Might return the number of chars read into the array.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    What Microcontroller/Compiler are you using?
    Most c compilers have ways of using printf and scanf with USART/UART - Providing you set it up correctly.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    18
    Quote Originally Posted by Click_here View Post
    What Microcontroller/Compiler are you using?
    Most c compilers have ways of using printf and scanf with USART/UART - Providing you set it up correctly.
    I'm using the nexys 3 board.

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    18
    Okay, I have attempted to read in the characters into an array, and this is what I have but it does not work. Right now it only put the number back to the terminal and then it stops putting characters. How could I fix this? Am I even on the right track???
    Code:
    #include <stdio.h>#include "platform.h"
    #include "xparameters.h"
    #include <string.h>
    
    
    
    
     int main()
    {
        int i, j;
        char  string[10][20], t[20];
        int num;
    
    
        while(1)
        {
            char ch = getchar();
            if(ch == 0)
                continue;
    
    
            if(ch <= 0x39 && ch >= 0x30)  //to get number of words in list
            {
                putchar(ch);
                num = ch - 0x30;
            }
            
            return num;
    
    
            if (ch <= 0x61 && ch >= 0x7A) 
            {
                putchar(ch);
                for(i=0; i<=num; i++)
                {
                    for(j=0; j<=20; j++)
                        ch = string[i][j];
                }
            if(ch == 0x0D)
                break;
    
    
            }
        }
    }
    I apologize if the code is "rough", like i said in my original post I am new to this. Thank you in advance for any recommendations.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    As soon as code execution reaches line 28 (the "return" statement), the program ends. Nothing after that will be executed.

    Code:
    if (ch <= 0x61 && ch >= 0x7A)
    Mistake here. How can 'ch' be both less than 0x61 and greater than 0x7A?

    You should also use character literals for character comparisons, instead of hex values. i.e.

    '0' instead of 0x30
    'a' instead of 0x61
    'z' instead of 0x7A
    etc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets after scanf, skips over fgets?
    By willane1965 in forum C Programming
    Replies: 1
    Last Post: 08-17-2014, 11:13 PM
  2. scanf getchar
    By hit in forum C Programming
    Replies: 5
    Last Post: 08-21-2012, 02:47 AM
  3. getchar and scanf
    By begin in forum C Programming
    Replies: 9
    Last Post: 06-20-2010, 07:51 PM
  4. scanf, getchar, gets, etc. are ignored.
    By daltore in forum C Programming
    Replies: 19
    Last Post: 12-27-2007, 10:47 PM
  5. scanf and getchar
    By axe in forum C Programming
    Replies: 7
    Last Post: 01-11-2006, 04:45 AM

Tags for this Thread