-1

A[0] not accessible.

Why? & How to solve this

If input is from index 1, A[0] and A[1] not acesible Why?

#include<bits/stdc++.h>

using namespace std;

int main()
{    
   int n;   
   cin>>n;    
   string A[n];    
   for(int i=0;i<n;i++)    
   getline(cin,A[i]);     
   for(int i=0;i<n;i++)     
   cout<<A[i]<<endl;     

   cout<<"first is "<<A[0]; 
   return 0;     
}

1 Answers1

0

Here there is an explanation why you should not use both getline and >>

This program works fine:

int n;
cout << "n: ";
cin >> n;
string A[n];
for (int i = 0; i < n; i++) {
    cout << i << ": ";
    cin >> A[i];
}
for (int i = 0; i < n; i++)
    cout << A[i] << endl;

cout << "first is " << A[0];
return 0;
Nikita Skrebets
  • 1,518
  • 2
  • 13
  • 19