Fortran Fortran updating number problem

  • Thread starter Thread starter gmcke1
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread Summary
The discussion centers on how to update a number in the command line without cluttering the output with intermediate values. The user seeks a method to display a running total that updates in place rather than printing each total on a new line. Suggestions include using control characters to manage cursor position, specifically employing backspaces to overwrite the previous output. An example in C demonstrates this technique, showing how to print numbers on the same line by using backspace characters. The conversation also touches on the need for similar functionality in Fortran, with advice to consult the Fortran reference guide for specific options related to output formatting.
gmcke1
Messages
9
Reaction score
0
Hello,
I am wondering if there is a way to update a number without printing a bunch of stuff in the CMD window. For example this is what I don't want (below). I don't want a long list of totals..

Total = 1
Total = 2
Total = 3
Total = 4

This is what I do want.

Total = # <=== I want that number to keep updating without printing a bunch of totals.

Anyone know how to do this?
 
Technology news on Phys.org
gmcke1 said:
Hello,
I am wondering if there is a way to update a number without printing a bunch of stuff in the CMD window. For example this is what I don't want (below). I don't want a long list of totals..

Total = 1
Total = 2
Total = 3
Total = 4

This is what I do want.

Total = # <=== I want that number to keep updating without printing a bunch of totals.

Anyone know how to do this?
Presumably you have a loop of some kind where you're keeping a running total of something. What you don't want is to print the intermediate values of your variable inside your loop. You do want to print the value of this variable after the loop is finished.
 
I'm guessing that you just want to update the screen image. You could try outputting an initial fixed size string, then for each update, output some fixed number of hex "08" (backspaces), then a fixed sized numeric output. On a windows console program, you could just prefix the string with a hex "0D" (return) and not include a linefeed or newline (hex "0A") at the end of the string. Otherwise, you'll need to use some screen oriented library to set the cursor position on each output.
 
First off, sorry about reporting those comments. I thought the report button said "reply" lol. Ok Mark, I do want to see each numerical value change through each pass of the loop but I don't want it to print down the screen in the cmd window. For rcgldr not 100% sure what you just said but I'm going to try and figure it out and see what I come up with. Thanks for the responses. I appreciate it.
 
Not a problem. I don't think I understood what you were asking. What rcgldr is proposing is that you print the intermediate numbers at the same location on the screen, so that they don't scroll down the screen. If that's what you're looking for, you need to control the cursor in the command window so that it backs over the number it previously printed, and then prints a new number in the same place as the old one was.
 
Example C program that outputs numbers from 0 to 99999 on the same line:

Code:
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
    printf("\n");
    printf("i = ");
    printf("     ");      // 5 spaces
    for(i = 0; i < 100000; i++)
        printf("\x08\x08\x08\x08\x08%5d", i);
    printf("\n");
    return(0);
}
 
Last edited:
Yes Mark, that's exactly what I'm looking for. Unfortunately back in Computational Tools 1 my professor skipped over Control Characters, so I'm not really sure how to use the the 1, Blank, 0, and + . I'm assuming I have to use the + because that is for printing over the previous line. rcgldr I'm not really familiar with C :/
 
Last edited:
Try the + as a leading character and rewriting the entire line. Your Fortran compliler may have an "advance = no" option and TL# (tab left # characters) option. You'll need to look this up in your Fortran reference guide.
 

Similar threads

Replies
20
Views
6K
Replies
4
Views
2K
Replies
9
Views
2K
Replies
22
Views
5K
Replies
8
Views
2K
Replies
11
Views
11K
Replies
5
Views
2K
Replies
2
Views
2K
Back
Top