Hello, the compiler has a problem with my "std::string line". It's in there twice, but I didn't know how else to do it. The program is like a lexical analyzer. What's the best way to fix this?
#include <iostream>
#include <fstream>
#include <string>
#include <iostream>
#include <string>
#include <cctype> // for std::isspace(), etc.
int main(int argc, char *argv[]) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " input-file\n";
return 1;
}
// open file
std::ifstream input(argv[1]);
if(!input.is_open()) {
std::cerr << "Error opening file \"" << argv[1] << "\"\n";
}
std::string line;
while(std::getline(input, line)) {
std::cout << "Got line: [" << line << "]\n";
}
//search
void print_tokens(const std::string &data);
std::string line;
while(std::getline(std::cin, line)) {
print_tokens(line);
}
return 0;
}
void print_tokens(const std::string &data) {
enum {
MODE_OUTSIDE,
MODE_IN_WORD,
MODE_SYMBOL
} mode = MODE_OUTSIDE;
std::string::size_type x = 0;
while(x < data.length()) {
char c = data[x];
switch(mode) {
case MODE_OUTSIDE:
if(std::isspace(c)) {
x ++;
}
else if(std::isalnum(c)) {
mode = MODE_IN_WORD;
std::cout << '"';
}
else {
mode = MODE_SYMBOL;
}
break;
case MODE_IN_WORD:
if(std::isalnum(c)) {
std::cout << c;
x ++;
}
else {
std::cout << '"' << std::endl;
mode = MODE_OUTSIDE;
}
break;
case MODE_SYMBOL:
if(!std::isalnum(c) && !std::isspace(c)) {
std::cout << "[" << c << "]\n";
x ++;
}
else {
mode = MODE_OUTSIDE;
}
break;
}
}
}