Help figuring out errors in code

Write your question here.
I have been stuck trying to figure out the following errors for a couple hours I'm very new to this and can't really figure out where I am going wrong any help would be appreciated thanks.
The errors I am getting from building my code are the following :
*c2065 x: undeclared identifier lines 29/30/33
*c2062 type int unexpected line 31/34
*c2181 illegal else without matching if line 32

Just looking for a push in the right direction because I'm getting very confused, thank you!

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>
using namespace std;

int main()
//start of bargraph generated section
{
	int bargraphValues[16]; //max bargraph values
	int bargraphValue = 0; //starts @ 0

	for (int x = 0; x < 16; x++)
	{
		cout << "Enter values for bargraph # " << x + 1 << endl;
		cin >> bargraphValue; // user inputs values for bargraphValue
		bargraphValues[x] = bargraphValue; //stores bargraph values 
	}
	for (int y = 0; y < 16; y++)
	{
		cout << "#" << y + 1;
		for (int z = 0; z < bargraphValues[y]; z++)
		{
			cout << "*"; //displays asterixs 
		}
		cout << endl; //working untill here then coded bottom half
	}
	cout << "The total amount of sand that can be held is..."; 
	cout << endl; 
	for (int z = bargraphValue + 1; z >= 0; z++) //may be prob here - supposed to be place in loop and itterate - starts loop
	{
		if (bargraphValues[x - 1] < bargraphValues[x + 1]) //if previous less than next
			int sand = bargraphValues[x - 1] - bargraphValues[x]; //how much sand can be held 
		cout << "sand value is:       " << int sand << endl; // displays value for amount of sand for this statement being true
		else {
			int sand = bargraphValues[x + 1] - bargraphValues[x]; // if next less than previous this is how much sand can be held
			cout << "sand value is:       " << int sand << endl; //displays value for amount of sand for this statement being true
		}


	}


	system("PAUSE");
	return 0;
} 
Could you explain in words what you are trying to do in lines 27-38. Are you simply adding up the lengths of the bars or trying to do something more subtle with differences between bar lengths? It is far from obvious from the comments. We could offer help, but it runs the risk of misadvising you if we are unsure of what you are intending to do.
I am trying to calculate how much "sand" can be held between the bars of the bargraph so basically the space between the high low high bars in total
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
#include <iostream>
#include <cmath>//for abs;

using namespace std;
const size_t SIZE = 6;//use global values, easier to change

template <typename T>
T sandCalc(const T bargraphValues[], size_t n)
{
    T sum = 0;
    for(size_t i = 0; i < n-1; i++)
    {
       sum += abs(bargraphValues[i+1] - bargraphValues[i]);//the absolute difference b/w 2 adjacent bars == 'sand' held;
    }
    return sum;
}

int main()
{
	int bargraphValues[SIZE]; //max bargraph values
    	for (int x = 0; x < SIZE; x++)
	{
		//int bargraphValue = 0; //you can cin directly into the array cell, this is not required;
		cout << "Enter values for bargraph #" << x + 1 << "\n";
		cin>>bargraphValues[x];
    	}
        cout << "The total amount of sand that can be held is: "<<sandCalc(bargraphValues, SIZE)<<"\n";
}

Last edited on
Oteej,
What do you mean by "the space between the high low high bars".
If gunnerfunner's interpretation is correct, then that is OK. Otherwise, please give a numerical example to illustrate what you mean.
Topic archived. No new replies allowed.