Possible reasons for the loop that does not call a function more than once

Hello fellows,
When I call the Func1 in the loop in Func2, it does not call the fucntion more than once, ie; the loop does not iterate. In Func2, in Func1 , when I call here :

cout << "for" << kk << "=" << Func1(kk, D);
D is shown as grey , however, kk is dark black. It seems there is problem in kk. I know it is hard to tell without reviewing whole code but it is not possible to put all the lines here. Maybe you can share with me some ideas about why it does not iterate.
Thank you in advance

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43


#include<iostream>
#include<fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm> 

using namespace std;

double Func1(int&x,vector<vector<double>>&Demand)
{

	///some tasks ;
	return 0;
}

double Func2(vector<vector<double>>&D)
{
	double somme = 0;
	int K = D.size();


	for (int  kk = 0;kk<K;kk++)
	{
		
		cout << "for" << kk << "=" << Func1(kk, D);

	}

	return 0;
}

int main()
{
    cout<<"Hello World";
   
    vector<vector<double>> D={{ 3 ,6, 8 ,9},{ 4, 9 ,12, 19},{20 ,100, 56, 90}};
    Func2(D);

    return 0;
}
Slightly changed code:

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
27
#include <iostream>
#include <vector>

using Matrix = std::vector<std::vector<double>>;

double Func1(size_t x, Matrix&) {

	///some tasks ;
	return x;
}

double Func2(Matrix& D) {
	//double somme {};
	const auto K { D.size() };

	for (size_t kk {}; kk < K; ++kk) {
		std::cout << "for " << kk << " = " << Func1(kk, D) << '\n';
	}

	return 0;
}

int main() {
	Matrix D { { 3 ,6, 8 ,9},{ 4, 9 ,12, 19},{20 ,100, 56, 90} };

	Func2(D);
}


Displays:


for 0 = 0
for 1 = 1
for 2 = 2


as expected. What are you expecting?
Last edited on
Thank you so much @seeplus. I tried with size_t as well but it does not iterate over loop. The function is called for once and return what we expect but it does not call the functio for the second time. Thank you so much
It's possible the code inside Func1 is causing the program to silently crash the second time it's called.

If K is larger than 1 and it only calls it once, I'd add cout statements before and after each call so you can see exactly where it stops.

You'll also want to add a cout statement after the entire loop, to make sure it exits the loop. If a cout statement after the loop doesn't output anything, then it's crashing.
> ///some tasks ;
Read this -> http://www.sscce.org/
At least try to convey some sense of what you're trying to achieve, preferably something that actually demonstrates the problem.

A for loop + "it doesn't work" is not going to give you direct answers. Only hand waving and wild guesses.

> D is shown as grey , however, kk is dark black.
Completely meaningless without knowing your colour scheme of your IDE.

> cout << "for" << kk << "=" << Func1(kk, D);
If Func1 also calls cout, then you have undefined behaviour.
Meaning anything can happen.

Better to do
1
2
double result = Func1(kk, D);
cout << "for" << kk << "=" << result << endl;


Also, not outputting a '\n' or endl means your entire output appears on a single line. Depending on your apparent terminal width, all the other output will disappear to the right, perhaps giving the illusion of a single iteration.

You also pass kk by non-const reference, so if Func1 changes kk to be larger than K, then your loop will also exit after one iteration.
Thank you so much for all these replies. @zapshe, thank you so much. You were right. There was a silent crash and I found it. Thank you again. @salem c , I will take into account your advices , thank you
Topic archived. No new replies allowed.