[英]unable to display string array
When i try to run this code, it compiles without any error, but i wanted that it should display the string and int array that it takes as input. 当我尝试运行此代码时,它会编译而没有任何错误,但是我希望它显示应该作为输入的字符串和int数组。 Instead, after giving one input, I am presented with a list of zeroes and program terminates.
取而代之的是,在给出一个输入之后,我会看到一个零列表,程序终止。 however,when i removed the line containing k[j] from both for loops, it worked.
但是,当我从两个for循环中删除包含k [j]的行时,它起作用了。 What am I doing wrong?
我究竟做错了什么? Pardon if asked something stupid as I'm a novice, please help...
请原谅如果我是新手,如果被问到一些愚蠢的事情,请帮忙...
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int t,count = 0;
string state[t];
int k[t];
cin>>t;
for (int j=0; j<t; j++)
{
getline(cin, state[j]);
cin>>k[j];
}
for (int j=0; j<t; j++)
{ cout<< state [j]<<'\t'<<k[j];
cout<<endl;
}
return 0
}
You declare both arrays k[]
and state[]
with t
wich is undefined at this moment. 声明两个阵列
k[]
和state[]
用t
至极此时未定义。
t
should be initiated with a valid value beforehand! t
应该事先以有效值启动!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.