how to print strings from array

Oct 12, 2013 at 9:30pm
closed account (G60iz8AR)
Does anyone know how to print strings from array with a filter

// 2.cpp Exercise #2 starter file
// Filter: only print those strings from the array that start with letter 'n'

#include <string> // string class
#include <iostream> // cin, cout: console I/O
#include <fstream> // ifstream, ofstream: file
#include <stdlib.h>
using namespace std;

const int CAP = 500;

bool startsWith_n( string word ); // prototype here. Fully written method below main

int main()
{
ifstream infile;
string arr[ CAP ]; // CAPACITY is 500
int count=0; // how many strings stored in array so far

// TRY TO OPEN THE INPUT FILE "words.txt"
infile.open("words.txt", ios::in);
if ( infile.fail() )
{
cout << "Can't find words.txt. Are you sure it's in this directory? (do a dir command).\nDoes it accidentally have a .txt.txt extension?\n";
exit(0);
}

while( count < CAP && infile >> arr[count] )
count++;
infile.close();

cout << count << " words stored into array from words.txt\n" ;
cout << "Now printing only those words from the array that start with letter 'n':\n";

// DO *NOT* MODIFY THIS LOOP OR ANYTING ELSE IN MAIN.
// JUST FILL IN THE startsWith_nl FUNCTION BELOW MAIN


for ( int i=0 ; i< count ; i++ )
{
if ( startsWith_n( arr[i] ) )
cout << arr[i] << "\n"; // each word on its own line
}

return 0;
}

// ================================================
// == == == Y O U F I L L I N F U N C T I O N B E L O W == == ==
// ================================================

bool startsWith_n( string word )
{
return false; // just to make it compile. you change as needed
}
Oct 12, 2013 at 9:34pm
The code to print from the array is already provided.
You just have to write the filter.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Oct 13, 2013 at 3:27pm
closed account (G60iz8AR)
Do you know how to write the filter is what I'm asking ?
Topic archived. No new replies allowed.