输入2d字符数组时出错
Error in inputting a 2d character array
我正在输入一个2D字符数组,当用户按回车键时必须停止输入。但是我的代码没有显示任何输出。
输入:
5 // where this is the number of columns,
// number of rows are unknown so have taken maximum rows as: 40
数组:toioynnkpheleaigshareconhtomesnlewx
期望输出:
i = 7, j = 5
下面是我的代码:
int main(){
char a[100][100];
int n, i, j, p, q;
cin >> n;
if(n==0)
exit(0);
for(i = 0; i < 40; i++){
for(j = 0; j < n; j++){
cin >> a[i][j];
if(a[i][j]==13) // 13 = ASCII code for enter key
goto jump;
}
}
jump:
cout<<i<<"n"<<j<<"n";
}
但是它没有打印任何东西。
有什么问题吗?
这是因为cin
忽略空白和换行符('n',其ASCII码为13)。这意味着条件if(a[i][j] == 13)
的求值永远不会为真。
cin.get(a[i][j])
代替cin>>a[i][j]
这可以工作,因为cin.get()
方法不会忽略换行符('n')。
相关文章:
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- 将链表转换为指针数组时出错
- 为 NewObjectA() 函数创建 jvalues 的参数数组时出错 - JNI Invocation API
- 基于数组的列表 - 动态数组创建时出错
- 从斐波那契序列 c++ 中的数组中查找正确的元素时出错
- 分段 使用 MPI_Gather 收集字符数组时出错
- 读取字符串字符时出错 - 对于数组中的字符串值
- 声明数组>> int arr[] 时出错;在C++(虽然在 Java 中有效)?
- 在 c++ 中返回一维数组时出错
- 将 2D 数组传递给 C++ 中的函数时出错
- 在 c++ 上创建动态数组时出错
- 尝试返回指向 Visual Studio 中的动态数组的指针时出错C++
- 将 find_if() 与数组一起使用的代码段中出错
- 在比较特定输入中的程序的数组中出错
- 分段 填充数组时出错
- 数组作为函数参数查找数组长度时出错
- 读取结构数组时出错
- 将数组指针传递到函数时出错
- C++将指针数组传递给函数指针数组时出错
- 在数组中插入数据时出错 - 数组下标'int[int]'的类型无效