0

I have a double value which is getting updated in a loop, the increment is about 0.0001, when I am trying to print it with cout, nothing is being printed. Whereas when I use printf("%lf", value), it gets printed easily. Why am I getting such behaviour ?

For eg.

I access the variable using a class object called var, as I have stored all variables in a header file, under the class var. Like

variables.h

class varibles {
  public:
    double t;
    // other variables too
};

This code below produces the right output for every updated value of var.t For eg.

0.00001 secs
0.00002 secs
0.00003 secs

Heap.cpp

#include <bits/stdc++.h>
#include "variables.h"
#include "Heap.h"
#include "Move.h"

void Heap::Heaps(variable &var) { // var is passed from main code
    for (condition) {
        // code

        var.t = var.t + var.dt; // t gets updated only here

        printf("%lf secs \n", var.t);

        // code
    }
}

Whereas on using the code below it does not print anything, neither the value nor secs.

Heap.cpp

#include <bits/stdc++.h>
#include "variables.h"
#include "Heap.h"
#include "Move.h"

void Heap::Heaps(variable &var) {
    //code
    for (condition) {
        //code

        var.t = var.t + var.dt; // t gets updated only here

        cout << var.t << "secs" << endl; // this line is causing problems
        //code
    }
}

Here is the contents of Heap.h which contains the function declaration:

#ifndef HEAP_H
#define HEAP_H

#include <stdio.h>
#include "variables.h"

class Heap {
  public:
    void Heaps(variables &var);
};

#endif

and main is defined in main.cpp:

#include <bits/stdc++.h>
#include "variables.h"
#include "Heap.h"

int main() {

    variables var;
    Heap heap;
    var.t = 0;

    // other code

    while (var.t < var.tfinal)
        heap.Heaps(var);
    }
    //code 
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 7
    Please edit your question to provide a [mcve] that demonstrates the problem. – G.M. Feb 14 '22 at 11:44
  • The `l` modifier is required in scanf with double, but not in printf. You can also use`%f`, `%g` or `%e` depending on how you want the number to be formatted. –  Feb 14 '22 at 11:48
  • 2
    Writing code around those two lines you show, which will result in an output from printf but not via cout is not very hard. Please show the code you use to achieve that, in the shape of a [mre]. Note that it requires much more than twe two output lines. The "minimal" might be misleading you. Please also cover the "reproducible". For details what that means please follow the link and read. – Yunnosch Feb 14 '22 at 11:54
  • 1
    Please don’t post pictures of code. Post the code itself. – Konrad Rudolph Feb 14 '22 at 12:04
  • Does this answer your question? [Correct format specifier for double in printf](https://stackoverflow.com/questions/4264127/correct-format-specifier-for-double-in-printf) –  Feb 14 '22 at 12:04
  • I am sorry for causing trouble, I will frame the question in a better way – theufohunter Feb 14 '22 at 12:05
  • 1
    @OrkhanAliyev: You are right that the `l` in `%lf` is not necessary with `printf`, only with `scanf`. However, there is nothing wrong with writing it. In fact, one could consider it a good habit. – Andreas Wenzel Feb 14 '22 at 12:15
  • You may want to read this: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/12149471) – Andreas Wenzel Feb 14 '22 at 12:15
  • In C++, a [mre] should include a function `main` and all `#include` directives and should preferably only consist of a single file. – Andreas Wenzel Feb 14 '22 at 12:26
  • I see nothing wrong with the code that you are showing us. Therefore, the problem is probably in the code that you are not showing us. That is another reason why we require a [mre]. – Andreas Wenzel Feb 14 '22 at 12:34
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). When you run the code in a debugger, is the `cout` line executed at all? Or does the control flow maybe never reach it? – Andreas Wenzel Feb 14 '22 at 12:35
  • 2
    You may want to read this: [Why should I not #include ?](https://stackoverflow.com/q/31816095/12149471) – Andreas Wenzel Feb 14 '22 at 12:39
  • Thanks Andreas, I will try to import only the required header files and try it. Thank you again. – theufohunter Feb 14 '22 at 12:43
  • Note that after your most recent edit, you still have not provided a [mre]. Your posted code is not complete, so that the problem is not reproducible. Please read the link on how to create such an example. – Andreas Wenzel Feb 14 '22 at 12:55
  • 1
    Note that in your most recent edit (revision 8), you overwrote all of the [formatting improvements that I had made in revision 7](https://stackoverflow.com/posts/71111302/revisions). I'm not sure if this was intentional. You may want to add them back. – Andreas Wenzel Feb 14 '22 at 13:00

0 Answers0