我得到C++语法错误
I am getting C++ syntax error
这个脚本应该从键盘上读取字符,将它们存储到数组中,然后输出:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void storeArraysintoStruct(char[], int);
int main()
{
char test[] ="";
int a = 0;
storeArraysintoStruct(test, a);
system("pause");
return 0;
}
void storeArraysintoStruct(char test[], int a)
{
int n;
cout << "Enter number of entries: " << endl;
cin >> n;
int i = 0;
for (i=0, i<n, i++)
{
cout << "Enter your character: " << endl;
cin.getline(test, n);
}
while (i < n)
{
cout << test[i] << endl;
i++;
}
}
编辑:修复:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void storeArraysintoStruct(char[], int);
int main()
{
char test[40] = "";
int a = 0;
storeArraysintoStruct(test, a);
system("pause");
return 0;
}
void storeArraysintoStruct(char test[], int a)
{
int n;
cout << "Enter number of entries: " << endl;
cin >> n;
int i;
for (i=0; i < n; i++)
{
cout << "Enter your character: " << endl;
cin >> test[i];
if (test[n-1])
{
cout << endl;
}
}
i =0;
while (i < n)
{
cout << test[i] << endl;
i++;
if(test[n-1])
{
cout << endl;
}
}
}
但是,我得到了预期的错误:while之前的主表达式为")"answers";"。任何帮助都将不胜感激。
编辑:脚本无法按预期工作,因为它没有输出存储的字符。如有任何建议,我们将不胜感激。
注释中已经指出了语法错误。此外,正如前面提到的,在for
循环之后,您永远不会重置i
,这会阻止您的while
循环运行。
然而,你也必须记住,这个
char test[] = "";
分配只有1个字符长的数组CCD_ 4。不能将多个字符的数据放入该数组中。换句话说,您的storeArraysintoStruct
肯定会超出数组,并落入未定义的行为领域。
如果要预先分配一个更大的缓冲区以备将来在storeArraysintoStruct
中使用,则必须显式指定大小。例如
char test[1000] = "";
将使CCD_ 7成为1000个字符的数组。当然,无论阵列有多大,您都有责任遵守大小限制。
附言:如果你从未在storeArraysintoStruct
中使用过参数a
,那么它的意义何在?
相关文章:
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 我该怎么做才能摆脱使用命名空间时的语法错误?
- 如何摆脱错误c2061;语法错误 cout?
- 语法错误:D3DX10Core 中的标识符'TEXTMETRICA'
- 错误 C2146:语法错误:标识符"A1"之前缺少","
- main.cpp(11):错误 C2059:语法错误:"字符串"
- RedisGraph 语法错误在偏移量 8 靠近"创建"
- 如何修复此 FSM C++语法错误 C3867
- 函数错误 C2059:语法错误:'>'不起作用
- 为什么在main()之前断言会导致语法错误"error: expected ')' before numeric constant"?
- c++default-int中的语法错误
- "pip install openexr"上的语法错误:错误的编译器?
- 为什么我收到语法错误:在"*"之前缺少";"
- 错误 C2059:语法错误:从结构成员声明'constant'
- C++语法错误,编译器不会警告或 int v = func(&v) 出错;
- 我正在调试这个C++程序.编译器不再显示语法错误,但存在隐藏的逻辑错误