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:

enter image description here

  • In insert, you make size ++ and cursor ++, although there is nothing by value in the cursor after the increase. And accordingly, the check in gotoNext will be successful, although there is nothing in the array for this value of the cursor. - Daniel Protopopov
  • @DanielProtopopov Yes, I understand you, here it is an extra ++, but I just don’t understand how I can fix it. - Nikita Gusev
  • Remove cursor ++ from there, this is done in getNext - Daniel Protopopov
  • @DanielProtopopov Then the values ​​even in Cur are garbage. I do not understand, somewhere near ... the answer. - Nikita Gusev
  • An array of indices 0-9, in getCursor cursor 9, size 10 now you will be accepted and ++ will be done. Check for <size - 1 - Daniel Protopopov

1 answer 1

An error was found in the bool ListADT::gotoNext() method. There was an extra step by one, and therefore the tenth element returned the garbage.

  1. The insert() method remains unchanged:

      void ListADT::insert(const DataType & newDataItem) { if (size + 1 <= maxSize) { dataItems[cursor] = newDataItem; size++; cursor++; } } 
  2. Method bool ListADT::gotoNext() . Change the size condition to size-1

      bool ListADT::gotoNext() { if (cursor < size-1) { cursor++; return true; } return false; } 
  • one
    You do not need to do cursor ++ in insert, it is enough to have dataItems [size] = newDataItem; size ++. Otherwise, if there is an insertion in the middle of using the cursor, it will be bo-bo - Daniel Protopopov