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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:00:42+00:00 2026-05-27T07:00:42+00:00

I’m having trouble with the program. The program is intended to take input from

  • 0

I’m having trouble with the program. The program is intended to take input from a file in the form of names separated by a comma, and tally them in an output file (which I haven’t gotten to yet). It fails before “good so far 3”. Apparently, what it’s breaking on is defrencing the iterator it. That is to say, what I’m trying to do is use it as a pointer, and ++ that pointer. This should skip ahead to the next struct in the array. Then, I want to use it->inputName to access something inside of the struct. However, when I do it->inputName, it tries to use it as a pointer and it can’t do that. Not sure where to go from here, if I try to make a regular pointer like name * it, it doesn’t work with nameStorage.begin(), as that only takes an iterator. I’m on windows 7 using Microsoft Visual Studio 2010, if that helps.

I’m fairly new to programming, so any other tips would also be great. Thank you!

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

struct name{
    int tally;
    string inputName;
};

bool die( const string& message );

int main( void ) {
    ifstream infile;
    infile.open( "input.txt", ifstream::in );

    vector<name> nameStorage; // stores all names and tallies of them
    vector<name>::iterator it;
    string tempInput; // Stores the most recent input temporarily
    char temp;
    int counter = 0;
    while ( temp = infile.get(), infile.good() ) { // loop while extraction from file is possible
        if ( temp != ',' ) {
            tolower ( temp ); // makes everything lowercase
            tempInput.push_back(temp); }
        else {
            cout<<"good so far"<<endl;
            for ( it = nameStorage.begin(); it <= nameStorage.end(); it++ ) {
                cout<<"good so far 2"<<endl;
                if ( tempInput == it->inputName ) { 
                    cout<<"good so far 3"<<endl;
                    it->tally++;
                    break;
                }
                else if ( it == nameStorage.end() ) {
                    name tempStruct;
                    tempStruct.inputName = tempInput;
                    tempStruct.tally = 1;
                    nameStorage.push_back( tempStruct );
                }
            }
            tempInput.clear(); // clears the string after it has stored it
        }
        counter++;
        cout<<"Detected "<<counter<<" characters so far."<<endl;
    }
}

bool die( const string& message ) {
    cerr<<message;
    exit (EXIT_FAILURE);
}
  • 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-27T07:00:42+00:00Added an answer on May 27, 2026 at 7:00 am

    This code has at least 4 problems with it, not least of which is that using vectors for this kind of lookup/tally is extremely inefficient.

    1) std::vector::end() returns a special type of iterator that does not support boolean operators such as <= (but does support operator- or operator-=). Well, it supports them, but the behaviour is undefined.

    So

    for ( it = nameStorage.begin(); it <= nameStorage.end(); it++ )
    

    should be

    for ( it = nameStorage.begin(); it != nameStorage.end(); it++ )
    

    2) Now that your for statement is correct, this comparison will never return true

    else if ( it == nameStorage.end() ) {
    

    and so your new value will never be stored in the vector. To find tempInput in nameStorage you can either use std::find

    if (std::find(nameStorage.begin(), nameStorage.end(), tempInput) != nameStorage.end())
    {
     /// temp struct blah blah blah
     nameStorage.push_back(tempStruct);
    }
    

    3) Using boolean equivalence operator on strings is generally considered bad form i.e.

    if ( tempInput == it->inputName ) {
    

    should be

    if (!tempInput.compare(it->InputName))
    

    but you wont need to do that if you use std::find (above).

    4) std::getline supports delimiters, and you should be using it instead of reading in 1 char at a time see http://www.cplusplus.com/reference/string/getline/

    Last, you should really be doing your lookup/tally with a map. std:map is fine

    std::map<std::string, int> nameStorage;
    
    if (nameStorage.find(tempInput) != nameStorage.end())
    {
     nameStorage[tempInput]++;
    } else
    {
     nameStorage[tempInput] =1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a text area in my form which accepts all possible characters from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but

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.