0
#include<stdio.h>

int main() {
    //system("cls");
    char randomStr[] = "0123456789";
    printf("\n\n\n\n\t\t\t\t\t");
    while(!kbhit()) {
        int i;
        for(i = 0;i < 10;i++) {
            printf("%c", randomStr[i]++);
            while(randomStr[i] == '\n' || randomStr[i] == '\r' || randomStr[i] == '\t' ||
              randomStr[i] == '\b' || randomStr[i] == '\0' || randomStr[i] == '\a' ||
              randomStr[i] == '\v' || randomStr[i] == '\f') {

                randomStr[i]++;
            }
        }
        printf("\b\b\b\b\b\b\b\b\b\b");
    }
    return 0;
}

I want the cursor to move to a bit right and start printing characters and then overriding themselves which produces a sort of scrolling effect (like in lottery machines). This works fine without using system("cls") but if I use system("cls") the cursor is pulled back to the beginning of the line. Images below:

Without system("cls") it works fine (chars in image are random, at an instance while program is running)

without system("cls")

After adding system("cls") this happens (the highlighted area is where my intended effect is taking place)

with system("cls")

  • don't put images here unless really necessary. Just right click on the console > mark and copy the content – phuclv Jul 07 '16 at 03:10
  • What makes you think the image is not necessary? I thought I have to show you what I need and what I am getting on console. Cant think of describing them just in text – Jagannath Suhit Jul 08 '16 at 03:35
  • don't write `\b`s to delete characters. To [clear a line use `\r` instead](http://stackoverflow.com/a/1508503/995714) – phuclv Jul 08 '16 at 04:07
  • Using \r and modifying code a bit worked out very well! Thanks @Luru. But, I do not understand why there is a difference in output before and after using system("cls"). I mean, there is nothing on the screen when I started the program. So system("cls") would again give me a blank console which is what I have initially, but output is varying... I have edited and wrote the full working code above.. Please try to execute and see the difference with and without the system command. Thank! – Jagannath Suhit Jul 08 '16 at 05:06

2 Answers2

1

system("cls") is a system call that clears the screen on the related console. It behaves the same way as if it was used as cls on your windows command line (try it). So, that would clear all the current chars from the screen leaving you without the "scrolling effect" you're looking for.

ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
  • Hi @ChiefToPencils, thanks for the answer, But the problem is regarding the positioning and not with the scrolling effect. Without system("cls") I am able to get the position right as shown in the 1st image. But when I use it, though it starts at the intended position it goes back to the beginning of the line and starts producing the effect. – Jagannath Suhit Jul 08 '16 at 03:39
  • Yes, but the command will impact the position; in the end the command will change the way the code behaves. The code is incomplete so when I run it it doesn't give me anything useful to work with. But again, the big picture is cls will clear the screen and and put the cursor at the top left corner of the screen @JagannathSuhit. – ChiefTwoPencils Jul 08 '16 at 04:28
  • I do know that cls will clear the screen. In my program system("cls") is the first line. So no change in the position right? I have edited the code and wrote the full working code now. Please test it with and without system(cls) and find the difference. Thanks for your effort! – Jagannath Suhit Jul 08 '16 at 05:11
0

system() command is the command to OS and "cls" is the parameter you pass. cls is the command for clear screen. In some OS cls parameter is not the command for clear screen, I am not sure but I think in Unix it does not work.

As far as why it behaves differently when you call it and not call it- I will need to more information because it depends on a lot of factor and provided code is not good enough to clear it.I would like to see what you doing before you call system("cls");

Denis
  • 1,219
  • 1
  • 10
  • 15