【问题标题】:In Array of strings of variable size, If input is taken from 0 index A[0] is not accessible; if input is from index 1, A[0] and A[1] not acesible Why?在可变大小的字符串数组中,如果输入来自 0 索引 A[0] 不可访问;如果输入来自索引 1,A[0] 和 A[1] 不可行为什么?
【发布时间】:2022-01-19 07:45:26
【问题描述】:

A[0] 不可访问。

为什么?以及如何解决这个问题

如果输入来自索引 1,A[0] 和 A[1] 不可用,为什么?

#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;     
}

【问题讨论】:

    标签: arrays string


    【解决方案1】:

    Here有一个解释为什么你不应该同时使用getline&gt;&gt;

    这个程序运行良好:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      相关资源
      最近更新 更多