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 217485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:39:42+00:00 2026-05-11T18:39:42+00:00

#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index

  • 0
#include <iostream>
#include <string>
#include <fstream>

using namespace std ;


string strWord( int index , string line)
{
       int count = 0;
       string word;
       for ( int i = 0 ; i < line.length(); i++)
       {
           if ( line[i] == ' ' )
           {
                if ( line [i+1] != ' ')
                {

                    count ++;
                    if (  count == index)
                    {
                       return word;
                    }
                    word ="";
                }
           }
           else
           {
                word += line[i];
           }       
       }
}







int main ( )
{
    ifstream inFile ;
    inFile.open("new.txt");
    string line  , id;

    cout <<"Enter id : ";
    cin >>id;
    while(!inFile.eof() )
    {
                        getline ( inFile , line );
                        if ( strWord ( 1, line ) == id ) 
                        {
                             cout <<strWord ( 2 , line ) <<endl;
                             break;
                        }
    } 
    system("pause");
}

Question is : Can someone explain this to me I do not get what it is doing i mean i get the concept but what is each line doing?

  • 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-05-11T18:39:43+00:00Added an answer on May 11, 2026 at 6:39 pm

    You wanted a comment of each line

    // function that returns a word from 'line' with position 'index'
    // note that this is not a zero based index, first word is 1,
    // second is 2 etc ..
    string strWord(int index, string line)
    {
        int count = 0; // number of read words
        string word; // the resulting word
        for (int i = 0 ; i < line.length(); i++) { // iterate over all characters in 'line'
            if (line[i] == ' ') { // if this character is a space we might be done reading a word from 'line'
                if (line[i+1] != ' ') { // next character is not a space, so we are done reading a word
                    count++; // increase number of read words
                    if (count == index) { // was this the word we were looking for?
                        return word; // yes it was, so return it
                    }
                    word =""; // nope it wasn't .. so reset word and start over with the next one in 'line'
                }
            }
            else { // not a space .. so append the character to 'word'
               word += line[i];
            }       
        }
    }
    
    
    int main( ) // main function of the program, execution starts here
    {
        ifstream inFile; // construct input file stream object
        inFile.open("new.txt"); // associate the stream with file named "new.txt"
        string line, id; // 
    
        cout << "Enter id : "; // write "Enter id :" to console
        cin >> id; // read input from console and put the result in 'id'
    
        while (!inFile.eof()) { // do the following as long as there is something to read from the file
            getline(inFile, line); // read a line from the file and put the value into 'line'
        if (strWord(1, line) == id) { // if the first word in 'line' equals 'id' ..
            cout << strWord(2, line) << endl; // prints the second word in 'line'
            break; // exits the while loop
        }
        } 
    
        system("pause"); // pause the program (should be avoided)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

This very simple code gives me tons of errors: #include <iostream> #include <string> int
I'm working this source code: #include <string> #include <vector> #include <iostream> #include <istream> #include
I have the following code: #include <iostream> #include <string> #include sqlite3.h int main() {
I'm getting compile error in this code #include<iostream> #include<cstdio> #include<string> using namespace std; void

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.