变量停止程序的C 增量

C++ increment of a variable stops program

本文关键字:增量 程序 变量      更新时间:2023-10-16

我正在尝试编写词汇分析仪程序。但是每次我在第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>吗?