comparing two substrings

Hi guys below is my code...so am trying to read for the input file then compare a specific part of a string using their substrings

input file format is:

1
2
3
4
5
6
7
8
9
10
11
12
13
Mark      Bwalya  2436586  20146438  2014
Max       Peter   7483289  20174893  2017
Lisa      Phiri   3674765  20813672  2018
Chitalu   Malama  4672762  20146437  2014
Frank     Tambo   6546727  20016367  2001
Malika    Chewe   4729208  20137346  2013
Raymod    Daka    3894782  20157835  2018
Lucy      Kalinga  4849535  20164675  2016
Jack      Kakwekwe 7548394  20143757  2014
 Oka      gjriudf  6458934  20135743  2013
Emmanucle Fuka    4325673  20137578  2013
Brian     Mwale   5327834  20174673  2017
Lisa      MWeka   1895865  20013647  2001


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
 #include <iostream>
#include <string>
#include <fstream>


using namespace std;

int main()
{
        ifstream file1;
        file1.open("input.txt");
        string str1;                
        string str2 =str1.substr(12,8);   
        string str3 = "Max peter 7483289 20174893 2017"; 
        string str4 = str2.substr(12,8); //the position
        if(file1.is_open())
        {
        while(!file1.eof())
        {
        getline(file1, str1);

         if (str2 == str4)
         {
         cout << "match!";
         }
         else
         {
         cout << "In line " << str1 << " there is no a match of: " << str2 << endl;
        }
      }
    }
    return 0;
}   


when i run the program it is crushing writing "terminate called after throwing an instance of 'std::out_of_range' what<>: basic_string::substr: __pos<which 12> >this ->size() <which is 0> This application has resquested the runtime to terminate it in an unusual way.Please contact the application's support team for more information."
Last edited on
You declare a string str1 on line 12.

Immediately after declaration it is empty (size 0).

So, how do you envisage finding the 12th character of this string on line 13?
-if it reads and compares successfully expected output
In line Mark Bwalya 2436586 20146438 2014 there was no match of 7483289

-then it moves to the next line until
In line Max peter 7483289 20174893 2017 there was a match of 7483289
Last edited on
You declare a string str1 on line 12.

Immediately after declaration it is empty (size 0).

-So, how do you envisage finding the 12th character of this string on line 13?

so would you recommend the use of getline?

kennedy
Why so complicated?
Much more natural and easier is:
1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream file1("input.txt");
if(!file1)
{
   // handle errror
   return 1;
}
string first_name, last_name;
int num1 = 0, num2 = 0, num3 = 0;

while (file1 >> first_name >> last_name >> num1 >> num2 >> num3)
{
  // do sth. with the data
}
1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream file1("input.txt");
if(!file1)
{
   // handle errror
   return 1;
}
string first_name, last_name;
int num1 = 0, num2 = 0, num3 = 0;

while (file1 >> first_name >> last_name >> num1 >> num2 >> num3)
{
  // do sth. with the data
}


but then how to i do the comparing if the (num) is set to zero?
Inside the while loop you check if num1 == 0
Topic archived. No new replies allowed.