Range Based for loop help

I am getting an error saying (this range-based 'for' statement requires a suitable "begin" function and none was found).

Each of the loops are in functions that I have passed the array into. I ran a test with a ranged based for loop inside of main and it worked fine. Am I missing something when I pass the array into the function?

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <string>
using namespace std;

//func prototype***********************************************************
void maxMinFunc(int []);
void total(int []);
void displaySales(string [], int []);
//*************************************************************************

int main()
{
	const int size = 5;
	string names[] = {"Mild", "Medium", "Sweet", "Hot", "Zesty"};
	int sales[size];

	//asks user for sales for each type of salsa
	for (int index = 0; index < size; index++)
	{
		cout << "What are the sales for " << names[index] << "?" << endl;
		cin >> sales[index];
	}

	displaySales(names, sales); //displays name and sales for each type

	total(sales);  //calculates & displays total amount of sales

	maxMinFunc(sales); //calculates & displays min and max salsa sold

	return 0;
}

//func header***************************************************************
void maxMinFunc(int sales[])
{
	int max = sales[0];
	int min = sales[0];

	for (int val : sales) //sales is not a suitable begin function??
	{
		if (val < min)
		{
			min = val;
		}

		if (val > max)
		{
			max = val;
		}
	}

	cout << "max " << max << endl;
	cout << "min " << min << endl;
}

void total(int sales[])
{
	int total = 0;

	for (int val : sales) //sales is not a suitable begin function??
	{
		total += val;
	}

	cout << "The total amount of jars sold is " << total << "." << endl;
}

void displaySales(string names[], int sales[])
{
	int index = 0;

	for (int val : sales) //sales is not a suitable begin function??
	{
		cout << "The sales for " << names[index] << " is " << val << " jars."
			 << endl;
		index++;
	}
}
//************************************************************************** 
You can't use a pointer with a ranged based loop. Remember when you passed the array into the function you actually end up with a pointer, not an array.

Why are you using arrays instead of std::vector? If you were using vectors you could use the ranged based loop.
Thanks for clearing that up for me. I have had some trouble with grasping how pointers worked.

Also, the problem wanted me to use arrays. It didn't specify that i needed functions. So i guess i could just do it in main. Next time ill use vectors.

Thanks
Just for the sake of completeness, you can use arrays with a range-based for loop. It's a bit tricky to pass an array to a function, though:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

constexpr int size = 5;

// example passing an array (rather than a pointer)
void total(int (&sales)[size]) {
    int total = 0;
    for (int val : sales) {
        total += val;
    }
    std::cout << "The total amount of jars sold is " << total << ".\n";
}

int main() {
    // example usage
    int sales[size] = { 4, 6, 2, 6, 10 };
    total(sales);
    return 0;
}

Note that, obviously, you can't do it this way unless you know how big the array is going to be at compile time.

EDIT:
I'm not actually suggesting you do it like this. This is a handy trick, sometimes, but overall it's much better and more convenient to be use std::vector, as @jlb suggested.
Last edited on
Instead of the ranged based loops you can just use a "normal" for loop to access the array elements, be sure to pass the number of elements into the functions.

1
2
3
4
5
6
7
8
9
10
11
void total(int sales[], size_t num_elements)
{
	int total = 0;

	for (size_t i = 0; i < num_emements; ++i)
	{
		total += sales[i];
	}

	cout << "The total amount of jars sold is " << total << "." << endl;
}
Some things to think about:

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
44
45
46
#include <iostream>

// Here's a thing that takes a pointer and a count and
// returns something you can use in a ranged-for loop.
template <typename T>
struct pointer_range_type
{
  T*          p;
  std::size_t n;
  pointer_range_type( T* p, std::size_t n ): p(p), n(n) { }
  T* begin() const { return p; }
  T* end()   const { return p + n; }
};

// (Convenience constructor)
template <typename T>
pointer_range_type <T>
range( T* p, std::size_t n )
{
  return pointer_range_type <T> ( p, n );
}

// Here's the function that totals an array 
// given as a pointer and an element count
int total( int* xs, int n )
{
  int total = 0;
  for (int val : range( xs, n ))
    total += val;
  return total;
}

// And here's a convenience for the above function
// when you still have access to an actual array
template <std::size_t N> 
inline
int total( int (&sales)[ N ] )
{
  return total( sales, N );
}

int main()
{
  int sales[] = { 4, 6, 2, 6, 10 };
  std::cout << "The total amount of jars sold is " << total( sales ) << "\n";
}

Also, you might want to consider using a std::map to track sales:

1
2
3
4
5
6
7
8
std::map <std::string, int> sales;
sales["Mild"] = 4;
...

total = 0;
for (auto sale : sales)
  total += sale.second;
std::cout << total << " jars sold.\n";

Hope this helps.
Thanks!

Duoas. There are a lot of things i haven't covered in your program yet. So, Ill have to study how you did it. Haha


For now, I just used a normal for loop.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
using namespace std;

//func prototype***********************************************************
void maxMinFunc(string [], int [], int);
void total(int [], int);
void displaySales(string [], int [], int);
//*************************************************************************

int main()
{
	const int size = 5;
	string names[] = {"Mild", "Medium", "Sweet", "Hot", "Zesty"};
	int sales[size];

	//asks user for sales for each type of salsa
	for (int index = 0; index < size; index++)
	{
		cout << "What are the sales for " << names[index] << "?" << endl;
		cin >> sales[index];
	}

	cout << endl;

	displaySales(names, sales, size); //displays name and sales for each type

	cout << endl;

	total(sales, size);  //calculates & displays total amount of sales

	cout << endl;

	maxMinFunc(names, sales, size); //calculates & displays min and max salsa sold

	return 0;
}

//func header***************************************************************
void maxMinFunc(string names[], int sales[], int size)
{
	int max = sales[0];
	int min = sales[0];

	string minName = names[0];
	string maxName = names[0];

	for (int index = 0; index < size; index++) 
	{
		if (sales[index] < min)
		{
			min = sales[index];
			minName = names[index];
		}

		if (sales[index] > max)
		{
			max = sales[index];
			maxName = names[index];
		}
	}

	cout << maxName << " has sold the most at " << max << " sales." << endl;
	cout << minName << " has sold the most at " << min << " sales." << endl;
}

void total(int sales[], int size)
{
	int total = 0;

	for (int index = 0; index < size; index++) 
	{
		total += sales[index];
	}

	cout << "The total amount of jars sold is " << total << "." << endl;
}

void displaySales(string names[], int sales[], int size)
{
	for (int index = 0; index < size; index++)
	{
		cout << "The sales for " << names[index] << " is " << sales[index] 
			 << " jars." << endl;
	}
}
//************************************************************************** 
Topic archived. No new replies allowed.