vendredi 16 septembre 2016

C++ One If Statement Throws Another Into an Infinite Loop?

I'm working on writing a scanner (for a class), and I believe I have most of it correct. However, I have this weird problem where I keep getting an infinite loop from two if statements. It doesn't always occur, I'm noticing it when I pass only a single alpha character to the isalpha() if statement. Any ideas on why this is happening? (Please note, it's probably something easy and I just can't figure it out. I haven't used C++ in a while)

if(isdigit(c)){
    std::string digit;
    int decicount = 0;
    do{ digit.push_back(c);
        if(c=='.'){decicount++;}
        token_stream.get(c);
    }while((isdigit(c))||(!isspace(c)));
    if(decicount>1){
        t.token = TOKEN::TOKEN_ERROR;
        t.text = "Invalid number of '.'";
        return t;
    }
    t.token = TOKEN::NUMBER;
    t.text = digit;
    return t;
}

if(isalpha(c)){
    std::string alpha;
    do{alpha.push_back(c);
        token_stream.get(c);
    }while((isalpha(c))||(isdigit(c))||(c=='_')||(!isspace(c)));
    if(alpha == "read"){
        t.token = TOKEN::READ;
        t.text = alpha;
        return t;
    }
    if(alpha == "write"){
        t.token = TOKEN::WRITE;
        t.text = alpha;
        return t;
    }
    t.token = TOKEN::ID;
    t.text = alpha;
    return t;
}

Thanks to anyone who can help ahead of time!

Aucun commentaire:

Enregistrer un commentaire