![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 2
Rep Power: 0
![]() |
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; } |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
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;
} |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2004
Posts: 12
Rep Power: 0
![]() |
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; |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2004
Posts: 2
Rep Power: 0
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|