Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   C++ Reading With Arrays [solved] (http://www.programmingforums.org/showthread.php?t=1249)

HostileRadish Nov 23rd, 2004 2:25 AM

I've been working on a program for class that is supposed to read in a phrase entered by the user, along with a paragraph entered by the user. After this data is entered, the program is supposed to find out how many times the phrase (2 words) shows up in the paragraph. However, I'm having a lot of trouble getting an array to read each word in the paragraph separately (so that the word can be compared to the 2 words in the phrase... if this is successful, it will increment 'count' which will be displayed at the end)... right now the loop won't close at all (which I believe was the result of the "cin.get(word,22,' ');" line below), and it's not reading the word into the array at all.

Any help would be appreciated :) - I've posted the code I have so far below:


#include <iostream.h>
#include <cstring>

void welcome();
void exit();

int main()
{

char phrase[22];
char phrase2[22];
char word[22];
char paragraph[500];
int count;
char response = 'y';
while (response == 'y')
{
welcome();
cout << "Phrase: ";
cin.width(22);
cin >> phrase;
cin.width(22);
cin >> phrase2;
cout << "You entered:\"" << phrase << " " << phrase2 << "\"";
cout << endl;
cout << endl;
cout << "Please enter the paragraph you want analyzed below.\n";
cout << "At the end of your paragraph, please enter in \"#\"\n";
cout << "and then hit enter once more.\n";
cout << endl;
cout << "Paragraph: ";
cin >> paragraph;
while (word[22] != '#')
{
cin.get(word,22,' ');
if (strcmp (word,phrase) == 0)
{
cout << "put cin.get here, followed by if for num2 and the ++count command";
}
else
{
}
}
cout << endl;
cout << endl;
cout << "The phrase \"" << phrase << "\" appeared " << count << " times.\n";
cout << endl;
cin.ignore();
cout << "Would you like to search for another phrase (y/n)? ";
cin >> response;
cin.ignore();
}
exit();

return 0;
}

void welcome()
{
cout << endl;
cout << "--------------------";
cout << endl;
cout << "Welcome to the Phrase Finder program!\n";
cout << "Please enter in a phrase below (2 words, separated by an enter)\n";
cout << "and a paragraph. The prorgam will then report\n";
cout << "the number of times this phrase shows up in the paragraph.\n";
cout << endl;
}

void exit()
{
cout << endl;
cout << endl;
cout << "Thank you for using the Phrase Finder program. Please search again!\n";
cout << "--------------------";
cout << endl;
cout << endl;
}

Eggbert Nov 23rd, 2004 2:38 PM

How much leeway do you have in using the standard library? C++ isn't good with string manipulation, especially if you restrict yourself to C-style strings. The easiest solution would be something like this:
:

#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
 char phrase[256];
 char paragraph[8096];

 cout<<"Enter a paragraph: ";
 if ( !cin.getline ( paragraph, sizeof paragraph ) ) {
  cerr<<"Input error"<<endl;
  return EXIT_FAILURE;
 }

 cout<<"Enter a phrase to search for: ";
 if ( !cin.getline ( phrase, sizeof phrase ) ) {
  cerr<<"Input error"<<endl;
  return EXIT_FAILURE;
 }

 unsigned count ( 0 );
 const char *p ( paragraph );
 size_t len ( strlen ( phrase ) );

 while ( ( p = strstr ( p, phrase ) ) != NULL ) {
  ++count;
  p += len;
 }

 cout<< count <<" occurances of the phrase were found"<<endl;

 return EXIT_SUCCESS;
}


Infinite Recursion Nov 24th, 2004 12:08 PM

Another way... check out the basic_string of the standard template library...

http://www.sgi.com/tech/stl/basic_string.html

monkey8 Nov 24th, 2004 5:48 PM

Since you're using c++, don't hurt yourself by using the old c libraries. (this is a common mistake, a lot of beginners don't know the difference between c and c++ libraries)

You're also using the c I/O library. Use
:

#include <string>
#include <iostream>

using namespace std;

And also check out vectors. They are like arrays, only they can dynamically change size. You don't have to use malloc to create/destroy new memory for them.

HostileRadish Dec 1st, 2004 11:24 PM

Sorry for the late reply, was gone over Thanksgiving. Thanks for the help, I was able to finish the program - didn't know there was much difference between iostream.h and iostream, I'll make sure to use <iostream> from now on in my programs.


All times are GMT -5. The time now is 1:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC