0

I have a calculator application, which enquires the user to input an arithmetic expression like 10 + 15, follow it with an = sign, and press Enter. The program should evaluate the expression and print the result.

However, the answer is on a new line, even though I want it on the same one like

10 + 15 = 25

I tried to use ungetch as well but it didn't work.

So how can I remove that newline character to obtain the upper result?

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
luckystrrrike
  • 265
  • 3
  • 11
  • i don't think so. I use getchar() and I can't proccess characters of some string. – luckystrrrike Mar 29 '16 at 17:18
  • 1
    @chux: I have the impression the OP wants to remove the new-line as it appeared on the terminal after having pressed enter. This cannot be achieved by chopping it off from what had been read, as per the link you show. – alk Mar 29 '16 at 17:19
  • 1
    This does not appear to be a dupe (of the suggested question, anyway), because it's not about removing the newline from the input data, but rather about undoing its effect on the text interface into which it was entered. – John Bollinger Mar 29 '16 at 17:19
  • 2
    You'll need to turn off echoing of all characters and then take that over. Look at ncurses or tcsetattr functionality. Lot of work to get rid of a newline though. – DrC Mar 29 '16 at 17:19

3 Answers3

0

If you are reading a line of input from the console, the user can freely edit the text they type in until they press Enter. When they do press Enter, the cursor immediately moves to the start of the line, possibly scrolling the screen buffer up in the process.

To move the cursor back to the previous line, you will need to use a "curses" library that allows you to send cursor movement commands to move the cursor back up to the previous line, and then to the end of their input. This could be complicated if they used tab characters and/or typed in more than one line of input.

See "ncurses" or, on Windows, PDCurses.

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44
0

If you're using a terminal it's not easy because it's typically waiting on newlines before it does anything. Have a look here...

setvbuf not able to make stdin unbuffered

If you are doing this on a socket or some other stream that you completely controle then you need to use unbuffered input. Have a look here

Buffered and Unbuffered inputs in C

Note, this will make your calculator a bit harder to write. Alternatively you could print the whole line ie input and answer in the terminal.

Community
  • 1
  • 1
Harry
  • 11,298
  • 1
  • 29
  • 43
0

If your terminal supports them, you can use terminal escape codes to move the cursor.

#include <stdio.h>

int main ( ) {
    char input[200] = "";
    int ch = 0;
    int count = 0;

    printf ( "\033[2J");//clear screen and move cursor to upper left corner
    printf ( "\033[8;H");//move cursor to line 8
    printf("Enter equation\n");//\n advances to line 9
    while ( ( ch = getchar ( )) != '\n' && ch != EOF) {
        input[count] = ch;//store input for processing
        count++;//count characters
    }
    printf ( "\033[9;%dH", count + 1);//move cursor to row 9 col count + 1
    printf ( "answer here\n");
    return 0;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16