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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:52:14+00:00 2026-06-17T10:52:14+00:00

I am building a simple program to ‘learn as I go’, this program takes

  • 0

I am building a simple program to ‘learn as I go’, this program takes a couple of text files and processors them, each line of the first text file is information about a person, and for each line an Object (of type Student (see below)) is created and added to a vector.
Within the Student objects is a map that stores that students marks.
I have a function within the student class that returns the map when called (or at least thats what im trying to do).
Currently this function is:

marksType Student::printMarks(){
    return marks;
}

(Where marksType = std::map<string, float>)
and marks is the map of type marksType.

Then in my main function I have:

Student a = *qw;
studmarks = a.printMarks();
for (std::map<string, float>::iterator iter = studmarks.begin(); iter != studmarks.end(); iter++){
    cout << "TEST" << endl;
}

Where qw is a pointer to a student object and studmarks is of type map<string, float>

The issue is that the cout doesn’t get called so the iterator seems to skip (but the student object does have items in the marks map).

Heres the complete Student class

#include "Student.h"
using namespace std;
typedef std::map<string, float> marksType;

Student::Student(const string &name, int regNo) : Person(name){
    marksType marks;
    this->regNo = regNo;
}

int Student::getRegNo() const{
    return regNo;
}
void Student::addMark(const string& module, float mark){
    pair<marksType::iterator,bool> check;
    check = marks.insert (pair<string,float>(module,mark));
    if (check.second==false){
        marks[module]=mark;
    }
}
float Student::getMark(const string &module) const throw (NoMarkException){
    if (marks.find(module) != marks.end()){
        return marks.find(module)->second;
    }
    else throw NoMarkException();
}
float Student::getAverageMark() const throw (NoMarkException){
    if (!marks.empty()){
        float avgmark = 0;
        for (marksType::const_iterator avgit=marks.begin(); avgit!=marks.end(); ++avgit){
            avgmark = avgmark + avgit->second;
        }
        avgmark = avgmark/marks.size();
        return avgmark;
    }
    else throw NoMarkException();
}
marksType Student::printMarks(){
    return marks;
}

Oh and below is the part of the main function that adds marks to the students,

for (vector<Student>::iterator it = students.begin(); it != students.end(); ++it){
    Student b = *it;
    if (regno == b.getRegNo()){
        found = true;
        b.addMark(module, mark);
    }
}

I know this works because when I use the getMark function it does work.

  • 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-17T10:52:16+00:00Added an answer on June 17, 2026 at 10:52 am

    You are “adding marks” to copies of the students stored in vector students. Each of these copies only lives during one iteration of the loop and the result is that you are not modifying the vector’s elements at all:

    for (vector<Student>::iterator it = students.begin(); it != students.end(); ++it){
        Student b = *it; // b IS A COPY
        if (regno == b.getRegNo()){
            found = true;
            b.addMark(module, mark); // modify local copy of Student
        }
    }
    

    To add them to the elements of the vector, use

    for (vector<Student>::iterator it = students.begin(); it != students.end(); ++it){
        if (regno == it->getRegNo()){
            found = true;
            it->addMark(module, mark);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building this simple program but i'm having some problems with it. I encapsulated
I am building a simple scheduling program for week long scheduling: Sunday - Saturday.
I am building a simple Point of Sale system for my project. This system
I'm building a word anagram program that uses a database which contains one simple
I am building a simple program on VS 11 Professional beta edition. #include <iostream>
I'm having trouble building a simple c++ program that tests out regex's from the
I am building the simple program where i have pre filled books with name
I'm building a simple C++ program and I want to temporarily substitute a system
I'm building a simple directions program in VB and would like to incorporate some
I am building a simple client-server program , I have in main : FTPClient

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.