Is there a way to allow the user to populate an Array with "cin"?

I have an excercise to do for college and one of the questions is as follows

a. Write a prototype for a function LargestInputValue that will allow the user to
enter a list of positive integers, followed by a terminator value –1. The
function should return the largest value entered.
b. Write a definition for the function LargestInputValue
c. Write a test application that will use the function LargestInputValue


The problem I'm having is how do I allow the user to enter there own numbers using cin, I know it involves a for loop.

lets say I declared an array in main int Array[20];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include iostream;
using namespace std;

int main()
{
for (int i = 0; i < 20; i++)
	{

		cin >> First[i];


	

	}


	cout << First;

}


But when I try output my array, It gives me the a hexdecimal memory address. How would I go about outputing All the elements in the array on the screen that the user just populated? Also can you explain Why the memory address is showing up, I know its completley wrong and the cout << First makes no sense but Im just giving you an idea what I'am trying to achieve.
Last edited on
You would need to loop through the array like you did with cin, except with cout.
1
2
3
4
5
6
7
// named constant; avoid magic numbers
const int array_size = 20;

for(int i = 0; i < array_size; i++)
{
    cout << First[i] << ' ';
}
But when I try to output my array, I get a hexadecimal memory address.

I'm assuming you have an array int a[10];, and you wrote std::cout << a << "\n"; to try and print it.

The simple answer is that a unqualified identifier of array type (i.e., a) "decays" to a pointer to its' first element in many (but not all) cases. This is one such case.
A pointer is a memory address; std::ostream::operator<<(), AKA (cout <<) happens to stream pointers in hexadecimal format by default.

The exact rules are quite complex and will assume a lot of knowledge about the language. If you're really interested, you can find them here:
http://en.cppreference.com/w/cpp/language/array
That site usually nicely paraphrases the horse's mouth, found starting here
http://eel.is/c++draft/conv#array-1

Here's an example of how to print the contents of an array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# include <iostream>

int main() {
    constexpr int sz = 10;
    int vals[sz], i = 0;
    
    for (i = 0; i < sz; ++i) {
        std::cin >> vals[i];
        if (! std::cin) { // error!
            std::cerr << "error populating array\n";
            return 1; // bail out.
        }
    }
    
    for (int i = 0; i < sz - 1; ++i) 
        std::cout << vals[i] << ", ";
    std::cout << vals[sz - 1] << "\n";
}


http://coliru.stacked-crooked.com/a/f60420ed6d2b2836
closed account (E0p9LyTq)
allow the user to enter a list of positive integers, followed by a terminator value –1.

Don't try using a static-sized array, use a dynamic array such as a vector.

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

int main()
{
   int input_value = 0;
   std::vector<int> list;

   std::cout << "input a list of values (-1 to stop):\n";

   while (true)
   {
      std::cin >> input_value;

      if (input_value == -1)
      {
         break;
      }

      // add a new value to the vector
      list.push_back(input_value);
   }

   // define a variable to check for largest value entered
   int largest = 0;
   
   std::cout << "\nThe contents of the vector:\n";
   
   // use a range based for loop to print out the contents of the vector
   for (auto x: list)
   {
      std::cout << x << '\n';
      
      if (largest < x)
      {
         largest = x;
      }
   }
   
   std::cout << "\nThe largest value entered was: " << largest << '\n';
}

input a list of values (-1 to stop):
5
175
25
50
19
-1

The contents of the vector:
5
175
25
50
19

The largest value entered was: 175

Is this the most efficient way to fulfill that objective? Doubtful, this was a quick and dirty knock-up.

Most instruction, classroom or online tutorial, avoid using the language's better features that end up making it harder to write good programs.
Last edited on
Topic archived. No new replies allowed.