CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2014
    Posts
    2

    Help with Vector input/couting

    Hello, I am 3/4 weeks into programming curriculum, so this should be a relatively easy question! I am trying to write a program that counts specific words that a user inputs "Howdy/howdy/Whoop/whoop" (yes I go to Texas A&M, hence those specific words haha ) I am having an issue where it wont count the first word even if it is "Howdy"

    For example if I put it:

    "Howdy howdy whoop Whoop" - it only outputs that it counted 3 words

    now if I were to do:

    "Hello Howdy howdy whoop Whoop" - it would count 4.

    Where did I go wrong? Thank you for any help!!

    Code:
    //Jordan Lewallen
    //ENGR 112 - 512
    //February 4, 2014
    //hw2pr3.cpp
    
    #include "std_lib_facilities_4.h"
    
    int main(){
    	
    	cout << "Please enter desired words, when you have entered all words, please type CTRL+d (EOF Command)\n" << endl; 
    	//It was assumed that EOF command was going to be used here hence the necessity of 'CTRL+d'
    	
    	vector<string>words;
    		string the_word;
    		cin >> the_word;  //when the desired words are entered, the if loop will catch those and enter them into the vector
    		while (cin >> the_word)
    		if (the_word == "Whoop" || the_word == "whoop" || the_word == "Howdy" || the_word == "howdy"){
    			words.push_back(the_word); //pushing the words into vector
    			}
    		cout << "Number of desired words: " << words.size() << endl; //displays the number of specific words that needed to be counted
    
    		return 0;
    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Help with Vector input/couting

    You have:

    Code:
    cin >> the_word;  //when the desired words are entered, the if loop will catch those and enter them into the vector
    while (cin >> the_word)
    {
     // etc.
    }
    The first cin reads the first word that is entered.

  3. #3
    Join Date
    Feb 2014
    Posts
    2

    Re: Help with Vector input/couting

    Quote Originally Posted by Philip Nicoletti View Post
    You have:

    Code:
    cin >> the_word;  //when the desired words are entered, the if loop will catch those and enter them into the vector
    while (cin >> the_word)
    {
     // etc.
    }
    The first cin reads the first word that is entered.
    Fixed it, and everything works. Thanks for your help!

Tags for this Thread

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