变量停止程序的C 增量
C++ increment of a variable stops program
我正在尝试编写词汇分析仪程序。但是每次我在第152行上汇总变量" varcount"时,我的程序都会崩溃。
更具体地说,只有当我将其增加超过1时,才崩溃。我尝试将其重命名,但它仍然不起作用。我不知道我做错了什么。
这是我的代码
#include<bits/stdc++.h>
using namespace std;
string presentKeyWords[100], presentVariables[100], operators[100] ;
int pkcount=0, varCount=0, opcount=0;
int main()
{
ifstream inputFile("input.txt");
ofstream outputFile("output.txt");
string line;
bool s_cmt = 0;
if(inputFile.is_open())
{
while( getline(inputFile, line) )
{
char arrline[line.length()+1] ;
strcpy( arrline, line.c_str() );
/* Removing comments */
// traverse each character of a line
for(int i=0; i<line.length(); i++)
{
if(arrline[i]=='/' && arrline[i+1]=='/'){
s_cmt = 1;
break; // skip if single-line cmt
}
else if(arrline[i]=='/' && arrline[i+1]=='*'){
while( getline(inputFile, line) ){
char arrline[line.length()+1] ;
strcpy( arrline, line.c_str() );
int x = 0;
for(int i=line.length(); i>0; i--){
if(arrline[i]=='/' && arrline[i-1]=='*'){
x = 1;
break;
}
}
if(x==1){
break;
}
}
break;
}
else{
s_cmt = 0;
outputFile << arrline[i];
}
}
/* Detecting variables and keywords */
if(s_cmt==0)
{
string words[100], var;
// break the line into words
int c=0;
istringstream streamOfString(line);
while( getline(streamOfString, var, ' ') ){
words[c]=var;
c++;
}
// loop through the words
for(int i=0; i<c; i++)
{
// Checking for special characters
int specialCharFound = 0;
string listOfOperators[] = { "+", "Plus",
"-", "Minus",
"*", "Multiplication",
"/", "Division",
"=", "Equal",
"~", "Difference",
"!", "Exclamation",
".", "dot",
"@", "at",
"#", "Hash",
"$", "Dollar sign",
"%", "Modulus",
"^", "Carrot",
"&", "AND",
"?", "Question Mark",
"<", "Greater than",
">", "Less than",
",", "Comma",
":", "Colon",
";", "Semicolon"
};
int sn = sizeof(listOfOperators)/sizeof(listOfOperators[0]);
for(int iter=0; iter<sn; iter+=2)
{
if( listOfOperators[iter]==words[i] )
{
operators[opcount] = listOfOperators[iter] ;
operators[opcount+1] = listOfOperators[iter+1] ;
opcount+=2;
specialCharFound = 1;
}
}
if(specialCharFound==1){
continue;
}
/* check for keyWords */
int keyWordFound = 0;
string keyWords[] = {"bool", "int", "float", "char", "if", "for", "while", "string", "break", "continue"};
for(int j=0; j<10; j++){
if(words[i]==keyWords[j]){
presentKeyWords[pkcount] = words[i] ;
pkcount++;
keyWordFound = 1;
break;
}
}
if(keyWordFound==1){
continue;
}
/* check for variables */
if( isdigit(words[i][0]) ){
//cout << words[i] << endl ;
}
else{
string tempVar = words[i] ;
string dType ;
for(int j=0; j<varCount; j++){
if( presentVariables[j]==tempVar ){
dType = presentVariables[j+1];
}
}
if(dType.empty() && pkcount!=0){
dType = presentKeyWords[pkcount-1];
}
presentVariables[varCount] = tempVar ;
presentVariables[varCount+1] = dType ;
varCount+=2;
}
}
}
outputFile << endl;
}
}
outputFile << "nn" << "Keywords n---------- n";
for(int i=0; i<pkcount; i++){
outputFile << presentKeyWords[i] << endl;
}
outputFile << "nnn" ;
outputFile << "nn" << "Variables n---------- n";
for(int i=0; i<varCount; i+=2){
outputFile << presentVariables[i] << " = " << presentVariables[i+1] << endl;
}
outputFile << "nn" << "Operators n---------- n";
for(int i=0; i<opcount; i+=2){
outputFile << operators[i] << " " << operators[i+1] << endl;
}
return 0;
}
有人请帮助...
(行#2(presentVariables[100]
最多可容纳100个元素;虽然varCount
可以走得更远。我尝试presentVariables[1000]
,并且该程序在我的计算机中运行良好(带有小输入(。
您应该使用vector<string>
吗?
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 增量运算符与后缀混淆
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 为什么带有逻辑和运算符以及增量运算符的 c++ 程序给出的输出与预期不同?
- 使用CLANG内联程序集创建C++预增量操作
- 变量停止程序的C 增量
- "矢量迭代器不可增量"与OpenGL,LeapMotion应用程序
- C ++,我的程序在调用list.erase后"go",没有增量迭代器?
- 整数增量使应用程序崩溃
- 矢量迭代程序的增量/减量
- 在应用程序中使用Rsync和增量传输算法
- C ++简单增量程序无法正确打印?什么?
- 我正在创建一个按增量计数的程序。我真的很接近,但我无法让程序按决定的增量上升?
- C程序的输出,包括前/后增量和指针