-
February 3rd, 2014, 05:50 PM
#1
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;
}
-
February 4th, 2014, 06:30 AM
#2
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.
-
February 5th, 2014, 04:49 PM
#3
Re: Help with Vector input/couting
 Originally Posted by Philip Nicoletti
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|