Re-reading chars from stdin

This is a discussion on Re-reading chars from stdin within the C Programming forums, part of the General Programming Boards category; Hi all, First post, so go easy on me! Writing a very basic command line calculator in C at the ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    Question Re-reading chars from stdin [RESOLVED]

    Hi all,

    First post, so go easy on me!

    Writing a very basic command line calculator in C at the mo. The user interface is as follows:

    number <space> number <space> operator

    So user can enter 2 (or more) numbers separated by a space and then the mathematical operator to use on them.

    e.g "1 2 3 +" is the same as 1 + 2 + 3

    My program works by reading in the first char and then determining whether it's a number or an operator (by seeing if the char is in the ASCII range for numbers 0-9).
    If the char is a number, it calls a function to read in the whole number (may not just be a single digit, e.g. 34, 5.385...) by calling scanf() and using the %f conversion specifier to store a float (remember numbers are separated by whitespace, so this is ok).

    BUT, here lies the problem, because I already used getchar() to get the first digit of the number, so when I use scanf() it misses off the first digit, and I cannot for the life of me find a suitable function in stdio.h that lets me move the file reader position back 1.

    Sorry I've gone on for so long, but can anyone help me out? I'd greatly appreciate it!

    Thanks,

    James
    Last edited by Uncle Albert; 04-01-2005 at 12:23 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Why not use fgets to grab a whole line, and then parse the line using sscanf or such?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    Quote Originally Posted by Dave_Sinkula
    Why not use fgets to grab a whole line, and then parse the line using sscanf or such?

    Thanks for the suggestion, but the program is for a set exercise, where I'm editing someone else's program (it originally required the user to enter 'n' between each number), and it has specifically told me how I have to go about coding the solution.

    Is there no easy way to do what I'm trying to do? The exercise specification says "you can get the next character from input using getchar() and put it back on the input again using putchar()". I've used getchar(), but from all the documentation I've read it looks like putchar() can only be used to print to stdout - not exactly what I want! Has my lecturer got it wrong, or am I missing something?

    Thanks,

    James

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Perhaps ungetc rather than putchar?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    You could try using a function where the output location is accepted as an argument, but instead of using stdout, use stdin. Whether that would put the output into the input buffer, I don't know, but if it does, this might be a route to solving your problem. Those characters would be put onto the end of the buffer queue, though.

    Personally, I would create an array and set the program to read one character at a time and store it in the array. Then make a separate function for reading those values and converting it into an int or float. This is kinda the same thing Dave said, though, so I don't know if it would go against your directions.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    Smile

    Quote Originally Posted by Dave_Sinkula
    Perhaps ungetc rather than putchar?
    Spot on! Thought I'd tried it, but guess not. Passing stdin as the stream (as I see Jaken Veina suggested after I'd tried it, thanks ) seemed to do the trick.

    Program now seems to be stuck in an inifinite loop, but it's returning the right answer, which is the main thing!

    Thanks so much to everyone who helped me out - added rep for all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading issue, mixed text file
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-22-2009, 05:38 AM
  2. reading and writing more than just chars to fstreams
    By DominicTrix in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2005, 09:58 AM
  3. reading chars with scanf
    By owi_just in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 12:48 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 11:41 AM

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