File Read & Write

closed account (DEhqDjzh)
Hello, To Improve my C++ skills (and some boredom), I've decided to create my mini programming language. It will read a string from a file and react as the context of the string. Here
is my question

- How to read a string in specific characters and give an error if they are missing. e.g:
1
2
3
4
5
6
7
8
9
if(readed_string == "print")
{
// here how to check if there are () or not.
if(have_())
{
// how to read text within "" ?
}

}
Last edited on
you can use find() or just pure iteration. find is nicer looking maybe but you iterate the string twice instead of once.
1
2
3
4
5
6
7
8
9
10
bool bad = false;
for(... rs.length)
{
  if(rs[i] == '(' || rs[i] == ')') 
   {
     bad = true;  
     break;
   }
}


Last edited on
Here is another way if you are interested. {auto}.
https://en.cppreference.com/w/cpp/language/range-for

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main() {
	string a = "lost (of) stuff"; 

	for (auto i : a) {
		if (i == '(' || i == ')')
			continue; 
		cout << i << endl; 
	}

	system("pause"); 
}
closed account (DEhqDjzh)
@tibrado Thanks !
Topic archived. No new replies allowed.