Fortran updating number problem

  • Fortran
  • Thread starter gmcke1
  • Start date
  • Tags
    Fortran
In summary: Thanks for the help. In summary, to update a number without printing a bunch of totals, you need to output a fixed sized string starting with a hex "0D" (return), followed by a fixed sized numeric output.
  • #1
gmcke1
9
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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:
  • #7
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:
  • #8
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.
 

Related to Fortran updating number problem

What is the Fortran updating number problem?

The Fortran updating number problem is a common issue encountered by programmers using the Fortran programming language. It occurs when a variable is updated multiple times within a loop, leading to unexpected results.

How does the Fortran updating number problem affect program output?

The Fortran updating number problem can result in incorrect output or program crashes. This is because the updated variable may not have the expected value, causing the program to behave differently than intended.

What causes the Fortran updating number problem?

The Fortran updating number problem is caused by the way Fortran stores and updates variables in memory. If a variable is updated within a loop, it retains the updated value in the next iteration, leading to unexpected behavior.

How can the Fortran updating number problem be prevented?

The Fortran updating number problem can be prevented by using the "SAVE" keyword when declaring variables that need to retain their values in between function calls. Additionally, careful programming and avoiding updating variables within loops can also prevent this issue.

Are there any alternative solutions to the Fortran updating number problem?

One alternative solution to the Fortran updating number problem is to use arrays instead of individual variables. Arrays can be updated within loops without causing unexpected results, as long as the programmer is careful with their indexing.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
20
Views
5K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
2
Replies
50
Views
5K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
11
Views
10K
Replies
1
Views
659
  • Programming and Computer Science
Replies
13
Views
3K
Back
Top