I am unsure on what im doing wrong.

Im not sure on what I put inside my 3 files, Shapes.dat, Area.dat and Perimeter.dat. Am i only supposed to use 2 files? or 3 files.

Instructions:
The programming assignment for Chapter 8 is designed to challenge your
understanding of arrays, repetition, and selection. Your program should:
1. Introduce the program to the user.
2. Obtain 2 file names from the user. One contains the names of several
simple geometric shapes.
3. Read the shape name file into an array of shape names.
4. In the second file, each line contains two floating point numbers associated
with each shape (each line corresponds to the names in the other file): area
and perimeter. These numbers are to be read into a multidimensional
array.
5. Your program should create three different output files: Shapes.dat,
Areas.dat, and Perimeter.dat. The data in each of these output files should
be presented in a table of aligned columns. The first column is shape name,
the second is area and the third is perimeter.
a. Shapes.dat should contain the data sorted with shape names in
alphabetical order (the associated data should appear on the same
line as the shape name).
b. Areas.dat should contain the same data, but sorted from the smallest
area to the largest.
c. Perimeter.dat should contain the same data, but sorted from the
smallest perimeter to the largest.
Remember to arrange the data in the appropriate order.










#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
int main()
{

// Initializing Varibles
int a, b, c, d;

// Initializing Arrays
char shapes[10][100];
char temp[100];
char f1[20];
char f2[20];

// Column 0 = Areas and Column 1 = Perimeters
float data[10][2];
float x;
// Declaring input and output
ifstream input;
ofstream output;

// introducting the program to the user
cout << "This is a program which takes two file names as an input. " << endl;
cout << "One of the files store, names of some simple geographic shapes." << endl;
cout << "The other file contains corressponding areas and perimeters. Then 3 files are generated named Shapes.dat, f1.dat and f2.dat" << endl;
cout << "Enter two file names. " << endl;

// Saving the inputs from the user
cin >> f1 >> f2;

// opening the input file
input.open(f1);


// Declaring what my Varibles will be
a = 0;
b = 0;
c = 0;

// Reading shape names

while (!input.eof())
{
input.getline(shapes[a++], 100);
input.close();

// number of shapes
d = a;
a = 0;
input.open(f2);
}

// Reading Area and Perimeters from file

while (!input.eof())
{
input >> data[a][0];
input >> data[a++][1];
}

// Closing the input file
input.close();


//Bubble Sort technique
for (b = 0; b < d; b++)
for (a = 0; a < d - 1; a++)
if (strcmp(shapes[a], shapes[a + 1]) > 0) //Sorting the arrays in alphabetical order
{
//Swapping portion
strcpy_s(temp, shapes[a]);
strcpy_s(shapes[a], shapes[a + 1]);
strcpy_s(shapes[a + 1], temp);

x = data[a][0];
data[a][0] = data[a + 1][0];
data[a + 1][0] = x;

x = data[a][1];
data[a][1] = data[a + 1][1];
data[a + 1][1] = x;
}
output.open("Shapes.dat");
for (b = 0; b < d; b++)
output << shapes[a] << "\t\t" << data[a][0] << "\t\t" << data[a][1] << endl; //Storing the data in file "Shapes.dat
output.close();

for (b = 0; b < d; b++) //Bubble Sort technique
for (a = 0; a < d - 1; a++)
if (data[a][0] > data[a + 1][0]) //Sorting the arrays with respect to area in ascending order
{
//Swapping portion
strcpy_s(temp, shapes[a]);
strcpy_s(shapes[a], shapes[a + 1]);
strcpy_s(shapes[a + 1], temp);

x = data[a][0];
data[a][0] = data[a + 1][0];
data[a + 1][0] = x;

x = data[a][1];
data[a][1] = data[a + 1][1];
data[a + 1][1] = x;
}
output.open("f1.dat");
for (b = 0; b < d; b++)
output << shapes[b] << "\t\t" << data[b][0] << "\t\t" << data[b][1] << endl; //Storing the data in "f1.dat" file
output.close();

for (b = 0; b < d; b++) //Bubble Sort technique
for (a = 0; a < d - 1; a++)
if (data[a][1] > data[a + 1][1]) //Sorting the arrays with respect to perimeter in ascending order
{
//Swapping portion
strcpy_s(temp, shapes[a]);
strcpy_s(shapes[a], shapes[a + 1]);
strcpy_s(shapes[a + 1], temp);

x = data[a][0];
data[a][0] = data[a + 1][0];
data[a + 1][0] = x;

x = data[a][1];
data[a][1] = data[a + 1][1];
data[a + 1][1] = x;
}
output.open("f2.dat");
for (b = 0; b < d; b++)
output << shapes[b] << "\t\t" << data[b][0] << "\t\t" << data[b][1] << endl; //Storing the data in "f2.dat" file
output.close();










return 0;

}
Last edited on
Topic archived. No new replies allowed.