0

im trying to write a program that involves a text file. my goal is to get any line that starts with the string "From:" and put that line without the "From" into an array

this is what ive come up so far, but it seems to be doing the exact opposite of what i want and saves every line that DOESNT have a "From:" in the line

#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <list>

using namespace std;

int main() 
{
  ifstream file;
  string array[10000];
  string line;
  string yep = "From:";
  int i = 0;
  file.open("myFile.txt"); //name of file im trying to open 

  while(!file.eof())
  {
    getline(file, line);
    
    if (line.find((yep)))
    {
        array[i++]=line;
    }
  }


  file.close();
  cout<< array[0] << endl;
 cout<< array[1] << endl;
 cout<< array[2] << endl;
 cout<< array[3] << endl;
 cout<< array[4] << endl;
 cout<< array[5] << endl;
 cout<< array[6] << endl;
 cout<< array[7] << endl; 
 cout<< array[9] << endl;
 cout<< array[10] << endl;
 cout<< array[11] << endl;
 cout<< array[12] << endl;
 cout<< array[13] << endl;
 cout<< array[14] << endl;
 cout<< array[15] << endl;
 cout<< array[16] << endl; 
 cout<< array[17] << endl; 
}

here is what is in "myFile.txt."

From:  Seoul, South Korea
To  :  Toronto, Canada
       Luxembourg, Luxembourg
       Seattle, United States
       Dhaka, Bangladesh
       Guatemala City, Guatemala
From:  Tokyo, Japan
To  :  Ottawa, Canada
       Vilnius, Lithuania
       Rome, Italy
From:  Hong Kong, SAR
To  :  New York City, United States
       New Delhi, India
       Washington, United States
       Dublin, Ireland
       Lisbon, Portugal
       Vienna, Austria
       Santiago, Chile
       Rio de Janeiro, Brazil
       Berlin, Germany
From:  London, United Kingdom
To  :  Accra, Ghana
From:  Osaka, Japan
To  :  Guatemala City, Guatemala
       Helsinki, Finland
       Detroit, United States
       Vienna, Austria
       Shenzhen, People's Republic of China
       Harare, Zimbabwe
       Montreal, Canada
       Jakarta, Indonesia
       Birmingham, United Kingdom
From:  Geneva, Switzerland
To  :  Ljubljana, Slovenia
       Kiev, Ukraine
       Pittsburgh, United States
       Bratislava, Slovakia
       Mumbai, India
       Luxembourg, Luxembourg
       Rome, Italy
       Lisbon, Portugal
       Abu Dhabi, United Arab Emirates
From:  Copenhagen, Denmark
To  :  Beijing, People's Republic of China
       Pittsburgh, United States
From:  Zurich, Switzerland
To  :  Seattle, United States
       Caracas, Venezuela
       Manila, Philippines
From:  Oslo, Norway
To  :  Dusseldorf, Germany
       Shanghai, People's Republic of China
       New Delhi, India
       White Plains, United States
       Pittsburgh, United States
       Denver, United States
       Rome, Italy
       Quito, Ecuador
       Buenos Aires, Argentina
From:  New York City, United States
To  :  Abidjan, Cote d'Ivoire
       Casablanca, Morocco
       Jakarta, Indonesia
       Copenhagen, Denmark
       Kingston, Jamaica
From:  St. Petersburg, Russia
To  :  Amman, Jordan
From:  Milan, Italy
To  :  Houston, United States
       Mexico City, Mexico
From:  Beijing, People's Republic of China
To  :  Ljubljana, Slovenia
       Kuala Lumpur, Malaysia
       Dhaka, Bangladesh
       Melbourne, Australia
       Sydney, Australia
       Casablanca, Morocco
       Munich, Germany
       Istanbul, Turkey

here is the output:

To  :  Toronto, Canada
       Luxembourg, Luxembourg
       Seattle, United States
       Dhaka, Bangladesh
       Guatemala City, Guatemala
To  :  Ottawa, Canada
       Vilnius, Lithuania
       Rome, Italy
       New Delhi, India
       Washington, United States
       Dublin, Ireland
       Lisbon, Portugal
       Vienna, Austria
       Santiago, Chile
       Rio de Janeiro, Brazil
       Berlin, Germany
To  :  Accra, Ghana
yessir
  • 1
  • 2
  • 3
    Considering that your line *starts* with the required text, `line.find(yep)` will return 0 - which evaluates as `false`. You should be checking `line.find(yep) != std::string::npos` – UnholySheep Apr 22 '22 at 19:39
  • 2
    Consider removing the array, as it is not needed. Replace the array assignment statement with your output statement. If you need to remember the `from` lines, consider using `std::vector` because you don't know how big the result set will be at run-time. – Thomas Matthews Apr 22 '22 at 20:07
  • Also, on a side note: [do not use `while(!file.eof())`](https://stackoverflow.com/questions/5605125/) – Remy Lebeau Apr 22 '22 at 21:05

1 Answers1

3

The issue is your if statement. The string::find() method returns the index of the first character in the substring being searched for. In this case, that's 0 (the start of the line), meaning the if evaluates to false. If the substring you're looking for is not present, find() returns std::string::npos, which is non-zero and thus in your if is evaluating to true.

What you should do is switch from if (line.find((yep))) to if (line.find(yep) == 0) if the "From" needs to be at the beginning of the line, or to if (line.find(yep) != string::npos) if it just needs to be contained anywhere in the line.

Xystem4
  • 66
  • 5
  • 1
    Actually since the question demands "starts with", the correct test would be `== 0` rather than `!= npos` – Ben Voigt Apr 22 '22 at 21:19