Searching for character in string

closed account (EvCSLyTq)
I'm trying to find a < character in a document, get it's position. Then find > and get it's position. Then i want to delete all things between that but runtime is terminating my process so i don't know what to do.

The 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <string>
#include <fstream>

using namespace std;

int main(){
    
    string text;
    string search = "<";
    string search2 = ">";
    int position1=0, position2=0;
    
  ofstream myfile;
  myfile.open ("output.txt");

  ifstream file;
  file.open ("text.txt");

//Finds < and >
while (file.good())
    {
      getline (file,text);
  std::size_t found = text.find(search);
  if (found!=std::string::npos)
  {
    position1 = found;
    cout << position1 << endl;
}

  found = text.find(search2);
  if (found!=std::string::npos)
  {
    position2 = found;
    cout << position2 << endl;
}

//delete everything between position1 and position2
text.erase(position1, position2+1);
cout << text << endl;
myfile << text << endl;
}
  file.close();
  myfile.close();

    return 0;
}
!indentation
<nolyc> indentation is optional whitespace. see also: !vowel
!vowel
<nolyc> vwls r bt s sfl s whtspc, spclly n vrbl nms. s ls: !ndnttn
!ndnttn
<nolyc> indentation is optional whitespace. see also: !vowel


Line 41 should execute only when you do found the substrings. Also, make sure that `position2>=position1'

Your loop is incorrect, use while( getline(file,text) ) instead
closed account (EvCSLyTq)
What do you mean by
Line 41 should execute only when you do found the substrings.
?
Sorry. I'm new at programming.
And thank you for other help.
Suppose that text is
`the quick brown fox jumps over the lazy dog'
¿what value position1 and position2 would hold?
¿what text.erase(position1, position2); would do in that case?
1
2
//delete everything between position1 and position2
text.erase(position1, position2+1);


This is wrong. This overloaded version of string::erase takes as its first argument the index of the first character to remove and as its second argument the number of characters to remove, so: text.erase(position1, position2-position1+1) is what you should be using. Of course, you should also verify that position2 comes after position1 in the string.

You should probably also verify that both files are being opened.
closed account (EvCSLyTq)
Thank everybody, but i don't know how to program what ne555 said:

Suppose that text is
`the quick brown fox jumps over the lazy dog'
¿what value position1 and position2 would hold?
¿what text.erase(position1, position2); would do in that case?

What if statement should i insert?
What if statement should i insert?

You already have the right if-statements when you're finding; but you're not using them in the right way. You cannot erase if you don't have valid start and end positions. (Where are the < and > in the string ne555 gives?)

It would be better to start the second find from one past the position of the first one. The position to start with can be specified using the second parameter (which default to 0)

Also, your code (if you sort out the if-statements) will only remove one <tag> a line. You'll need another loop if you want to remove more than one per line.

string::find
http://www.cplusplus.com/reference/string/string/find/

You should probably also verify that both files are being opened

ifstream::is_open
http://www.cplusplus.com/reference/fstream/ifstream/is_open/
ofstream::is_open
http://www.cplusplus.com/reference/fstream/ofstream/is_open/

Andy
Last edited on
Topic archived. No new replies allowed.