0

I don't know much about what I can play with CMD (And English too). So I would like to ask. Can we use printf to print characters until it covers all CMD in one line? For example. In CMD at one line, I want to print " = " until the end of the line no matter what CMD size.

like this

CMD Screen

========================================================================================
 "Other character"
========================================================================================

I try to do this because I need to make a game in CMD with the C language for my homework project. and I want to print it like that because it will be my first screen in the game (Like the logo at the start).

I'm using VS code but my teacher using Dev-C++ and the CMD size is not the same. So, I try to fix it right now.

This is I want with any CMD size (I run this with Dev-C++)

Thank you for your answer in advance.

  • 2
    You can call the Windows API [GetConsoleScreenBufferInfo](https://learn.microsoft.com/en-us/windows/console/console-screen-buffer-info-str) function which gets the screen buffer size amongst other info. You might need to print one character less than the width, or an automatic line feed might be issued and the next line jumps a line. – Weather Vane Nov 09 '22 at 16:57
  • 1
    What should happen if the user resizes the CMD window while your program is running? – Bodo Nov 09 '22 at 17:09
  • @Bodo I want "=" to still cover all space at the line. Can I do that? – LunaticCoder Nov 09 '22 at 17:13
  • 1
    The *previous* outputs too? For any next output you can retrieve the buffer info just before you make the output. – Weather Vane Nov 09 '22 at 17:14
  • @LunaticCoder if the output has been written and the cmd windows is resized after, then you cannot do anything. – Jabberwocky Nov 09 '22 at 17:14
  • @WeatherVane yes. – LunaticCoder Nov 09 '22 at 17:16
  • @Jabberwocky that's so sad... – LunaticCoder Nov 09 '22 at 17:17
  • 1
    Please [edit] your question and add more background information. What is your program supposed to do? How do you want the whole output to look like? Maybe you can implement your user interface with `curses`. – Bodo Nov 09 '22 at 17:19
  • 1
    You might be able to get a window resize notification but it's making a lot of work. Just let it scroll off and get the size before any such output. It's just a console. – Weather Vane Nov 09 '22 at 17:20
  • 1
    Related previous question: [Is there a resize event for screen in Windows](https://stackoverflow.com/questions/7279867/is-there-a-resize-event-for-screen-in-windows) but now you have to implement the GUI messaging system. – Weather Vane Nov 09 '22 at 17:22
  • 2
    Read up on the ncurses library. It exists for Windows too, although setting it up could be a bit tricky, and using it is not for the faint of heart either. – n. m. could be an AI Nov 09 '22 at 17:34
  • @WeatherVane I edited the question to be clearer about what I want. Could you read it and help me again? – LunaticCoder Nov 09 '22 at 17:34
  • I don't get why you need to pander to the user changing the screen size *during* a game. They can set the console size they want, and at the start of a game you get the console size and arrange the output accordingly (including the title, which you have not centred). Personally, I would leave that kind of embellishment to one side, and get on with the game itself. Come back to it if you have time afterwards. For now, just get the screen size and work with that: you did give the reason "my teacher's screen size is different" which is quite another matter than dynamically resizing the game. – Weather Vane Nov 09 '22 at 17:39
  • Simplest solution is to query the environment variable COLUMNS to get the width of the display. If the user does not set that value properly, call it their problem. To do this correctly will be a lot of work, so just do the simple thing first. – William Pursell Nov 09 '22 at 17:45
  • @WeatherVane I think that too but when I don't know how to fix it. It just bothers me so I write this in advance to use when I'm polishing the game. – LunaticCoder Nov 09 '22 at 17:46
  • see https://stackoverflow.com/q/6812224/10622916 – Bodo Nov 14 '22 at 10:46

0 Answers0