0

I'm working on a text adventure game:

#include <string>
#include <cstdio>


using namespace std;

int main (){
cout << "Welcome to text adventure! Do you want to play? (y/n)\n";

char ansr;
cin >> ansr;

while (ansr == 'y' || 'Y'){
    cout << string (50, '\b');
    cout << "Okay, let's go!";
    return 0;
}
}

This works alright, but for readability I want the new text "okay, let's go!" to appear at the top of the screen. This is part of an educational assignment, but i really like my idea for my text adventure game. system(CLS); Does the job but i've been reading that there are some major portability issues with this since it can only work with windows.

I've tried resplacing \n with \b and it doesn't do anything because \b is only meant to erase what the console is typing. I'm wondering if there's a way the earlier things that were printed on the console. If not, are there any other alternatives to system(CLS) other than what I have on there to allow the new text to be bumped up to the top of the screen?

  • 4
    Standard C++ has no facilities to manipulate the screen in this way. This type of screen control is either done by platform-dependent libraries or OS calls. – PaulMcKenzie Mar 29 '20 at 01:47
  • 1
    `ansr == 'y' || 'Y'` is a bug. This is always true. – drescherjm Mar 29 '20 at 01:52
  • i think `gotoxy()` from `` can still be used today. – acegs Mar 29 '20 at 01:53
  • @acegs No, conio.h does not exist. – Zan Lynx Mar 29 '20 at 01:55
  • On some compiler but its not standard `c++` – drescherjm Mar 29 '20 at 01:55
  • it's `c`. we used that years ago in `TURBOC` compiler. i don't know if there's a version that can be used alongside `c++` in compilers nowadays. i think they shouldn't remove those `gotoxy()` like functions since it's useful for text-only programs. – acegs Mar 29 '20 at 01:58
  • You might want to look up [ncurses](https://linux.die.net/man/3/ncurses). On Windows, look for [pdcurses](https://pdcurses.org/). This gives you a *reasonably* portable way to manipulate the screen. – Jerry Coffin Mar 29 '20 at 01:59
  • That's because as @PaulMcKenzie eludes, how the terminal is controlled is specific to that one terminal. (though there are a few standard models like those with VT100 emulation, etc..) There are 3 main approaches -- all NON-portable to the other. (1) conio.h for DOS/windows; and (2) ANSI escape sequences and (3) `tput` facilities for Unix/LInux terminals. Libraries like ncurses, attempt to standardize for Unix/Linux. – David C. Rankin Mar 29 '20 at 01:59
  • 1
    @acegs the last TurboC (Borland) release was 1997, nearly 2.5 decades ago. – David C. Rankin Mar 29 '20 at 02:00
  • @DavidC.Rankin -- You also have the ConsoleAPI calls for MSWindows. – PaulMcKenzie Mar 29 '20 at 02:01
  • @PaulMcKenzie -- right. Missed that one -- probably because I never used it `:)` – David C. Rankin Mar 29 '20 at 02:02
  • @drescherjm: yeah i noticed it always does this first one even when i put in a while loop for ```ansr == 'n' || ansr == 'n'```, why does it behave like this? I'm pretty new to c++. –  Mar 29 '20 at 02:20
  • @DavidC.Rankin conio.h kinda dated and dowsn't work on new versions, though Win10 introduced Linux-sh terminal esc sequences. `curses` is just a wrapper around latter. SO it all depends on versions too – Swift - Friday Pie Mar 29 '20 at 02:21
  • [https://stackoverflow.com/questions/32035762/if-always-returns-true](https://stackoverflow.com/questions/32035762/if-always-returns-true) should explain why `(ansr == 'y' || 'Y')` is a bug – drescherjm Mar 29 '20 at 02:23
  • 1
    `while (ansr == 'y' || 'Y')` is the same as `while ((ansr == 'y') || (true))` ... probably not what is intended. – Eljay Mar 29 '20 at 03:35

0 Answers0