-4

So here is my program with the error:

// ConsoleApplication42.cpp : Defines the entry point for the console application.
//

    #include "stdafx.h"
    #include <iostream>
    #include <list>
    #include <cstdlib>
    #include <time.h>
    #include <algorithm>
    #include <string>
    #include <vector>

    using namespace std;


    const int numberOfStudents = 9;
    string names[numberOfStudents] = {"Abe","Billy","Carl","Dillan","Eddie","Felix","Gill","Herald","Isaac"};

    struct StudentInfo {
        string name;
        int grade;



        bool operator< (int grade){
            return grade < grade;
        }

        bool operator< (string name){
            return name < name;
        }
        };



    void populateStudentRecords(vector<StudentInfo>Students,vector<int>::iterator iter, int x){

        for(auto iter = Students.begin(); iter != Students.end(); ++iter){
            iter->name = names[x];
            iter->name.push_back(x);
            iter->grade = x++;
            x = x++;

        }



    }

    bool sortByName(const StudentInfo x, const StudentInfo y){
        return x.name < y.name;
    }

    bool sortByGrade(const StudentInfo x, const StudentInfo y){
        return x.grade < y.grade;
    }

    void displayRecords(vector<StudentInfo>Records,vector<int>::iterator iter){
        for(auto iter = Records.begin(); iter != Records.end(); ++iter){
            cout<<*iter->name<<" -"<<*iter->grade<<endl;
        }
    }

    void displayMaxAndMinGrade(vector<StudentInfo>Records,vector<int>::iterator iter){
        for(auto iter = Records.begin(); iter != Records.end(); ++iter){
            cout<<*iter->name<<" - " <<*iter->grade<<endl;
            iter = Records.end();
            cout<<*iter->name<<" - " <<*iter->grade<<endl;
        }
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
        vector<StudentInfo>Records (numberOfStudents);
        vector<int>::iterator iter;


        populateStudentRecords(Records,iter,0);
        sort(Records.begin(),Records.end(),sortByName);
        displayRecords(Records,iter);
        sort(Records.begin(),Records.end(),sortByGrade);
        cout<<" "<<endl;
        displayMaxAndMinGrade(Records, iter);

        return 0;
    }

In the displayRecords function and the displayMaxAndMin function, I have the * symbol next to the iterator. I want the computer to display the value of these variables within each occurrence of the structure in the vector. However, I get an error c2100 when I try to build the program. I have tried to run the program without including the * symbol, but that displays the address of each variable and also leads to a crash. How do I fix this? Thank you.

  • According to https://msdn.microsoft.com/en-us/library/bzf3eha6.aspx, c2100 means "Indirection operator ( * ) is applied to a nonpointer value." Pretty good hint there that you didn't want the *. – user4581301 Sep 14 '15 at 04:43

1 Answers1

1

You need to read an introductory book on programming with C++. This code is full of errors. You don't know how to use pointers/references/iterators.

  1. Learn about copying, references, and pointers from a good book. Follow the link at the end of this answer.
  2. Variables should be limited to the scope in which they are used. Stop passing so many arguments to your function when they don't really need them.
  3. You are using both the indirection and dereference operators at the same time when you only need one for the type you are applying the operators to. This is the reason for your error.
  4. You are incrementing an array index twice per loop, which makes it go out of bounds.
  5. You are using your container's end() member function with the assumption that it returns an iterator to the last element in the container.
  6. You want to print the lowest and highest grades but loop through the whole container.

For your sake, I've fixed the most glaring errors:

#include <iostream>
#include <list>
#include <cstdlib>
#include <time.h>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

const int numberOfStudents = 9;
string names[numberOfStudents] = { "Billy", "Abe","Carl","Dillan","Eddie","Felix","Gill","Herald","Isaac" };

struct StudentInfo
{
    string name;
    int grade;
};

void populateStudentRecords(vector<StudentInfo>& Students)
{
    int x{ 0 };
    for (auto iter = Students.begin(); iter != Students.end(); ++iter)
    {
        iter->name = names[x];
        iter->name.push_back(x);
        iter->grade = ++x;
    }
}

bool sortByName(const StudentInfo x, const StudentInfo y)
{
    return x.name < y.name;
}

bool sortByGrade(const StudentInfo x, const StudentInfo y)
{
    return x.grade < y.grade;
}

void displayRecords(vector<StudentInfo>Records)
{
    for (auto iter = Records.begin(); iter != Records.end(); ++iter)
    {
        cout << iter->name << " -" << iter->grade << endl;
    }
}

void displayMaxAndMinGrade(vector<StudentInfo>Records)
{
    auto iter = Records.begin();
    cout << iter->name << " - " << iter->grade << endl;
    iter = Records.end() - 1;
    cout << iter->name << " - " << iter->grade << endl;
}

int main()
{
    vector<StudentInfo>Records(numberOfStudents);

    populateStudentRecords(Records);
    sort(Records.begin(), Records.end(), sortByName);
    displayRecords(Records);
    sort(Records.begin(), Records.end(), sortByGrade);
    cout << " " << endl;
    displayMaxAndMinGrade(Records);

    return 0;
}

Please do yourself a favor and visit the following link:

The Definitive C++ Book Guide and List

Community
  • 1
  • 1
bku_drytt
  • 3,169
  • 17
  • 19