Failed usage of functions under chrono header

I'm trying to get the time taken for a process to happen (from the start of the program until my calculation achieves a certain value). However, I keep getting stop was not declared in this scope.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <chrono>
#include <iostream>

using namespace std;
using namespace std::chrono;

int main()
{
     auto start = steady_clock::now();

     declaration of variables...

     for (int i = 0; i < 10000; i++)
     {
          some calculations and function calls...
          if (a value is achieved)
          {
               auto stop = steady_clock::now();
          }
     }
     duration <double> time = start - stop;//error occurred on this line
     cout << " Time taken to achieve value = " << time;

     return 0;
}

*I referred to this link: https://en.cppreference.com/w/cpp/chrono
Last edited on
Do you not know what a scope is?
stop is declared inside the body of the if.
It doesn't exist outside of that.
You could declare it before the if like this:

 
time_point<steady_clock> stop;

However, your fake code doesn't make much sense (what if the value isn't achieved???).
You could also get rid of stop and just replace stop on line 18 with steady_clock::now().
Hi dutch,

Nope, I always thought the scope was within the whole function as long as before I use it. Then this makes me wonder, why other variables in the if (variables I don't show in the above code) don't have the same problem? Is it that if I declared in line 11, the whole main function can use it, but if I only declare in the if, it can only be accessed in the if?

But I only want it to stop when achieving it, so I should add line 21 and 22 into the if, is that right? Or should I just exit the for loop when that value is achieved and take this
 Process exited after 68.87 seconds with return value 0 
as the time?

If the value isn't achieved, then I won't need the time taken, that's why I put it in the if, sorry if I didn't explain properly what I'm trying to do. What I'm doing is a type of optimisation problem. If the value IS achieved, I want the time taken, otherwise the time taken wouldn't matter. I should edit my code to make it clearer, hold on.
Last edited on
So as you can see, there will be 10000 iterations of calculations, and I want to get the time taken for the first time that value is achieved, if it isn't achieved, then nothing else will happen, it just proves the problem isn't optimised.
Last edited on
Is it that if I declared in line 11, the whole main function can use it, but if I only declare in the if, it can only be accessed in the if?

Yes, that's the basic idea. Variables only exist within the block where they are declared.

Maybe something like this will work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main()
{
    auto start = steady_clock::now();

    declaration of variables...

    bool achieved = false;
    for (int i = 0; i < 10000; i++)
    {
        some calculations and function calls...
        if (a value is achieved)
        {
            achieved = true;
            break; // exit the loop
        }
    }

    if (achieved)
    {
        auto stop = steady_clock::now();
        duration<double> time = stop - start;
        cout << "Time taken to achieve value = " << time.count() << '\n';
    }
    else
        cout << "Value not achieved.\n";
}

Last edited on
Hi dutch,

What if the value is achieved multiple times? Will the second time overwrite the first time?
The code above will stop the loop when the value is achieved for the first time. If you need to print the times for achieving the value multiple times then you need to put the printing of the time inside the if (like you suggested).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    auto start = steady_clock::now();

    declaration of variables...

    for (int i = 0; i < 10000; i++)
    {
        some calculations and function calls...
        if (a value is achieved)
        {
            auto stop = steady_clock::now();
            duration<double> time = stop - start;
            cout << "Time taken to achieve value = " << time.count() << '\n';
        }
    }
}

Last edited on
Hi dutch,

Sorry that was a silly question, I didn't notice the break in the if.

By the way, I got shocked when I got the result from your code because I got a negative value, so maybe if you may, please edit the start - stop to stop - start in case someone else needs to refer to this in the future.

Thanks for your help!
By the way, I got shocked when I got the result from your code because I got a negative value

I never noticed that!
I edited the code.
Thank you again for your help!
Topic archived. No new replies allowed.