【发布时间】:2015-11-12 14:26:10
【问题描述】:
我正在制作程序来检查用户输入文本中的孔...孔是像'A','B','P'等字符...但是它在循环中出现错误
我已经在下面发布了完整的代码..帮助查找错误
#include <iostream>
#include <cstring>
#ifdef __cplusplus__
#include <cstdlib>
#else
#include <stdlib.h>
#endif
using namespace std;
int main()
{
label:
cout << "Enter Number of test Case : ";
int tc;
cin >> tc;
int * hls = new int [tc];
hls = {0};
if(tc > 40)
{
if (system("CLS")) system("clear");
goto label;
}
char *str = new char [tc];
for(int a = 0; a < tc; ++a)
{
cout << "Enter your " << a+1 << " text : ";
cin >> str[a];
}
for(a = 0; a < tc; ++a) // getting error in this line.
{
for(int b = 0; b < strlen(str[a]); ++b)
{
switch(str[b])
{
case 'A' :
case 'D' :
case 'O' :
case 'Q' :
case 'P' :
++hls[b];
break;
case 'B' : hls[b] += 2;
break;
default :
break;
}
}
}
if (system("CLS")) system("clear");
for(a = 0; a < tc; ++a)
cout << hls[a] << endl;
return 0;
}
【问题讨论】:
-
欢迎来到 Stack Overflow!请edit您的问题与minimal reproducible example 或SSCCE (Short, Self Contained, Correct Example)
-
代码,正如你所展示的,不应该编译,因为它在语义上是错误的。
-
str是字符串还是字符串的数组/向量/...? -
现在我已经发布了完整的代码..请检测错误显示的原因。
标签: c++ error-correction