i am writing a program to open 2 files that the user inputs, One will have names of shapes and the other will have the perimeter and area for the corresponding shapes. Since Im writing the code i have to make it so the arrays lengths are specific to the information in the files. This is where i find a problem. Ive tried to jank a solution for the problem but im running into issues. I feel like there is a pretty simple way to fix this problem. My code right now will only prompt the user to have one input file and to input the name of said file. The file will have shapes in it .
)// David Wood
// Date
//Sort
//This program will sort user files alphabetically and numerically
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
void Introduction(); // prototype for introduction
void Swap();// Prototype for Swap function
int main() {
Introduction (); // introduction to the program
int count = 0;
string shapes; //defeniton for shapes
string filename1; // definiton for filename1 / Name of shapes
string filename2; // definition of Filename 2 / Perimeter and area of shapes
ifstream Input1; //Input 1 / Shape names
ifstream Input2; // Input 2 / Perimeter and Area of shapes
cout << "Please enter the two files you wish to use in this program (First imput the File that contains Shapes)" << endl;
cin >> filename1;
Input1.open(filename1.c_str());
while (Input1.fail()) { //If the file is not found prompts user to input again
Input1.clear(); // clears Input 1
cout << "No file name found, Please enter again ";
cin >> filename1; // Asks user to input the file name again
Input1.open(filename1.c_str()); // Opens the file
}
//cin >> filename2;
//Input2.open(filename1.c_str());
/*while (Input2.fail()) { //If the file is not found prompts user to input again
Input2.clear(); // clears Input 1
cout << "No file name found, Please enter again ";
cin >> filename2; // Asks user to input the file name again
Input1.open(filename2.c_str()); // Opens the file
}
*/
string ShapeNames[20]; // Array of strings. Information from input 1 gets written to array
// Copies File information into ShapeNames
//while (!Input1.eof()) {
// Input1 >> shapes;
// count++;
for (int i = 0; i < 5; i++) { // for loop to gather information from input 1
Input1 >> ShapeNames[i];
cout << ShapeNames[i] << endl; //Debugging purposes
}
// }
//double Area_Perimeter[20][2]; // Array of doubels. Information from input 2 gets written into array
// Input2.open(filename2.c_str());
//for (int i = 0);
system("pause");
return 0;
}
//David wood
// November 6,2018
//Introduction
// The introduction to the code
void Introduction() {
cout << "This code will ask you to chose 2 file names that you provide which contain two different types of information, Types of shapes," << endl
<< " and their area and perimeter. The code will take that information and sort it alphabetically, and numerically smallest to largest" << endl;
}
#include <iostream>
#include <string>
#include <fstream>
#include <cstdio>
usingnamespace std;
constint MAX_ROWS = 20;
constint MAX_COLS = 2;
int read_names(istream& is, string names[]);
int read_data(istream& is, double data[][MAX_COLS]);
void Introduction();
int main()
{
string names[MAX_ROWS];
double data[MAX_ROWS][MAX_COLS] = {{0.0}};
ifstream shapes("your filename");
ifstream data_src("your filename");
if (!shapes || !data_src)
{
perror(nullptr);
return 1;
}
int num_names = read_names(shapes, names);
cout << "Read " << num_names << " shape names.\n";
int num_data = read_data(data_src, data);
cout << "Read " << num_data << " perimeters and areas.\n";
// now do the rest
}
// reads the shape names from is into names and returns the number of lines read
int read_names(istream& is, string names[])
{
// TODO - implement me
}
// reads the data from is into names and returns the number of lines read
int read_data(istream& is, double data[][MAX_COLS])
{
// TODO - implement me
}
//David wood
// November 6,2018
//Introduction
// The introduction to the code
void Introduction()
{
cout << "This code will ask you to chose 2 file names that you provide which contain two
different types of information, Types of shapes," << endl
<< " and their area and perimeter.
The code will take that information and sort it alphabetically,
and numerically smallest to largest" << endl;
}
For this program we’re not allowed to use structures because it’s not in the chapter we just learned. I understand that it would be easier but that isn’t an option sadly. How would I go about doing it with arrays only. Also the code is supposed to read from the files and then sort them in alphabetical order and numerically smallest to largest and then output those into 3 different files. I forgot to mention that in the first post. I don’t think I’ll have a problem sorting the arrays. The only problem would be the file inputs and stuff
There are no structures in my example, just arrays.
I was talking about a structured approach - Structured programming is actually a technique and has nothing to do with structures. Sorry for the confusion.
Okay well, I figured that Im not going to go with the approach above, im stilling trying to wrap my head around this whole thing myself and i feel like if i do it myself ill figure more things out and how to use things properly. so right now i added a few things. I got the first file to open and copy itself into the first array for the shapes and count how many words are in the array, but im having problems with the second array and it looping through the file. my input 2 file has 4 areas and 4 perimeters in the same format that a multidimensional array would have 4 rows and 2 columns. I don't know what is causing the problem but i cant seem to figure out how to copy those into the array and loop it so it will count the number of doubles in the file.
my code currently looks like:
// David Wood
// Date
//Sort
//This program will sort user files alphabetically and numerically
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
void Introduction(); // prototype for introduction
void Swap();// Prototype for Swap function
int main() {
Introduction(); // introduction to the program
int count = 0;
string shapes; //defeniton for shapes
string filename1; // definiton for filename1 / Name of shapes
string filename2; // definition of Filename 2 / Perimeter and area of shapes
ifstream Input1; //Input 1 / Shape names
ifstream Input2; // Input 2 / Perimeter and Area of shapes
string temp; // Temporary place holder
double x; // Place holder for doubles
cout << "Please enter the two files you wish to use in this program (First imput the File that contains Shapes)" << endl;
cin >> filename1;
Input1.open(filename1.c_str());
while (Input1.fail()) { //If the file is not found prompts user to input again
Input1.clear(); // clears Input 1
cout << "No file name found, Please enter again " << endl;
cin >> filename1; // Asks user to input the file name again
Input1.open(filename1.c_str()); // Opens the file
}
cin >> filename2;
//Input2.open(filename1.c_str());
while (Input2.fail()) { //If the file is not found prompts user to input again
Input2.clear(); // clears Input 1
cout << "No file name found, Please enter again ";
cin >> filename2; // Asks user to input the file name again
Input1.open(filename2.c_str()); // Opens the file
}
// Copies File information into ShapeNames
string ShapeNames[20]; // Array of strings. Information from input 1 gets written to array
while (!Input1.eof()) {
Input1 >> temp; // Input into temp placeholder
ShapeNames[count] = temp; // Array = temp place holder
if (!Input1.good()) { // tests to see if input.eof works correctly. If not ends code
break;
}
cout << ShapeNames[count] << endl; // Debugging purposess
count++;
//cout << count<<endl; // debug
}
count = 0; //sets count back to zero to be used again in other loops
// cout << count; // for debugging
// Copy File information From Input2 into Area_Perimeter
double Area_Perimeter[20][2]; // Array of doubles. Information from input 2 gets written into array
while (!Input2.eof()) {
Input2 >> x;
Area_Perimeter[count][0]= x;
Area_Perimeter[count][1] = x;
count++;
cout << count << endl;
}
system("pause");
return 0;
}
//David wood
// November 6,2018
//Introduction
// The introduction to the code
void Introduction() {
cout << "This code will ask you to chose 2 file names that you provide which contain two different types of information, Types of shapes," << endl
<< " and their area and perimeter. The code will take that information and sort it alphabetically, and numerically smallest to largest" << endl;
}
// David Wood
// Date
//Sort
//This program will sort user files alphabetically and numerically
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
constint MAX_ROWS = 20;
//!! Cut down on the copy and paste.
//!! When you copy/paste a block of code, make it a function instead.
//!! The tweaks you make to the 2nd copy become the parameters to the function.
void openFile ( string prompt, ifstream &infile ) {
cout << prompt << endl;
string filename;
cin >> filename;
infile.open(filename.c_str());
while ( infile.fail() ) {
infile.clear();
cout << "No file name found, Please enter again " << endl;
cin >> filename; // Asks user to input the file name again
infile.open(filename.c_str()); // Opens the file
}
}
void Introduction(); // prototype for introduction
void Swap();// Prototype for Swap function
int main() {
Introduction();
//!! Better variable names need no comment telling you what they are
ifstream shapeNameFile;
ifstream perimeterAreaFile;
//!! Now opening both files is 2 lines in main, not 20.
openFile("Shape file",shapeNameFile);
openFile("Perimeter and area file",perimeterAreaFile);
//!! This should be a function, even though you call it once
//!! it just keeps things tidy
string temp;
string ShapeNames[MAX_ROWS]; //!! consider using std::vector here
int count = 0;
while ( count < MAX_ROWS && shapeNameFile >> temp ) {
ShapeNames[count] = temp;
count++;
}
//!! This should be a function, even though you call it once
//!! it just keeps things tidy
double Area_Perimeter[MAX_ROWS][2];
double x,y;
count = 0;
while ( count < MAX_ROWS && perimeterAreaFile >> x >> y ) {
Area_Perimeter[count][0] = x;
Area_Perimeter[count][1] = y;
count++;
}
}
1 2 3
Input1 >> temp; // Input into temp placeholder
ShapeNames[count] = temp; // Array = temp place holder
if (!Input1.good()) { // tests to see if input.eof works correctly. If not ends code
It was a valiant attempt, but you need to test good() immediately after the stream operation.
But the whole testing eof/good just makes the code cumbersome to begin with, when the stream extractor returns a status anyway, as shown above.
Okay so i changed my code up a bit and made it neater like you said and made certain things more simple. Now i need to sort the program and have it output into 3 different files. I feel like i kind of have an idea of how to do this but i still have a couple problems. So the while loops counted the amount of doubles and amount of strings in each file, so how would i take that information and use it to sort the two arrays in alphabetical and numerical order while keeping the two arrays parallel?