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.

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;
}

Edited 6 Years Ago by Ancient Dragon: n/a

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.

Edited 6 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.