I have an assignment to read from a file and to print only the names that have spaces in them. This is my text file:
Arkansas
California
Colorado
Connecticut
Delaware
District Of Columbia
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Ohio
Oklahoma
Oregon
Pennsylvania
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming
American Samoa
District of Columbia
Guam
Northern Mariana Islands
Puerto Rico
United States Virgin Islands
How do I get specific names to print? Thank you for your time.
#include <iostream>
#include <string>
#include <fstream>
bool has_char( const std::string& str, char ch )
{
// http://www.cplusplus.com/reference/string/string/find/return str.find(ch) != std::string::npos ;
}
bool has_space( const std::string& str ) { return has_char( str, ' ' ) ; }
int main()
{
std::ifstream file( "whatever.txt" ) ; // open file for input
std::string line ;
// http://www.cplusplus.com/reference/string/string/getline/while( std::getline( file, line ) ) // for each line in the file
if( has_space(line) ) std::cout << line << '\n' ; // print it if it has a space
}