Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8115979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:39:50+00:00 2026-06-06T03:39:50+00:00

The code below is for storing a group of words in a std::vector and

  • 0

The code below is for storing a group of words in a std::vector and to count how many times a particular word given by the user appears in the vector by comparing it to all the words stored in the vector.

The console does not prompt me for input at the second std::cin >> in the program below.

#include <iostream>
#include <ios>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;

int main(int argc, const char * argv[])
{
   cout<<"enter your words followed ny EOF"<<endl;
   vector<string> words;
   string x;
   typedef vector<string>::size_type vec_size;
   vec_size size;
   while (cin>>x) 
   {
     words.push_back(x);
   }
   size=words.size();
   cin.clear();

   //now compare
   cout<<"enter your word:"<<endl;
   string my_word;
   int count=0;

   cin>>my_word;              //didn't get any prompt in the console for this 'cin' 
   for (vec_size i=0; i<size; ++i) 
   {
      my_word==words[i]?(++count):(count=count);
   }
   cout<<"Your word appeared "<<count<<" times"<<endl;
   return 0;

}

The final output I get is “Your word appeared 0 times”.
What is the problem with the code. Any help would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:39:52+00:00Added an answer on June 6, 2026 at 3:39 am

    The program reads the word list until end of file. So, at a terminal, you can type the EOF character (Ctrl-D on Linux, Ctrl-Z Return on Windows), but what then?

    I think after resetting the stream, a terminal will continue to read. But if the program is taking input from a disk file, pipe, etc., there is no hope. End-of-file is forever.

    Instead, use some sort of sentinel, or prefix it by a count. That way the first loop can run until the logical end of the list. And then it can read the word intended for the summary logic.

    while (cin>>x  &&  x != '*')   // The word "*" ends the list of words
       {
         words.push_back(x);
       }
       size=words.size();
    
       //now compare
       cout<<"enter your word:"<<endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble understanding why the code below #include <string> #include <vector> #include <map>
I have the code below: string SQL = select * from + TableName; using
I have strings formatted using the code below String.Format({0,-10} {1,10}, Kills:, kills); String.Format({0,-10} {1,10},
Please have a look at the code below: import string from collections import defaultdict
The code below is designed to take a string in and remove any of
The code below produces an error saying that my input string was not in
I have code as below: var s : String = hello world var xml
For the code below, I would like to append echo $_SERVER['QUERY_STRING']; to the end
Consider the code below, private void Convert_Click(Object sender, RoutedEventArgs e) { string[] strCmdLineParams =
I've been stumped by the code below. I pass a string date to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.