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

Edited 5 Years Ago by rjcenteno: n/a

May I suggest viewing this thread? It seems to be a discussion that is relevant to your situation.

What it boils down to is creating an inheritance hierarchy that makes sense for shape analyses.

that thread would of helped if i knew how to do classes, but i haven't got that far yet.

Except for some behavioral differences that occur "by default", a struct and a class are the same thing. I won't elaborate on the specific differences here because I don't want to cause you additional confusion. In the example thread that I linked, you should be able to take the "class" keyword and replace it with the "struct" keyword, then remove the access modifiers (i.e. "private:", "protected:", and "public:") and have the basic idea.

Anyways, back OT:

Are you required to use arrays, or can you use vectors? Using a std::vector will make your life much easier because a vector is easy to change and re-size (dynamic) whereas arrays are not readily adjustable (static).

Edited 5 Years Ago by Fbody: n/a

Are you required to use arrays, or can you use vectors? Using a std::vector will make your life much easier because a vector is easy to change and re-size (dynamic) whereas arrays are not readily adjustable (static).

If he doesn't know classes it's a sure bet vectors are out of the question...

yeah i don't know classes, haven't got that far yet.

but all i really need help with is knowing how to store these tokens into an array and then manipulating them because i'm not having any luck with that and once i am able to store everything i should be able to do all the checks to see if the object is valid or not.

thanks for the help =)

This article has been dead for over six months. Start a new discussion instead.