Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 23rd, 2004, 1:25 AM   #1
HostileRadish
Newbie
 
Join Date: Nov 2004
Posts: 2
Rep Power: 0 HostileRadish is on a distinguished road
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;
}
HostileRadish is offline   Reply With Quote
Old Nov 23rd, 2004, 1:38 PM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
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;
}
Eggbert is offline   Reply With Quote
Old Nov 24th, 2004, 11:08 AM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Another way... check out the basic_string of the standard template library...

http://www.sgi.com/tech/stl/basic_string.html
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Nov 24th, 2004, 4:48 PM   #4
monkey8
Newbie
 
Join Date: Nov 2004
Posts: 12
Rep Power: 0 monkey8 is on a distinguished road
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.
monkey8 is offline   Reply With Quote
Old Dec 1st, 2004, 10:24 PM   #5
HostileRadish
Newbie
 
Join Date: Nov 2004
Posts: 2
Rep Power: 0 HostileRadish is on a distinguished road
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.
HostileRadish is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:24 PM.

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