how compare simples aspes?

Mar 21, 2021 at 5:49pm
how can i compare simples aspes?
1
2
3
4
5
6
7
8
9
10
11
12
string text= "safdsA var ifl  ; -.67Ç  68.9\"hello \"\" mother!!!\" \'d\' afdsafd , if do";
for( CharacterIndex=0;CharacterIndex<(int)text.size();  CharacterIndex++)
        {

            //Getting Char:
            if(text[CharacterIndex]=='\'' && blnChar==false)
            {
                cout<< "char";
                blnChar=true;
                Tok.Name+=text[CharacterIndex];
                Tok.Type=Token::Char;
            }

the 'cout' isn't printed, because i belive "text[CharacterIndex]=='\''" is wrong :(
but i don't understand why.. can anyone correct me?
Last edited on Mar 21, 2021 at 5:51pm
Mar 21, 2021 at 6:02pm
double check the value of blnChar.
\' is the single quote escape, so yes, '\'' is correct.
Mar 21, 2021 at 6:07pm
It wouldn't have taken much effort on your part to produce a minimal compilable example to test your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;
int main()
{
  bool blnChar = false;
  string text = "safdsA var ifl  ; -.67Ç  68.9\"hello \"\" mother!!!\" \'d\' afdsafd , if do";
  for (int CharacterIndex = 0; CharacterIndex < (int) text.size(); CharacterIndex++) {
    //Getting Char:
    if (text[CharacterIndex] == '\'' && blnChar == false) {
      cout << "char found at " << CharacterIndex << endl;
      blnChar = true;
      //Tok.Name += text[CharacterIndex];
      //Tok.Type = Token::Char;
    }
  }

  return 0;
}

$ g++ foo.cpp
$ ./a.out 
char found at 51


> the 'cout' isn't printed, because i belive "text[CharacterIndex]=='\''" is wrong :(
> but i don't understand why.. can anyone correct me?
So if you're not seeing the printout, you need to post your real code.



Mar 21, 2021 at 6:07pm
I don't what simples aspes are, but '\'' is indeed the character literal for an apostrophe.

What would you expect to happen in the following code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Example program
#include <iostream>
#include <string>
using namespace std;

enum class Token {
    Char
};

struct TokenInfo {
    std::string Name;
    Token Type;
};

int main()
{
    bool blnChar = false;
    string text= "safdsA var ifl  ; -.67Ç  68.9\"hello \"\" mother!!!\" \'d\' afdsafd , if do";
    TokenInfo Tok;
    
    for( int CharacterIndex=0;CharacterIndex<(int)text.size();  CharacterIndex++)
    {
        //Getting Char:
        if(text[CharacterIndex]=='\'' && blnChar==false)
        {
            cout<< "char";
            blnChar=true;
            Tok.Name+=text[CharacterIndex];
            Tok.Type=Token::Char;
        }
    }
}
Last edited on Mar 21, 2021 at 6:08pm
Mar 21, 2021 at 6:11pm
now 'cout' is printed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void ValidCharacters(string text)
    {
        Tokens Tok;
        int CharacterIndex=0;
        cout << text << "\n";
        bool blnString =false;
        bool blnNumber =false;
        bool blnChar =false;

        for( CharacterIndex=0;CharacterIndex<(int)text.size();  CharacterIndex++)
        {

            //Getting Char:
            if(int(text[CharacterIndex])==39 && blnChar==false)
            {
                cout<< "\nchar\n";
                blnChar=true;
                Tok.Name+=text[CharacterIndex];
                Tok.Type=Token::Char;
            }
            else if(blnChar==true)
            {
               Tok.Name+=text[CharacterIndex];
               Tok.Type=Token::Char;

            }
            else if(int(text[CharacterIndex])==39 && blnChar==true)
            {
                blnChar=false;
                Tok.Name+=text[CharacterIndex];
                //Tok.Type=Token::Char;
                TokenList.push_back(Tok);
                Tok.Name="";
                //Tok.Type=Token::ID;
            }

but the next Tok.Type will be Token::Char instead different :(
i change it from '\'' to '39'.. now enters
Mar 21, 2021 at 6:26pm
that is interesting. locale issue?
what does this print?
int wtf = '\'';
cout << wtf << endl;

all your code sets it to type = Char or the last block does nothing, so it is whatever at that stage. set it to what it needs to be in each block -- you can optimize removal of excess assignments later with a careful trace.
Mar 21, 2021 at 6:29pm
from these text:
string text= "safdsA var ifl ; -.67Ç 68.9\"hello \"\" mother!!!\" \'d\' afdsafd , if do";
i need get the: " 'd' " token or:
string text= "safdsA var ifl ; -.67Ç 68.9\"hello \"\" mother!!!\" \'\t\' afdsafd , if do";
get the " '\t' "... i update the code:
1
2
3
4
5
6
7
8
9
10
11
12
//Getting Char:
            if(int(text[CharacterIndex])==39 && blnString==false && blnNumber==false)
            {
                Tok.Name=39;
                CharacterIndex++;
                Tok.Name+=text[CharacterIndex];
                CharacterIndex++;
                Tok.Name+=39;
                Tok.Type=Token::Char;
                TokenList.push_back(Tok);
                Tok.Name="";
            }

now my problem was resolved ;)
like everyone see, i just change the ' text[CharacterIndex]=='\'' ' to "int(text[CharacterIndex])==39".
the special characters is best compared converted to a ASCII number(int(CharLetter)))
thank you so much for all
Topic archived. No new replies allowed.