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

  • Home
  • SEARCH
  • 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 6084725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:33:22+00:00 2026-05-23T11:33:22+00:00

#include<iostream> #include<vector> #include<fstream> #include stock.h int main(){ double balance =0, tempPrice=0; string tempStr; vector

  • 0
#include<iostream>
#include<vector>
#include<fstream>
#include "stock.h"
int main(){
    double balance =0, tempPrice=0;
    string tempStr;

    vector < Stock > portfolio;
    typedef vector<Stock>::iterator StockIt;

    ifstream fileIn( "Results.txt" );
    for(StockIt i = portfolio.begin(); i != portfolio.end(); i++)
    {

        while ( !fileIn.eof( ))
        {
            getline(fileIn,tempStr);
            i->setSymbol(tempStr);

            fileIn >> tempPrice;
            i->setPrice(tempPrice);

            getline(fileIn,tempStr);
            i->setDate(tempStr);
        }
        fileIn.close();
    }
    for(StockIt i = portfolio.begin(); i != portfolio.end(); i++){
        cout<<i->getSymbol() <<endl;
        cout<<i->getPrice() <<endl;
        cout<<i->getDate() <<endl;
    }
    return 0;

}

Sample text file, Results.txt:

GOOG    569.964 11/17/2010
MSFT    29.62   11/17/2010
YHOO    15.38   11/17/2010
AAPL    199.92  11/17/2010

Now obviously, I want this program to create a vector of Stock Objects which has the appropriate set/get functionality for object: Stock(string, double, string).

Once that is done, I want to print out each individual member of each Object in the vector.

One thing that boggles my mind about fstream, is how can it decipher spaces and end of lines, and intelligently read strings/ints/doubles and place them into the appropriate data type? Maybe it can’t…and I have to add an entirely new functionality?

now it would seem that I’m not actually creating a new object for each iteration of the loop? I think would need to do something along the lines of:

portfolio.push_back(new Stock(string, double, string));? I’m just not entirely sure how to get to that point.

Also, this code should be interchangeable with std::list as well as std::vector. This program compiles without error, however, there is zero output.

  • 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-23T11:33:23+00:00Added an answer on May 23, 2026 at 11:33 am

    First of all, iterating over the vector only makes sense when it isn’t empty. So remove the line:

    for(StockIt i = portfolio.begin(); i != portfolio.end(); i++)
    

    because otherwise the contents of this loop will never be executed.

    Second, you have problems with your input reading: you use getline for the first field, which would read the values of all 3 fields on the line into the tempStr variable.

    Third, you shouldn’t use while(!fileIn.eof()) – the eof function only returns true after you tried to read past the end of the file. Instead, use:

    while (fileIn >> symbol >> price >> date) {
        //here you should create a Stock object and call push_back on the vector.
    }
    

    This will read the three fields, which are separated by spaces.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
#include <iostream> #include <algorithm> #include <vector> #include <boost/array.hpp> #include <boost/bind.hpp> int main() { boost::array<int,
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
here is code #include <iostream> #include <string> #include <algorithm> #include <vector> #include <fstream> #include
#include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; class Dict {
I have following structure. #include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector>
Consider the following : #include <vector> #include <string> #include <iostream> #include <boost/format.hpp> #include <boost/assign.hpp>
Given this code: #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <boost/regex.hpp> using
i have following code for copying content of vector into file #include<iterator> #include<algorithm> #include<fstream>

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.