0

hi I'm new to c and i made a random number app that follows:

#import "stdio.h"
#import "stdlib.h"
#import "time.h"
int main (int argc, char *argv[]){
    unsigned int iseed = (unsigned int)time(NULL);
    srand (iseed);
    int Ran;
    int N;
    int max;
    int min;
    int dec;
    int dec1;
    printf("Enter Max Value: \n");
    scanf ("%i",&max);
    printf("Enter Min Value: \n");
    scanf ("%i",&min);
    Ran=rand ();
    N=(Ran % (max+1-min))+min;
    //this is a decoration
    for(i=1;i<1000;i++){
        dec=rand ();
        dec1=(dec % (max+1-min))+min;
        printf("Random Number is %d\r",dec1);
        sleep(10)
     }
    printf ("Random Number is %d",N);
    getchar();

it asks you to enter a max and a min and then it shows "Random Number is %%" and the %% part changes every 10 milliseconds. The problem is that "Random Number is" is also rewritten every time because of \r. though it may be too fast for people to see it, i still know its there and i don't like it. how can i fix this problem?

jedwards
  • 29,432
  • 3
  • 65
  • 92
alexforC
  • 11
  • 3
  • 1) You should try formatting your code in a more readable manner. 2) I don't understand what exactly is your problem. – hugomg Jun 23 '12 at 03:29
  • [You used both tabs *and* spaces?](http://www.emacswiki.org/pics/static/TabsSpacesBoth.png) (it breaks the formatting on here) – Pubby Jun 23 '12 at 03:29
  • See [this SO question](http://stackoverflow.com/questions/1337529/how-to-update-a-printed-message-in-terminal-without-reprinting-linux) for similar problem. – Mateusz Kowalczyk Jun 23 '12 at 03:31
  • He basically doesn't want to go all the way back and overwrite the first part, he only wants to override the random number. I think. – Mateusz Kowalczyk Jun 23 '12 at 03:32
  • You should edit in or at least leave a comment with what exactly you hope the output to be – jedwards Jun 23 '12 at 04:08
  • If you rewrite the same characters on a screen, you can't see it even if you do it slowly. You're trying to fix a problem that isn't a problem. – Jim Balter Jun 23 '12 at 05:16
  • @jedwards The OP is hoping for exactly the same output, but without "Random number is " being written more than once. It's a bit like wishing for fewer angels dancing on the head of a pin. – Jim Balter Jun 23 '12 at 05:18
  • nitpicks: `#import` isn't standard C, you should use `#include` with the file names in `<>`. Casting the return of `time` shouldn't be necessary. – Jens Gustedt Jun 23 '12 at 06:56
  • i'm sorry i'm a sloppy person – alexforC Jun 27 '12 at 13:31

2 Answers2

3

On most terminals you can move the cursor to any place on the screen using ANSI control sequences.

What you'd need to do in order to only rewrite the number part is to move the cursor back just far enough so that it overwrites only the number part on the next loop.

For example from the above document the ANSI code to move the cursor backward COUNT characters is:

Cursor Backward: <ESC>[{COUNT}D

Moves the cursor backward by COUNT columns; the default count is 1.

So you'd do something like:

// Pseudocode..
printf("The random number is: ");
for (...) {
   printf("%5d", dec1); // Print a 5 field wide number
   printf("\e[5D");     // Move cursor back 5 steps
   ...
}

However you shouldn't really be worried about reprinting the line every time. The mechanism for doing it so that you only reprint the number will only unnecessarily complicate your code. You have to look at the rewards & complexity vs. effort. Modern computers print text far faster than you ever need to worry about.

Also if you only print the number then if the user happens to type in the terminal while your program is running the whole sequence will get messed up and it'll start to look strange on the screen. By reprinting the whole line spurious garbage characters from the user (or potentially from somewhere else) won't mess up the output since it'll just regenerate everything on the next loop.

So yes, you can do it with only the number, but it gets messy, and in my opinion in this case it's not worth it. I'd actually prefer to reprint the whole line.

However if you're building something more complex where you want a lot of control over the screen and cursor, then you could look at something like the curses programming library, which will allow "full screen" control of the cursor and your output.

Community
  • 1
  • 1
Casper
  • 33,403
  • 4
  • 84
  • 79
0

If the problem is printing out "Random number is" over and over, why not just do printf("%d\r",dec1);

This will print each new number after the last. Of course you won't be able to overwrite terminal output that you've already written unless you do something really fancy.

Antimony
  • 37,781
  • 10
  • 100
  • 107