Please help... This is driving me nuts. I have been searching for an answer to this for a few days now.

In c...

How do you stop a line feed or new line after pressing enter for an input. I want the printed information to appear on the same line as the input. Nothing I have tried prevents the cursor from moving to the next line after typing an integer then pressing enter. There has got to be a simple answer to this.

My appologies if this has been answered before, but my searches keep coming up empty.

Here is what I get:

Input Output
27
27

What I want it:

Input Output
27 27

Thanks for any assistance you can provide.

Dani AI

Generated

You are running into a terminal behavior, not a C language feature. When you press Enter in canonical mode, the terminal echoes a newline and moves the cursor to the next line. ISO C has no cursor-positioning or echo control, so you must either use a terminal library (ncurses/terminfo) or manage the terminal with POSIX APIs or escape sequences. On Linux, termios controls echo and canonical mode, while libraries like ncurses give you move()/printw() to place text wherever you want. (c-faq.com, man7.org)

If you just want a light-weight fix that works on ANSI/VT-compatible terminals (most Linux terminals), read the number normally, then move the cursor back to the previous line and print your output after the digits you typed:

#include <stdio.h>

int main(void) {
    int n;
    printf("Input    Output\n");
    if (scanf("%d", &n) == 1) {
        char buf[32];
        int len = snprintf(buf, sizeof buf, "%d", n);  /* number of digits typed */
        if (len < 0) len = 0;
        printf("\033[F");                 /* go to start of previous line */
        printf("\033[%dC %d\n", len + 1, n); /* skip past input and print output */
        fflush(stdout);
    }
    return 0;
}

The \033[F (CPL: cursor to previous line) and \033[nC (CUF: cursor forward n cols) control codes are defined by ECMA-48 and are implemented by the Linux console and common terminal emulators. (man7.org)

For a more robust, portable approach on SUSE, follow /@Ancient Dragon’s suggestion and use ncurses: read input with getstr()/wgetnstr(), then move(y, x) to the same line and printw() the result, finishing with refresh(). Link with -lncurses. See the curs_move man page for details. (man7.org)

Recommended Answers

All 7 Replies

You may want to investigate conio.h or curese.h libraries for this behaviour.

commented: No one sane should be using those relics. -1

With a MS-Windows 32-bit (or 64-bit) compiler you will have to use the console functions in windows.h to get and set the current cursor position. Something like this:

#include <Windows.h>
#include <stdio.h>

void gotoxy(int x, int y)
{
    // set the current cursor position
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

COORD getxy()
{
    // get the current cursor position
    CONSOLE_SCREEN_BUFFER_INFO info;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
    return info.dwCursorPosition;
}

int ndigits(int n)
{
    // calculate the number of digits in an integer
    int x = 0;
    while( n > 0)
    {
        x++;
        n /= 10;
    }
    return x;
}

int main(int argc, char* argv[])
{
    int x,y;
    COORD coord1, coord2;
    printf("Enter two numbers\n");
    coord1 = getxy();
    scanf("%d", &x);
    coord2 = getxy();
    if( coord2.Y != coord1.Y )
    {
        gotoxy(coord2.X+ndigits(x)+1, coord1.Y);
    }
    scanf("%d", &y);
    
	return 0;
}

Thanks, I will go take a look at conio.h and curese.h

Thanks for the windows.h info (and sorry I should have mentioned it...), but I'm running Suse Linux and compiling with gcc. So, I need a more universal answer to this using hopefully the C Standard Library.

There is no universal answer -- only 3d party libraries such as curses.h. You can do something similar to what I posted by using terminfo functions but it would be easier to just use curses. I looked at conio.h and did not see anything useful there, at least not in the Microsoft port of that file.

May be this could fix your problem , run it .
I tried it worked, I cant figure out any other alternate way to it in C

#include<graphics.h>
#include<conio.h>
#include<stdio.h>

int main()
{
int ip;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cscanf("%d",&ip);
getch();
printf("Input Output \n");
printf("%d    %d",ip , ip);
getch();
return 0;
}

One thing which is very important is that you need to
input your number on screen through keyboard blindly
without computer prompting you

In a nutshell:
Just enter your number once
and then press enter key

You will have your result!

May be this could fix your problem , run it .
I tried it worked, I cant figure out any other alternate way to it in C

#include<graphics.h>
#include<conio.h>
#include<stdio.h>

int main()
{
int ip;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cscanf("%d",&ip);
getch();
printf("Input Output \n");
printf("%d    %d",ip , ip);
getch();
return 0;
}

Let me guess -- you are using Turbo C or Turbo C++ compiler. graphics.h is obsolete and not supported by any modern compiler, although there might be a 32-bit port of it that I am not aware of.

And the OP has already stated that he is using *nix, not MS-Windows. So that code, like the one I posted, is of no value to him.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.