Skip to content

Instantly share code, notes, and snippets.

@rayzing
Created October 23, 2018 04:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayzing/2b892433e90876b25fed1e2325d74842 to your computer and use it in GitHub Desktop.
Save rayzing/2b892433e90876b25fed1e2325d74842 to your computer and use it in GitHub Desktop.
Creating a 2D array which outputs values of an array from a user-generated list of coordinates
J t //these are the data values in the 2D array that I pull from based on the coordinates. If it's 8 0 for example I take from row 8
A ' // and column 0 which would mean its corresponding value is V.
} -
F -
r /
1 j
[ y
7 O
V 7
] <
t A
= Y
! \
load fulldata.txt //makes program load this file with the data.
location -10 -0
location 2 0 //the user puts this in. First numeric value as an x coord (row) and next as a y coord (column)
location 3 0
location 8 0
location 2 1
location 1 0
location 25 -0
location 1 1
location -2 4
location -11 4
location 22 -0
location 17 -1
location -12 -0
int main ()
/*Function: main
Purpose: To call the read2D_Data function to find valid coordinates
and output whatever the loaded file has into those coordinates of the array.
Input: input.txt
Output: output.txt
Commands: Calling function read2D_data.
Could always just make a main.cpp
*/
{
read2D_Data( "input.txt", "output.txt" ); //Start it.
cout << "Done." << endl;
return 0;
}
/**************************************************************
Definition of function read2D_Data: *
To read the commands file and then call a function for output.*
**************************************************************/
void read2D_Data( string input, string output )
{
char toomanyDims[ROWS][COLS];
for (int count = 0; count < ROWS; count++)
{
for (int count2 = 0; count2 < COLS; count2++)
{
toomanyDims[count][count2] = ' ';
}
}
string command, command2, liveData;
int x, y, num; //num is there as a test, may or may not use.
char letter;
ifstream userTask; //input file
userTask.open(input);
ofstream coords; //output file
coords.open(output);
if (userTask.fail())
{
cout << "Error opening userTask file.\n";
}
else
{
cout << "Now reading data and taking in commands." << endl; // Process the file. Courtesty selection statement of Tony Gaddis.
}
reading_jibbs(userTask, command, liveData, x, y, command2, coords, toomanyDims, ROWS);
/* for (int count = 0; count < ROWS; count++)
{
for (int count2 = 0; count2 < COLS; count2++)
{
cout << toomanyDims[count][count2] << " " << endl;
}
} */
userTask.close();
coords.close();
}
/*****************************************
Definition of function reading_jibbs: *
To read the commands file and apply the *
appropiate actions to the right commands.*
*****************************************/
void reading_jibbs(ifstream& userTask, string& command, string& liveData, int& x, int& y, string& command2, ofstream& coords, char toomanyDims[][COLS], int rows) //Read load command and load the variable.
{
getline(userTask, command, ' ');
getline(userTask, liveData);
cout << liveData << endl;
if (command == "load")
{
ifstream data; //input file to open if the command said so.
data.open(liveData);
if (data.fail())
{
cout << "Something went wrong." << endl;
}
else
{
cout << "It seems to have worked!!!" << endl;
}
reading_jibbschild(data, toomanyDims, rows);
getline(userTask, command, ' ');
userTask >> x;
userTask.ignore();
userTask >> y;
cout << x << " " << y << " " << command << endl;
while(!userTask.fail()) //this input fails for some reason after the fifth entry.
{
finpos( coords, x, y, data, toomanyDims, rows);
getline(userTask, command, ' ');
userTask >> x;
userTask.ignore();
userTask >> y;
}
}
}
/*************************************************
Definition of function finpos: *
To output the coords and value in correspondence.*
*************************************************/
void finpos( ofstream& coords, int& x, int& y, ifstream& data, const char toomanyDims[][COLS], int rows)
{
if (x >= 0 && x < 13 && y >= 0 && y < 2)
{
coords << x << ", " << y << ": " << toomanyDims[x][y] << endl;
}
else if (x < 0 || x >= 13 || y < 0 || y >= 2)
{
coords << x << ", " << y << ": " << "bad location" << endl;
}
}
/******************************************
Definition of function reading_jibbschild:*
To read the values of the coords *
and fill up the array. *
******************************************/
void reading_jibbschild(ifstream& data, char toomanyDims[][COLS], int rows)
{
/*if (x >= 0 && x < 13 && y >= 0 && y < 2) //checking if we have valid coordinates
{*/
cout << "Things look great." << endl;
for (int count = 0; count < ROWS; count++)
{
for (int count2 = 0; count2 < COLS; count2++)
{
data >> toomanyDims[count][count2];
}
}
}
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using std::ofstream;
using std::ifstream;
using std::string;
using std::cout;
using std::endl;
const int ROWS = 13;
const int COLS = 2;
void read2D_Data( string input, string output );
void reading_jibbschild(ifstream& data, char toomanyDims[][COLS], int rows);
void reading_jibbs(ifstream& userTask, string& command, string& liveData, int& x, int& y, string& command2, ofstream& coords, char toomanyDims[][COLS], int rows);
void finpos( ofstream& coords, int& x, int& y, ifstream& data, const char toomanyDims[][COLS], int rows);
-10, 0: bad location
2, 0: }
3, 0: F
8, 0: V
2, 1: -
//the problem is that my output file (this) only outputs five values no matter what. I have good reason to believe that the input file fails for some reason.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment