0
#include <iostream>
using namespace std;
int n, a, k; 
main(){
cin>>n;
for(k=1; k<=n; k++){
cin>>a;}} 

how can I store inputs to use them in future operations, I'm beginner programmer, I'm looking for easiest way for that.

  • 2
    Welcome to Stack Overflow! What you want is called an array, or a `std::vector`. That said, it sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Oct 18 '19 at 19:19
  • Please format your code with tabs, it is good to know the good practice ince the beginning. – Mauricio Ruiz Oct 18 '19 at 19:24
  • You are looking for something like `array` or `vector`. – ph3rin Oct 18 '19 at 19:24
  • Related question: https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c – ph3rin Oct 18 '19 at 19:25
  • Possible duplicate of [How do I use arrays in C++?](https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – Ross Jacobs Oct 18 '19 at 21:26

1 Answers1

3

If you want to read n integers you can use a vector:

#include <iostream>
#include <vector>

int main()
{
   int n; 
   std::vector<int> vec;    //Better try to use namespaces
                            //Some spaces add clarity
   std::cin >> n;
   vec.resize(n);

   for(int k=0; k<n; k++)   //Define local varibales relative to loop for iterator
   {
      std::cin >> vec[k];
   }

   return 0;      //Always return a code at the finish of the program
} 
Mauricio Ruiz
  • 322
  • 2
  • 10