Dec 15, 2015 at 12:36am Dec 15, 2015 at 12:36am UTC
I'm trying to create a program for class that will read data from a txt file and translate it into a 4x6 array, reading it line by line. I'm supposed to minimize global constants and use functions.
But I'm running into trouble. I've created a function with the idea/hope that it will load the array values for later use and manipulation. But I can't get to the point where it will compile. I need some pointer objects but I've gotten pretty lost.
I was trying to structure this program off one I did before that used a 1D array and functions. Clearly it doesn't translate completely and I haven't figured out how to code differently for it. I'm still pretty new at this. Any guidance would be much appreciated!
Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int getARRAY(const int [][], int );
//void goal1
//void goal2
//void goal3(const int[][COLUMNS], int);
//void goal4(const int[][COLUMNS], int);
int main()
{
const int COLUMNS = 6;
const int ROWS = 4;
int ARRAY[ROWS][COLUMNS];
cout << "The contents of the array are:\n" ;
getARRAY;
//goal1;
//goal2;
//goal3;
//goal4;
system("Pause" );
return 0;
}
int getARRAY(int [][], int ARRAY)
{
fstream inputFile;
inputFile.open("C:\\ExternalFiles\\exfile2DARRAY.txt" );
if (inputFile.fail())
{
cerr << "\a\a**Error opening input file.\n" ;
exit(EXIT_FAILURE);
}
int count = 0;
while (count < ARRAY[][] && inputFile >> ARRAY[count])
{
count++;
}
inputFile.close();
return ARRAY[4][6];
}
Last edited on Dec 15, 2015 at 10:14pm Dec 15, 2015 at 10:14pm UTC
Dec 15, 2015 at 4:08am Dec 15, 2015 at 4:08am UTC
The syntax is wrong, but before we get into fixing it, can you show what the content of the text file so we can better understand what you're trying to do?
Dec 15, 2015 at 9:27am Dec 15, 2015 at 9:27am UTC
You still have quite a few syntax errors:
1>main.cpp(21): error C2144: syntax error : 'int' should be preceded by ')'
1>main.cpp(21): error C2660: 'getARRAY' : function does not take 0 arguments
1>main.cpp(21): error C2059: syntax error : ')'
1>main.cpp(41): error C2059: syntax error : ']'
1>main.cpp(42): error C2143: syntax error : missing ';' before '{'
1>main.cpp(47): error C2059: syntax error : ']'
Here is a little example how to read the data and show it on the screen.
Maybe you can adopt ypur program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int COLUMNS = 6;
const int ROWS = 4;
void getArray(int numbers[ROWS][COLUMNS]);
void showArray(int numbers[ROWS][COLUMNS]);
int main()
{
int numbers[ROWS][COLUMNS];
cout << "The contents of the array are:\n" ;
cout << "==============================\n\n" ;
getArray(numbers);
showArray(numbers);
cout << "\n==============================\n\n" ;
system("Pause" );
return 0;
}
void getArray(int numbers[ROWS][COLUMNS])
{
fstream inputFile;
ofstream outputFile;
inputFile.open("Numbers.txt" );
if (!inputFile)
{
cerr << "\a\a**Error opening input file.\n" ;
exit(EXIT_FAILURE);
}
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLUMNS; col++)
{
inputFile >> numbers[row][col];
}
}
}
void showArray(int numbers[ROWS][COLUMNS])
{
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLUMNS; col++)
{
cout << numbers[row][col] << '\t' ;
}
cout << endl;
}
}
Last edited on Dec 15, 2015 at 9:45am Dec 15, 2015 at 9:45am UTC
Dec 15, 2015 at 9:43pm Dec 15, 2015 at 9:43pm UTC
Ah. I see. Thank you for your guidance!!
Dec 15, 2015 at 10:14pm Dec 15, 2015 at 10:14pm UTC
Okay so I was able to fix some of the syntax problems for this, but now all that is showing up is the first line of the array.
How do I make the loop continue so that all 24 elements of the array are printed?
Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
int getARRAY(int numbers[4][6], int , int );
void showARRAY(int numbers[4][6], int , int );
int main()
{
const int ROWS = 4;
const int COLUMNS = 6;
int numbers[ROWS][COLUMNS];
getARRAY(numbers, ROWS, COLUMNS);
cout << "\n\n\tSee files in C:\\ExternalFiles for output.\n\n\n" ;
showARRAY(numbers, ROWS, COLUMNS);
system("Pause" );
return 0;
}
int getARRAY(int numbers[4][6], int ROWS, int COLUMNS)
{
fstream inputFile;
ofstream outputFile;
inputFile.open("C:\\ExternalFiles\\exfile2DARRAY.txt" );
if (inputFile.fail())
{
cerr << "\a\a**Error opening input file.\n" ;
exit(EXIT_FAILURE);
}
for (int x = 0; x < ROWS; x++)
{
for (int y = 0; y < COLUMNS; y++)
{
inputFile >> numbers[x][y];
}
}
}
void showARRAY(int numbers[4][6], int ROWS, int COLUMNS)
{
ofstream outputFile;
outputFile.open("C:\\ExternalFiles\\exfile2DARRAYout.txt" );
if (outputFile.fail())
{
cerr << "\a\a**Error opening output file.\n" ;
exit(EXIT_FAILURE);
}
outputFile << "Content of this array:\n\n" ;
for (int x = 0; x < ROWS; x++)
{
for (int y = 0; y < COLUMNS; y++)
{
outputFile << setw(8) << numbers[x][y] << "\t" ;
}
outputFile << endl;
outputFile.close();
}
}
Also, I keep getting this error: not all control paths return a value.
What do I do about that?
Links or tutorials would be much appreciated!
Last edited on Dec 15, 2015 at 10:37pm Dec 15, 2015 at 10:37pm UTC