Hello, tell me, please, an error. I can not find the problem.
There is a class lisdtADT , which contains maxSize , size and cursor . For approval, I initialize maxSize=11 ; `immediately, the other variables in the constructor.
The problem is that the DataType getCursor(); method DataType getCursor(); * typedef int DataType; returns invalid value (garbage).
After debugging, I realized that the newDataItem in the insert method is initialized with the correct values entered.
Further, in the source.cpp file source.cpp I try to calculate the sum of my entered values by the string total += samples.getCursor(); . And precisely getCursor(); returns me the garbage, which of course does not correctly count the amount.
I think that I go out in some place and add an extra one, and therefore such an error occurs.
Help, please, sort it out. Thank you.
My code is:
ListADT.h
#include<iostream> const int defMaxListSize = 10; typedef int DataType; class ListADT { private: //Data members int maxSize=11; int size; int cursor; DataType *dataItems=NULL; public: //Constructor ListADT(int maxNumber = defMaxListSize); //Destructor ~ListADT(); //List manipulation operations void insert(const DataType &newDataItem); //Insert after cursor void remove(); //Remove Data item //void replace(const DataType &newDataItem); //Replace data item //List status operations bool isEmpty()const; //List is empty bool isFull() const; //List is full //List iteration operations void gotoBeginning() ; //Go to beginning void gotoEnd(); //Go to end bool gotoNext(); //Go to next data item bool gotoPrior(); //Go to prior data item DataType getCursor(); //Return data item //Output the list structure -used in testinf/debugging //void showStructure()const; }; ListADT.cpp
#include "ListADT.h" ListADT::ListADT(int maxNumber):cursor(0),size(0) { if (maxNumber <= defMaxListSize) dataItems = new DataType[maxNumber]; else std::cout << "The list is overloaded" << std::endl; } ListADT::~ListADT() { delete dataItems; } void ListADT::insert(const DataType & newDataItem) { if (size + 1 <= maxSize) { dataItems[cursor] = newDataItem; size++; cursor++; } } void ListADT::remove() { if (size - 1 >= 0) cursor--; } bool ListADT::isEmpty() const { if (size <= 0) return true; else return false; } bool ListADT::isFull() const { if (size == defMaxListSize) return true; else return false; } void ListADT::gotoBeginning() { cursor = 0; } void ListADT::gotoEnd() { cursor = size; } bool ListADT::gotoNext() { if (cursor < size) { cursor++; return true; } return false; } bool ListADT::gotoPrior() { if (!isEmpty() && cursor != 1) { cursor--; return true; } else return false; } DataType ListADT::getCursor() { if (!isEmpty()) { //std::cout << "C: " << cursor << std::endl; //правильный результат //cursor--; return dataItems[cursor]; // возвращает мусор } else { std::cout << "Empty list" << std::endl; } } Source.cpp
#include <iostream> #include "ListADT.h" void main() { ListADT samples; int newSample=0; int total = 0; //Read in a set of samples from the keyboard. std::cout << "Enter list of samples (end with eof): "; for (int i = 0; i < 10; i++) { std::cin >> newSample; samples.insert(newSample); } //Sum the samples and output the result. if (!samples.isEmpty()) { samples.gotoBeginning(); do { std::cout << "CUR " << samples.getCursor() << std::endl; total += samples.getCursor(); } while (samples.gotoNext()); } std::cout << "Sum is " << total << std::endl; system("pause"); } Result:

Curare garbage. I do not understand, somewhere near ... the answer. - Nikita Gusev