May 23, 2020 at 8:58am May 23, 2020 at 8:58am UTC
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 May 23, 2020 at 11:32am May 23, 2020 at 11:32am UTC
May 23, 2020 at 9:02am May 23, 2020 at 9:02am UTC
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?
May 23, 2020 at 9:08am May 23, 2020 at 9:08am UTC
-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 May 23, 2020 at 11:30am May 23, 2020 at 11:30am UTC
May 23, 2020 at 12:09pm May 23, 2020 at 12:09pm UTC
Inside the while loop you check if num1 == 0