i have a program that reads in a file with shapes and numbers and tokenizes each line. after it tokenizes, it then calculates the shapes dimensions. what i need to do is make it so that this program reads in the file and stores every shape and its values into an array first. then after it stores everything into an array it then does it's calculations and then last prints out the results.
changes i need to do:
- Use an array of generic pointers (for example, void* shapes[100];), and another array of integer codes to store up to 100 objects. I also will need a way to keep track of exactly how many valid objects are stored. Also, i need to use dynamic memory allocation to create struct instances, and store the memory address of each in the array of generic pointers.
- I need to write print functions for each shape (for example, void printSquare(Square* s)), which should print to the cout standard console output.
im having trouble storing the char* tokens into an array with its name and values. This part must be done first before it calculates anything. So if there are multiples of the same shapes, then that must be stored first.
any help or code would help a lot. appreciate it.
const int MAX_CHARS_PER_LINE = 50;
const int MAX_TOKENS_PER_LINE = 4;
const char* DELIMITER = " ";
struct Square
{
double length;
};
void squareCalc(double);
int main()
{
cout << endl;
cout << "Description: Calculates area, perimeter, surface area, and volume" << endl;
cout << "of 6 different geometric objects." << endl;
cout << endl;
ifstream fin;
fin.open("geo.txt");
if(!fin.good())
return 1;
char* token[MAX_TOKENS_PER_LINE] = {0};
Square shape;
while(!fin.eof())
{
char buf[MAX_CHARS_PER_LINE];
fin.getline(buf, MAX_CHARS_PER_LINE);
int n = 0;
token[0] = strtok(buf, DELIMITER);
if(token[0])
{
for(n = 1; n < MAX_TOKENS_PER_LINE; n++)
{
token[n] = strtok(0, DELIMITER);
if(!token[n]) break;
} //for
if((strcmp(token[0], "SQUARE") == 0))
{
if(n != 2)
cout << token[0] << " Invalid Object";
else
{
shape.length = atof(token[1]);
squareCalc(shape.length);
} //else
} //if
cout << endl;
} //while
cout << endl;
cout << "Press Enter to continue..." << endl;
cin.get();
return 0;
} //main
void squareCalc(double side)
{
double perimeter;
double area;
perimeter = side * 4;
area = side * side;
cout.setf(ios::fixed|ios::showpoint);
cout << setprecision(2);
cout << "SQUARE side=" << side << " perimeter=" << perimeter;
cout << " area=" << area;
} //squareCalc
input file:
SQUARE 14.5
RECTANGLE 14.5 4.65
CIRCLE 14.5
CUBE 13
PRISM 1 2 3
SPHERES 2.4
CYLINDER 50 1.23
TRIANGLE 1.2 3.2 3.4