kenneth
kenneth

Reputation: 1

Using gotoxy for printf with newlines

I am creating digital clock with an output using ascii and I made a large number consisting of five lines. I want to move the number using gotoxy but only the first line moves and the rest line is ignoring the y coordinates.

How can I move my entire number using gotoxy while my printf has newlines?

#include <stdio.h>
#include <time.h> 
#include <windows.h>
#include<conio.h>
void gotoxy(int col, int row);

void disp0();
int count=219;
int main()
{

gotoxy(10,10);disp0(); 

}

void gotoxy(int col, int row)
{
COORD coord;
coord.X = col; coord.Y = row;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void disp0(){
    printf("%c%c%c\n%c %c\n%c %c\n%c %c\n%c%c%c",count,count,count,count,count,count,count,count,count,count,count,count);//0

}

This is the output I get:

enter image description here

Upvotes: 0

Views: 217

Answers (1)

Lundin
Lundin

Reputation: 213990

You need to do a gotoxy and a printf for every individual digit. No \n should be used. If you want to be fancy you can use trigonometry, like in this very sloppy example:

#include <stdio.h>
#include <windows.h>
#include <math.h>

void gotoxy(int x, int y)
{
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), 
                           (COORD){ .X=x, .Y=y });
}

int main() 
{
  const int center_x = 12;
  const int center_y = 12;
  const int radius = 6;
  double angle = 0;

  int clock [12] = {3, 2, 1, 12, 11, 10, 9, 8, 7, 6, 5, 4};

  for(int i=0; i<12; i++)
  {
    int x = round(radius * cos(angle));
    int y = round(radius * -sin(angle)); // - to shift y axis on console coord system
    angle += (2.0 * 3.1415) / 12.0; 

    // 2*x rough estimate to compensate for font height vs width:
    gotoxy(center_x + 2*x, center_y + y); 

    printf("%d", clock[i]);
  }
  gotoxy(1,center_y*2);
}

Ugly output:

            12
      11          1

  10                  2


9                       3


  8                   4

      7           5
            6

Upvotes: 1

Related Questions