I am working on a project I have for a programs and algorithms class. I have posted the Project first and my coding below that. My first coding started as a single main.cpp file, Now I am trying to make it object-oriented but am getting errors. I specify more with my coding below.
PROJECT DIRECTIONS
--------------------------------------------------------------------------------
For this project you will design and implement a program that receives data and commands read in from a text file. The data will be positive integers, which are to be stored in an array. The commands will include operations such as adding new data, removing data, accessing data, and printing the contents of the array.
The array must be a data member of a class that you design and implement to provide all of the following functionality:
* data members: capacity of the array, number of elements currently in the array, a dynamic array of integers, a flag that allows an instance to be set to fixed size or allows for auto-resizing (auto-resized arrays double in capacity on resizing)
* default capacity of 5 elements, fixed size
* the ability to create an instance with a specified array capacity, defaults to fixed size
* the ability to create an instance with a specified array capacity, specified fixed size/auto-resize
* a function to add new data (data will always be added to the next available "slot" at the end of the array; add data fails if the array is already filled to capacity (fixed size) or causes the array to grow in size (auto-resizing)
* a function to insert data at a specified index; overwrites any data already present at the index, returns -1 if the index value is invalid
* a function to retrieve data at a specified index; returns false if the index value is invalid, otherwise it should set a by-reference parameter to the data at the index and return true
* a function to "remove" data at a specified index; removal means shifting all values at higher indices in the array down one to fill the "slot" from where the value was removed; returns -1 if the index value is invalid (invalid index here means the index was outside the bounds of the array OR the index value refers to a "slot" that is not being used
* a function to retrieve the capacity of the array
* a function to retrieve the number of elements in the array
* a function to print the capacity, number of elements, and all of the values in the array; this function must NOT use cout -- instead, this function will create a string with the desired output information and return the string to the caller
* do NOT do any outputs, except for your own debugging work, in your array class
The input text file will contain the following information: array setup (capacity, fixed size or auto-resize), commands (with data as appropriate). The commands are as follows:
* i VALUE INDEX
('i' is the insert command, VALUE is the value being inserted, INDEX is the index in the array); on success, your program should display "VALUE inserted at index INDEX"; on failure, your program should display "INDEX is an invalid index"
* g INDEX
('g' is the get command, INDEX is the index in the array from where the data should be gotten); on success, your program should display "value at INDEX: VALUE"; on failure, your program should display "INDEX is an invalid index"
* a VALUE
('a' is the add command, VALUE is the value to be added at the next available "slot" at the end of the array); on success (fixed size or auto-resizing), your program should display "VALUE added"; on failure (fixed size only), your program should display "array is filled to capacity"; if the array must grow to accommodate the new value (auto-resizing only), your program should display "array resized to new capacity CAPACITY" where CAPACITY is the new capacity
* r INDEX
('r' is the remove command, INDEX is the index in the array from where the data should be removed); on success, your program should display "value at INDEX removed"; on failure, your program should display "INDEX is an invalid index"
* c
('c' is the capacity command); your program should display "array capacity is CAPACITY" where CAPACITY is the capacity of the array
* n
('n' is the number of elements command); your program should display "array has NUMBER elements" where NUMBER is the number of elements in the array
* p
('p' is the print command); display the number of elements and the capacity as "NUM_ELEMENTS/CAPACITY", and display the data in the array in a comma-separate list, at most 10 values per line
The format of the text file will be the following:
* line #1: array capacity (an int value)
* line #2: fixed size (0) or auto-resize (1)
* lines #3 and on: commands (see sample input file here)
* last line: -1
The input file will be specified via the argv array. If no input file is specified, your program should default to the file lab3input.txt.
--------------------------------------------------------------------------------
ALL OF MY CODING IS BELOW:
**main.cpp:**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include "CBArray.h"
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
int capacity = 5;
bool resize = true;
int* numbers;
int numberSize = 0;
void processes(string, int);
int main(int argc, char** argv)
{
string filename = "lab3input.txt";
if(argc == 2)
filename = argv[1];
cout << filename << endl;
ifstream fin(filename.c_str());
if(!fin.fail())
{
int count = 1;
string nextLine = "";
char nextChar = fin.get();
while(nextChar!= EOF)
{
if(nextChar == 'n')
{
processes(nextLine, count);
nextLine = "";
count ++;
}
else
{
nextLine+=nextChar;
}
nextChar = fin.get();
}
}
return 0;
}
|
**CBArray.cpp**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include "CBArray.h"
#include <fstream>
#include <iostream>
#include <sstream>
void processes(string nextLine, int count)
{
if(count == 1)
{
stringstream ss(nextLine);
ss>>capacity;
cout << capacity << endl;
}
else if(count == 2)
{
stringstream ss(nextLine);
ss>>resize;
numbers = new int[capacity];
}
else if(nextLine[0] == 'p')
cout << numberSize << '/' << capacity << endl;
else if(nextLine[0] == 'n')
cout << "array has "<< numberSize << " elements" << endl;
else if(nextLine[0] == 'c')
cout << "array capacity is "<< capacity << " elements" << endl;
else if(nextLine[0] == 'a')
{
if(numberSize<capacity)
{
stringstream ss(nextLine.substr(2));
int value = 0;
ss>>value;
numbers[numberSize] = value;
numberSize++;
cout << value << " added" << endl;
}
else
cout << "array resized to new capacity " << capacity << endl;
}
else if(nextLine[0] == 'i')
{
stringstream ss(nextLine.substr(2));
int value = 0;
int index = 0;
ss>>value;
ss>>index;
numbers[index] = value;
cout << value << " inserted at index " << index << endl;
}
else if(nextLine[0] == 'g')
{
stringstream ss(nextLine.substr(2));
int index = 0;
ss>>index;
cout << "value at " << index << ": " << numbers[index] << endl;
}
/*
else if(nextLine[0] == 'r')
{
stringstream ss(nextLine.substr(2));
int index = 2;
ss>>index;
for (int i=index+1; i<count; i++)
numbers[i-1] = numbers[i];
count--;
}
*/
else if(nextLine[0] == '-')
{
cout << "-1" << endl;
}
}
|
Problem 1: lines 62 through 72 above are commented out because the program would crash and I cannot figure out why.
Problem 2: Line 6 gives the errors
C:UsersCRBottiniDesktopProjectThreeCBParser.cpp|6|error: variable or field 'processes' declared void|
C:UsersCRBottiniDesktopProjectThreeCBParser.cpp|6|error: 'string' was not declared in this scope|
C:UsersCRBottiniDesktopProjectThreeCBParser.cpp|6|error: expected primary-expression before 'int'|
||=== Build finished: 3 errors, 0 warnings ===|
CBArray.h:
1 2 3 4 5 6 7 8 9 10 11
|
class CBArray
{
public:
int capacity;
bool resize;
int* numbers;
int numberSize;
private:
};
|
Any insight to my problems would be greatly appreciated. Look forward to hearing your responses