Skipping \n

I am reading a file with ifstream::get in the gettok function. I am trying to create my lexer to ignore '\n' when looping through the file. It, for some reason, when skipping spaces until the next token, does not skip \n. Here is my main lexer code. It is supposed to seperate tokens, and skip \n as well as spaces.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//          LEXER.H
#ifndef LEXER_H_INCLUDED
#define LEXER_H_INCLUDED
#include <cstdio>
#include <sstream>
#include <cctype>
enum tokenset
{
    integer, def, eof
};


class lexer
{
    public:
    tokenset gettok(void);
    std::string lastident;
    bool numvalue;
};

tokenset lexer::gettok(void)
{
    char currentchar;
    tokenset maintok;
    std::string token="";
    std::string::size_type size;
    if(file.is_open())
    {
        do
        {
        file.get(currentchar);
        } while((isspace(currentchar)));
        token+=currentchar;
        if(isdigit(currentchar))
        {
            getchar();
            bool exit=true;
            while(exit)
            {
                file.get(currentchar);
                if(currentchar==' ')
                {
                    exit=false; continue;
                }
                token +=currentchar;
            }
            std::istringstream convert(token);
            if(!(convert >>numvalue))
                numvalue=-1;
        }
        if(isalpha(currentchar))
        {
            bool exit=true;
            while(exit)
            {
            file.get(currentchar);
            if(currentchar == ' ')
            {
                exit=false; continue;
            }
            else
            token +=currentchar;
            }
            if(token=="def")
            {
                lastident=token;
                return def;
            }
        }
        if(currentchar=='#')
        {
            while(currentchar!='\n' && currentchar !='\r' && currentchar!=EOF)
                file.get(currentchar);
            if(currentchar!=EOF)
            {
            return gettok();
            }
            if(currentchar==EOF)
                return eof;
        }
    }
    else
    {
        std::cout <<"File is not open";
        getchar();
    }
    std::cout <<token;
}

#endif // LEXER_H_INCLUDED

/////////////MAIN.CPP//////////#include <iostream>
#include <fstream>
#include <string>
std::ifstream file;
#include "lexer.h"

int main(int argc, char *argv[])
{
    std::string input;
    file.open(argv[1]);
    lexer mainlex;
    std::string random="random";
    if(file.is_open())
    {
    mainlex.gettok();
    std::cout <<mainlex.lastident;
    mainlex.gettok();
    std::cout <<mainlex.lastident;
    std::cin>>input;
    }
    else
    {
        std::cout <<"An error has occured when opening " <<argv[1];
    }
}


Inside the text file im reading says
1
2
def
def


It is outputing
1
2
def
def

Instead of defdef. Please help. It works perfectly when i have in the file
 
def def
Last edited on
Can you please explain?
look at your lexer, then look at the operator i told you to look for. youll feel a lot better if you get it yourself
I have looked through it, changed a few things, but it still wont work. Ive been working at that for days on my own.
Last edited on
no you havent. what operator did i tell you to look for?
'||'
||, and, sorry, but i still am not seeing my error.
you should have told me you changed the | -> ||. im not always going to scroll up to see your code

change the while loop in a do-while loop and use <cctypes>::isspace
I just changed it, and i am still getting the exact same output.
1
2
def
def
Last edited on
I have modified the code some more, and it is still not working. Does anyone see my mistake?
Topic archived. No new replies allowed.