i'm stuck on a program that i am working on in class. your suppose to open a text file with names of shapes and certain lengths of it's sides. it then calculates what it's area/perimeter/volume/circumference and so on and so forth for each shape named in the text file.
i tokenized the input file and was able to print on screen. now i am stuck trying to figure out how to manipulate the tokens. i am not sure how i can manipulate the tokens, maybe array?
here is what i have so far:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cmath>
#include <cstring>
using std::string;
using std::strtok;
using std::strncmp;
#include <cstdlib>
const int MAX_CHARS_PER_LINE = 50;
const int MAX_TOKENS_PER_LINE = 4;
const char* DELIMITER = " ";
int main()
{
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};
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
for(int i = 0; i < n; i++)
{
cout << "Token[" << i << "] = " << token[i] << endl;
}
cout << endl;
} //while
cout << endl;
cout << "Press Enter to continue..." << endl;
cin.get();
return 0;
} //main
here is the input file:
SQUARE 14.5
RECTANGLE 14.5 4.65
CIRCLE 14.5
CUBE 13
PRISM 1 2 3
SPHERES 2.4
CYLINDER 1.23
CYLINDER 50 1.23
TRIANGLE 1.2 3.2
the output should look something like this:
SQUARE side=14.5 area=210.25 perimeter=58.00
RECTANGLE length=14.5 width=4.65 area=67.43 perimeter=38.30
CIRCLE radius=14.5 area=660.52 circumference=91.11
CUBE side=13 surface area=1014.00 volume=2197.00
PRISM length=1 width=2 height=3 surface area=22.00 volume=6.00
SPHERES invalid object
CYLINDER radius=1.23 height=0 surface area=9.51 volume=0.00
CYLINDER radius=50 height=1.23 surface area=16094.37 volume=9660.39
TRIANGLE invalid object
and for any objects with no sides it gives invalid object.
can anyone point me in the right direction.