Incrementing value of variables in an array

Can anyone tell me how I'm supposed to increment in variables that are in arrays? I have tried looking on the internet but they aren't very clear. Also, how am I suppose to cout the values of those variables? Thanks.
Last edited on
Here's an array of 10 int: int intArr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

Let's say you want to increment the value stored on the 4th slot of the array, you do this:
intArr[3]++;

To cout an individual value you simply cout << intArr[3];

Or if you want to display all variables stored in the array you can use a for loop like so:
1
2
3
4
for (int count = 0; count < 10; count ++)
{
    cout << "\n" << intArr[count];
}


Last edited on
Following is a demonstration of some ways to achieve what you want.

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
#include <array>
#include <iostream>

template <typename T, size_t arrSize>
void Output(std::array<T, arrSize> const arr)
{
   for (auto const output : arr)
   {
	  std::cout << output << '\n';
   }
   std::cout << std::endl;
}

auto main() -> int
{
   const int NUMELS{ 5 };

   std::array<int, NUMELS> values{ 2, 4, 6, 8, 10 };

   std::cout << "Original values:\n";
   Output(values);

   // Increment each value in the array by one
   for (auto &&incrementInt : values) { incrementInt++; }

   std::cout << "Integer values incremented by 1:\n";
   Output(values);

   std::array<char, NUMELS> chars{ 'a', 'b', 'c', 'd', 'e' };

   std::cout << "Original char array:\n";
   Output(chars);

   // Increment each character by one
   for (auto &&incrementChar : chars) { incrementChar++; }

   std::cout << "Character values incremented by 1:\n";
   Output(chars);
 
   return 0;
}


Or like this:

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

auto main() -> int
{
   const int NUMELS{ 5 };

   int intArray[NUMELS]{ 1, 2, 3, 4, 5 };

   std::cout << "Original values:\n";
   for (auto const output : intArray)
   {
	  std::cout << output << '\n';
   }
   std::cout << std::endl;

   std::cout << "Original values incremented by 1:\n";
   for (auto &output : intArray)
   {
	  std::cout << ++output << '\n';
   }

   return 0;
}


Or like this:

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

auto main() -> int
{
   const int NUMELS{ 5 };

   int intArray[NUMELS]{ 1, 2, 3, 4, 5 };

   std::cout << "Original values:\n";
   for (auto i = 0; i < NUMELS; i++)
   {
	  std::cout << intArray[i] << '\n';
   }
   std::cout << std::endl;

   std::cout << "Original values incremented by 1:\n";
   for (auto i = 0; i < NUMELS; i++)
   {
	  std::cout << (intArray[i] += 1) << '\n';
   }

   return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <array>
#include <iostream>

using namespace std;

int main()
{
  // OLD C STYLE
  // int numbers[] = {1, 2, 3, 4};

  // NEW STYLE C++11

  array<int, 4> numbers = { 1, 2, 3, 4 }; 
  cout << "Original numbers: ";
  for (int i: numbers)
    cout << i << ' ';

  numbers[0] = numbers[0] + 1; // or numbers[0]++

  cout << "\n\nNumbers after the change: ";
  for (int i : numbers)
    cout << i << ' ';
}

I tried douing what you all sayd, but its saying syntax error: unexpected token 'constant', expected 'id-expression' and that I need a ;. But where? I actually don't know what I'm doing anymore. The program is basically suppose to be a poll on which drink is a persons favorite, its then suppose to cout the results and ask again. It sounds easy but the teacher wants us to use his code, so I dont even know anymore.
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
// Program Favorit surveys users to determine the favorite soft drink.
#include <iostream> 
using namespace std;
enum Drinks { COKE, PEPSI, SPRITE, DR_PEPPER }; 
void Prompt();
int main() {
	int favorites[4]; //holds sum of users who favor each drink 
	int number; 
	Drinks index;
	for (index = COKE; index <= DR_PEPPER; index = Drinks(index + 1))
	{
		// FILL IN Code to initialize array favorites to all zeros
		int favorites[4] = { 1 }; //
		//
		Prompt();
		cin >> number;
		while (number != 4)
		{
			// FILL IN Code to increment the proper drink fvorites based on user selections 
			// e.g. if user enter 0 increment favorites[COKE] etc.
			if (number == 0)
			{
				enum Drinks[1]++;
			}
			cout << favorites[COKE];
			Prompt(); 
			cin >> number;
		}
	}
	// FILL IN THE Code to write out the totals in the format: 
	// Drink Number of favorites
	cout << index;
	system("PAUSE");
	return 0;
}
/*******************************************************/
void Prompt() {
	cout << "Enter a 0 if your favorite is a Coke." << endl; 
	cout << "Enter a 1 if your favorite is a Pepsi." << endl;
	cout << "Enter a 2 if your favorite is a Sprite." << endl;
	cout << "Enter a 3 if your favorite is a DrPepper." << endl; 
	cout << "Enter a 4 if you wish to quit the survey." << endl;
}
Your problem lies in the fact that you are trying to increment the enum itself, which - of course - does not and will not work.

 
line 23: enum Drinks[1]++;


What you want to do instead is this:

 
favorites[number]++;


Here is a complete (and hopefully working example) you can take away some inspiration from.

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
#include <array>
#include <iostream>
#include <string>

enum class Drinks : int { COKE = 1, PEPSI = 2, SPRITE = 3, DR_PEPPER = 4 };

struct Selection 
{ 
   Drinks drink; 
   int	  uSelection{ };
};

auto main() -> int
{
   const int NUMELS = 4;

   // We instantiate a struct object here and assign it the value of 1 in the second line
   Selection select;
   select.drink = Drinks::COKE;

   // Two arrays - one whose values is initalized to 0 and the other holds the drink names.
   std::array<int, NUMELS> favorites{ 0 };
   std::array<std::string, NUMELS> drinkNames{ "1 - COKE ", "2 - PEPSI ", "3 - SPRITE", "4 - DR_PEPPER" };

   std::cout << "Please select your favorite drink [1-4] or [0] to quit:\n\n";
   
   do
   {
	  for (auto const &drinkList : drinkNames)
	  {
		 std::cout << drinkList << "\n";
	  }

	  std::cout << "\nYOUR CHOICE: ";
	  std::cin >> select.uSelection;
	  std::cout << std::endl;

	  if (select.uSelection != 0)
	  {
		 // Here we increment the value of each of the array elements by one
		 // depending on the user choice made. The -1 is only there to allow
		 // the user to input 1-4 instead of having to input array indices.
		 favorites[select.uSelection - 1]++;
	  }
   } while (select.uSelection != 0);

   std::cout << "These are the results of our survey:\n";
   for (auto i = 0; i < NUMELS; i++)
   {
	  std::cout << drinkNames[i] << ": " << favorites[i] << "\n";
   }

   return 0;
}
Last edited on
Topic archived. No new replies allowed.