English 中文(简体)
不能比较C++的地体
原标题:Cannot compare strings in C++
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
#include <iostream>

FILE *pfile;
using namespace std;

string temp_string;
string reserved[25] = {"AND", "CALL", "DECLARE", "DO", "ELSE", "ENDDECLARE", "ENDFUNCTION", "ENDIF", "ENDPROCEDURE", "ENDPROGRAM", "EXIT", "FALSE", "FOR", "FUNCTION", "IF", "IN", "INOUT", "NOT","OR", "PROCEDURE", "PROGRAM", "RETURN", "THEN", "TRUE", "WHILE"};


int main(void)
{
    pfile = fopen("hello.cel", "r");
    char cha, temp_token[30], temp;
    int count = 0, check = 1, i;
    cha = fgetc(pfile);
    while (cha != EOF)
    {
        if(isalpha(cha) || cha ==  _ )
        {
            temp_token[0] = cha;
            count = 1;
            cha = fgetc(pfile);
            while(isdigit(cha) || isalpha(cha) || cha ==  _ )
            {
                if(count < 30)
                {
                    temp_token[count] = cha;
                    count++;
                }
                cha = fgetc(pfile);         
            }
            count--;
            for(i = 0; i <= count; i++)
            {
                temp_string += temp_token[i];
            }
            cout << temp_string;
            for(i = 0; i < 25; i++)
            {
                if(temp_string == reserved[i])
                {
                    printf(": RESERVED
");
                }
                else
                {
                    printf(": ALPHA
");
                }
            }

            cha = ungetc(cha, pfile);
            count = 0;
        }
        fclose(pfile);
}

I have a problem with the comparison statement between the reserved[i] and temp_string strings. I cannot succeed printing "RESERVED", it always print "ALPHA". To your knowledge, this is a program that gets each character from a file (hello.cel) and prints the type of each token.

EDIT:诱因是临时储存词。 添加以下文字:<代码>temp_string += 页: 1

问题回答

不宣布节奏。

Have you declared temp_string as string? For me it prints Reserved for keywords.

坡道的尾部有线chy;你有一条缺失的<条码>><>>>>和<条码>ungetc()像完全错做的。 你们需要改变

            cha = ungetc(cha, pfile);
            count = 0;
        }
        fclose(pfile);
}

纽约总部

        }
        cha = fgetc(pfile);
    }
    fclose(pfile);
}

Also declare temp_string just before the loop that populates it (or, if you really want it 纽约总部be global for some reason, call clear() at that point). Better still, initialise it from the buffer, after removing the pointless count--:

std::string temp_string(temp_token, temp_token+count);

甚至更糟的是,摆脱了临时缓冲,并且正如你所读的那样,建造了扼子:

        std::string token(1, cha);
        cha = fgetc(pfile);
        while(isdigit(cha) || isalpha(cha) || cha ==  _ )
        {
            if(token.size() < 30)
            {
                token.push_back(cha);
            }
            cha = fgetc(pfile);         
        }

最后,在检查所有保留标记后,只有打印的<代码>ALPHA:

bool is_reserved = false;
for(i = 0; i < 25; i++)
{
    if(token == reserved[i])
    {
        is_reserved = true;
        break;
    }
}
printf(": %s
", is_reserved ? "RESERVED" : "ALPHA");

Here是一个不太破碎的版本。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签