CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2013
    Posts
    5

    C++ Program help for a Newbie

    Hello everyone. I am taking an online c++ class which offers very little instruction. I am brand new to programing and I am struggling with an assignment. If you would be kind enough to help me it would be greatly appreciated.

    Here is the problem: I am trying to write a program that ask the user to enter a sentence and then a word to be searched for in the sentence. The program must then search for the word in the sentence in a loop and continue to output each place the word is found. For example if the sentence is : I like pickles, pickles, pickles

    and you searched for pickles it would return pickles found at 7, pickles found at 16, pickles found at 25.

    I am having trouble writing the equation to make the find keep searching after each occurrence of the word being searched.

    Here is the code I have so far
    HTML Code:
    #include <iostream>
    #include <string> 
    
    using namespace std;
    
    
    int main()
    {
    	string paragraph;
    	cout << "Please enter your paragraph:";
    	getline (cin,paragraph);
    	cout << "Hello, your paragraph is " << paragraph << "!\n";
    	
    	cout << "The size of your paragraph = " << paragraph.size() << " characters. \n\n";
    	string word;
    	cout << "Please enter the word you are searching for:";
    		getline (cin,word);
    		cout << "Hello, your word is " << word << "!\n";
    	size_t position = paragraph.find(word);
    
    	
    
    bool wordsearch = true;
    	while (paragraph.find(word) != string::npos)  {
    		
    
      if (paragraph.find(word) == string::npos)
    		cout << "" << word << " does not exist in the sentence" << endl;
    		size_t position = paragraph.find(word);
    
      if (position!=string::npos)
        cout << "The word " << word << " is found at: " << position << '\n';
    
      position=paragraph.find((word),position+1);
      if (position!=string::npos)
        cout << "The word " << word << " is also found at " << position << '\n';
      position=paragraph.find((word),position+1);
      if (position!=string::npos)
        cout << "The word " << word << " is also found at: " << position << '\n';
     
     position=paragraph.find((word),position+1);
      if (position!=string::npos)
        cout << "The word" << word << "is also found at: " << position << '\n';
    
      
    
    
      
      else  
    	  cout << "There are no more occurences of the word " << word << '\n';
    
    	system ("pause");
    	
    	}while (wordsearch = true);
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: C++ Program help for a Newbie

    Quote Originally Posted by radkowboy View Post
    Hello everyone. I am taking an online c++ class which offers very little instruction.
    What " online c++ class" do you mean?
    I see neither "online" nor "offline" class definition in your post.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: C++ Program help for a Newbie

    Please use code tags when posting code and not HTML tags. Go Advanced, select the code and click '#'

    You're nearly got it, but there's no need to have 3 find tests in the loop. Just one in the while loop condition is all that is needed. So your code can be simplied to

    Code:
    #include <iostream>
    #include <string> 
    using namespace std;
    
    int main()
    {
    string paragraph;
    
    	cout << "Please enter your paragraph: ";
    	getline (cin,paragraph);
            cout << "Hello, your paragraph is " << paragraph << "!\n";
    	cout << "The size of your paragraph = " << paragraph.size() << " characters.\n\n";
    
    string word;
    	cout << "Please enter the word you are searching for: ";
    	getline (cin,word);
    	cout << "Hello, your word is " << word << "!\n";
    
    size_t position = 0;
    bool wordsearch = false;
    
    	while ((position = paragraph.find(word, position)) != string::npos) {
    	        cout << "The word " << word << " is found at: " << position++ << endl;
            	wordsearch = true;
    	}
     
    	if (wordsearch)
    		cout << "There are no more occurences of the word " << word << endl;
    	else
    		cout << "" << word << " does not exist in the sentence" << endl;
    
      return 0;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: C++ Program help for a Newbie

    Quote Originally Posted by VictorN View Post
    What " online c++ class" do you mean?
    I see neither "online" nor "offline" class definition in your post.
    I think he means an internet training course.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Oct 2013
    Posts
    5

    Re: C++ Program help for a Newbie

    2kaud,

    Thank you for your help. You are a life saver. I will be sure to use the code tag next time. I have a question for you if you don't mind. Can you explain to me what the bool statements are doing in this program? I have trouble using them properly.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: C++ Program help for a Newbie

    bool variables take either the value true (non zero) or false (0). They are often used to indicate that a certain condition or criteria have been met. In this case the bool wordsearch indicates if a word has been found or not. When a word has been found it is set to true. Once the end of the paragraph has been reached and the while loop terminated, wordsearch is tested to see if true (non zero) or not and appropriate messages output.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured