arrays and functions -changin the array

Pages: 12
So I am trying to load this array with numbers from a data file (just random numbers). Then I need to have the function call the array and display the contents. I am stuck I am not sure why it isn't working. It also says that the datafile isnt there but it uses some of the numbers that is in the data file.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

#include <fstream>
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;


//prototype

int display ();


int fac1_1, fac1_2, fac1_3, fac1_4, fac2_1, fac2_2, fac2_3, fac2_4, fac3_1, fac3_2, fac3_3, fac3_4, fac4_1, fac4_2, fac4_3, fac4_4;




//the array

int factory[6][4] = { 
					 {fac1_1, fac1_2, fac1_3, fac1_4},
					 {fac2_1, fac2_2, fac2_3, fac2_4},
					 {fac3_1, fac3_2, fac3_3, fac3_4},
					 {fac4_1, fac4_2, fac4_3, fac4_4}
					
				  	};


int main()
{
	int choice;
	bool again = true;
	
	
	while (again)
	{
		system("cls");
		cout << "1 to display all factories and shifts production runs ";
		cout << "\n2 to displya highest and lowest production runs " ;
		cout << "\n3 Display the average of a factory and shift ";
		cout << "\n4 Manually change the production numbers ";
		cout << "\n5 quit ";
		cout << "\nwhat would you like to do? ";
		cin >> choice;
		
		switch (choice)
		{
		
			case 1:
			{
				cout << "Yay";
				display ();
				getch();
				break;
			}
			
			
			case 2:
			{
				cout << "2 yay";
				break;
			}
			
			case 3:
			{
				
				break;
				
			}
			
			case 4:
			{
				break;
			}
			
			case 5:
			{
				system("cls");
				cout << "You have chosen to quit this program ";
				again = false;
				break;
			
			
			}
		}
	}
	
}


int display()
{
	system("cls");
		
	cout << "You have choosen to see all the factories and their current production runs ";
	//bringing in the file
	
	ifstream inFile("FactoryData.txt", ios::in);
	
	
	if (!inFile);
	
	cout << "\nnot infile\n";
	
	cout << "\n\nhere is the array ";
	
	inFile >> fac1_1 >> fac1_2 >> fac1_3 >> fac1_4 >> fac2_1 >> fac2_2 >> fac2_3 >> fac2_4 >> fac3_1 >> fac3_2 >> fac3_3 >> fac3_4 >> fac4_1 >> fac4_2 >> fac4_3 >> fac4_4;	   	   
	
	
	for (int row = 0; row <= 5; row++)
	{
			
		for	(int col = 0; col <=3; col++)
		{
			
			cout << factory [row][col];	   
			//cout << fac1_1 << fac1_2 << fac1_3 << fac1_4 << fac2_1 << fac2_2 << fac2_3 << fac2_4 << fac3_1 << fac3_2 << fac3_3 << fac3_4 << fac4_1 << fac4_2 << fac4_3 << fac4_4;	   	   	   	   
		}
			
	cout << "\n";
		
	//inFile >> fac1_1 >> fac1_2 >> fac1_3 >> fac1_4 >> fac2_1 >> fac2_2 >> fac2_3 >> fac2_4 >> fac3_1 >> fac3_2 >> fac3_3 >> fac3_4 >> fac4_1 >> fac4_2 >> fac4_3 >> fac4_4;
		
	}
	inFile.close();
}

Last edited on
closed account (D80DSL3A)
The factory[][] array element values are not affected by the assignments on line 109.
Try instead: inFile >> factory[0][0] >> factory[0][1] .....
Or do that in a for loop.

The values you see may be from the uninitialized variables used on line 22 to initialize the factory[][] array elements (but just the 1st 4 of the 6 rows).

You can lose all the fac1_1, etc... variables and just use the factory array.

hey fun2code thanks for your reply. I don't think that I follow the first set of instructions. If I change the inFile to what you are saying then how would it know what numbers I want them to be?

The variables that I put it were a grasp at life trying to make it do what I want it to.

In the function I open the file that I want it to read from. Once the file is open I want it to create a 2 dimensional array just how I put the variables on line 23. The issue is that it is not reading from the file and I am not sure why. I feel like the logic goes wrong somewhere.
closed account (j3Rz8vqX)
Well for starters you can correctly check if your file opened correctly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int display()//change void instead?
{
    //system("cls");//For me: only available with <cstdlib>, which isn't included
    //http://www.cplusplus.com/reference/cstdlib/system/

    cout << "You have choosen to see all the factories and their current production runs ";
    //bringing in the file
    ifstream inFile("FactoryData.txt", ios::in);
    if (!inFile)
    {
        cout << "\nnot infile\n";
    }
    else
    {
        //... Something is wrong in the section(logically that is).
    }
    inFile.close();
    return 0;//? change to void and remove return?
}

closed account (D80DSL3A)
You're welcome.
If I change the inFile to what you are saying then how would it know what numbers I want them to be?

I'm not sure what you mean. How did you know what numbers you wanted fac1_1, fac1_2, etc... to be?

The numbers are all to come from the file, right?
Your code for opening the file looks good. Perhaps you are placing the file in the wrong folder?

I would go with some code which watches for end of file while filling the factory array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const int rows = 6;
const int cols = 4;
int factory[rows][cols];

ifstream inFile("FactoryData.txt", ios::in);	
if (!inFile)
{
    cout << "\nnot infile\n";
    return 0;// bail from function. Why return int? (as Dput asked)
}
	
// If we get here, the inFile is good

int r=0, c=0;
while( r < rows && inFile >> factory[r][c] )// stops when array is full or file is empty, whichever happens 1st.
{
    ++c;// increment column
    if( c >= cols )// too high
    {
        c = 0;
        ++r;// increment row
    }
}
Last edited on
fun2code,

I think that I am having issues with getting the program to actually open the file. I made a replica program of it and it works just fine it doesn't trip the
if (!infile) statement. On the real one (the one i posted) the if (!infile) activates.

The two programs are in different folders however the data files are in the same folder with the program. There is two copies of the exact file. I have deleted them and resaved them just about everything i can think of to fix it. Any ideas what is going on?
closed account (j3Rz8vqX)
Try this:
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
int display()//change void instead?
{
    //system("cls");//For me: only available with <cstdlib>, which isn't included
    //http://www.cplusplus.com/reference/cstdlib/system/

    cout << "You have choosen to see all the factories and their current production runs ";
    //bringing in the file
    ifstream inFile("FactoryData.txt", ios::in);
    if (!inFile)
    {
        cout << "\nnot infile\n";
    }
    else
    {
        for(int y=0;!inFile&&y<6;y++)
        {
            for(int x=0;!inFile&&x<4;x++)
            {
                inFile >> factory[y][x];
                cout<<"Factory ["<<y<<"]["<<x<<"]: "<<factory[y][x]<<endl;
            }
        }
    }
    inFile.close();
    return 0;//? change to void and remove return?
}


Was written with browser text editor; does not ensure compilation.

Hopefully this helps.
hey Dput thanks for trying to help,

With the help of fun2code I think I understand the logic. I am using the
if (!infile) line to just see if the program is finding the file and it is not. I just recently tried putting the program in a folder along with the FactoryData.txt and I am still having no luck with it.

As in the previous post the other program works with it just fine, the main one is having issues. It is really weird and I am not sure why its happening. I think its something small that I'm overlooking.
Here is what I have so far with my code. The issue still remains as not reading the file into the function. I have exhausted everything I know to get the file to work so I hope someone can now help me figure this out.


This is the part that is having issues
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
void display()
{
	system("cls");
		
	cout << "You have choosen to see all the factories and their current production runs ";
	//bringing in the file

	ifstream inFile("FactoryData.txt" , ios::in); //wont find the file its in the same folder as the program its name is "FactoryData"
	
	inFile >> factory[5][3];  //not sure if i need this or not doesnt seem to hurt or help
	
	cout << "\n\nhere is the array ";	
	
	if (!inFile); //goes into this if statement everytime
	{
		cout << "\nnot infile\n";
		getch();
	}
	
	
	for (int row = 0; row <= 5; row++)
	{	 	 
		for	(int col = 0; col <= 3; col++)
		{	 
			cout << factory [row][col] << "\t";	      	  	  
		}
			
	cout << "\n"; 
		
	
		
	}
	inFile.close();
}




This is the entire code that I have right now
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

#include <fstream>
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;


//prototype

void display ();


//int fac1_1, fac1_2, fac1_3, fac1_4, fac2_1, fac2_2, fac2_3, fac2_4, fac3_1, fac3_2, fac3_3, fac3_4, fac4_1, fac4_2, fac4_3, fac4_4;




//the array

int factory[3][5]; // { 
					// {fac1_1, fac1_2, fac1_3, fac1_4},
					// {fac2_1, fac2_2, fac2_3, fac2_4},
					 //{fac3_1, fac3_2, fac3_3, fac3_4},
					// {fac4_1, fac4_2, fac4_3, fac4_4}
					
				  //	


int main()
{
	int choice;
	bool again = true;
	
	
	while (again)
	{
		system("cls");
		cout << "1 to display all factories and shifts production runs ";
		cout << "\n2 to displya highest and lowest production runs " ;
		cout << "\n3 Display the average of a factory and shift ";
		cout << "\n4 Manually change the production numbers ";
		cout << "\n5 quit ";
		cout << "\nwhat would you like to do? ";
		cin >> choice;
		
		switch (choice)
		{
		
			case 1:
			{
				cout << "Yay";
				
				display ();
				getch();
				break;
			}
			
			
			case 2:
			{
				cout << "2 yay";
				break;
			}
			
			case 3:
			{
				
				break;
				
			}
			
			case 4:
			{
				break;
			}
			
			case 5:
			{
				system("cls");
				cout << "You have chosen to quit this program ";
				again = false;
				break;
			
			
			}
		}
	}
	
}


void display()
{
	system("cls");
		
	cout << "You have choosen to see all the factories and their current production runs ";
	//bringing in the file

	ifstream inFile("FactoryData.txt" , ios::in); //wont find the file its in the same folder as the program its name is "FactoryData"
	
	inFile >> factory[5][3];  //not sure if i need this or not doesnt seem to hurt or help
	
	cout << "\n\nhere is the array ";	
	
	if (!inFile); //goes into this if statement everytime
	{
		cout << "\nnot infile\n";
		getch();
	}
	
	
	for (int row = 0; row <= 5; row++)
	{	 	 
		for	(int col = 0; col <= 3; col++)
		{	 
			cout << factory [row][col] << "\t";	      	  	  
		}
			
	cout << "\n"; 
		
	
		
	}
	inFile.close();
}

closed account (j3Rz8vqX)
I'm not sure the intentions of the program, but the below code works (possibly not your intentions):
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <fstream>
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;


//prototype

void display ();


//int fac1_1, fac1_2, fac1_3, fac1_4, fac2_1, fac2_2, fac2_3, fac2_4, fac3_1, fac3_2, fac3_3, fac3_4, fac4_1, fac4_2, fac4_3, fac4_4;




//the array

int factory[3][5]; // {
					// {fac1_1, fac1_2, fac1_3, fac1_4},
					// {fac2_1, fac2_2, fac2_3, fac2_4},
					 //{fac3_1, fac3_2, fac3_3, fac3_4},
					// {fac4_1, fac4_2, fac4_3, fac4_4}

				  //


int main()
{
	int choice;
	bool again = true;


	while (again)
	{
		//system("cls");
		cout << "1 to display all factories and shifts production runs ";
		cout << "\n2 to displya highest and lowest production runs " ;
		cout << "\n3 Display the average of a factory and shift ";
		cout << "\n4 Manually change the production numbers ";
		cout << "\n5 quit ";
		cout << "\nwhat would you like to do? ";
		cin >> choice;

		switch (choice)
		{

			case 1:
			{
				cout << "Yay";

				display ();
				getch();
				break;
			}


			case 2:
			{
				cout << "2 yay";
				break;
			}

			case 3:
			{

				break;

			}

			case 4:
			{
				break;
			}

			case 5:
			{
				//system("cls");
				cout << "You have chosen to quit this program ";
				again = false;
				break;


			}
		}
	}

}
void display()//change void instead?
{
    //system("cls");//For me: only available with <cstdlib>, which isn't included
    //http://www.cplusplus.com/reference/cstdlib/system/

    cout << "You have choosen to see all the factories and their current production runs ";
    //bringing in the file
    ifstream inFile("FactoryData.txt", ios::in);
    if (!inFile)
    {
        cout << "\nnot infile\n";
    }
    else
    {
        for(int y=0;y<6;y++)
        {
            for(int x=0;x<4;x++)
            {
                inFile >> factory[y][x];
                cout<<"Factory ["<<y<<"]["<<x<<"]: "<<factory[y][x]<<endl;
            }
        }
    }
    inFile.close();
}


FactoryData.txt:
1
2
3
4
5
6
1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
8 9 1 2
3 4 5 6


Output Result:
Factory [0][0]: 1
Factory [0][1]: 2
Factory [0][2]: 3
[...]
Factory [5][1]: 4
Factory [5][2]: 5
Factory [5][3]: 6


I've modified your display() with the one posts previously minus function header modification and return modification.

Try it.
closed account (j3Rz8vqX)
Modified to your like of output:
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <fstream>
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;


//prototype

void display ();
//int fac1_1, fac1_2, fac1_3, fac1_4, fac2_1, fac2_2, fac2_3, fac2_4, fac3_1, fac3_2, fac3_3, fac3_4, fac4_1, fac4_2, fac4_3, fac4_4;

//the array
int factory[3][5]; // {
					// {fac1_1, fac1_2, fac1_3, fac1_4},
					// {fac2_1, fac2_2, fac2_3, fac2_4},
					 //{fac3_1, fac3_2, fac3_3, fac3_4},
					// {fac4_1, fac4_2, fac4_3, fac4_4}
int main()
{
	int choice;
	bool again = true;
	while (again)
	{
		//system("cls");
		cout << "1 to display all factories and shifts production runs ";
		cout << "\n2 to displya highest and lowest production runs " ;
		cout << "\n3 Display the average of a factory and shift ";
		cout << "\n4 Manually change the production numbers ";
		cout << "\n5 quit ";
		cout << "\nwhat would you like to do? ";
		cin >> choice;

		switch (choice)
		{
			case 1:
			{
				cout << "Yay";
				display ();
				getch();
				break;
			}
			case 2:
			{
				cout << "2 yay";
				break;
			}
			case 3:
			{
				break;
			}
			case 4:
			{
				break;
			}
			case 5:
			{
				//system("cls");
				cout << "You have chosen to quit this program ";
				again = false;
				break;
			}
		}
	}

}
void display()//change void instead?
{
    //system("cls");//For me: only available with <cstdlib>, which isn't included
    //http://www.cplusplus.com/reference/cstdlib/system/

    cout << "You have choosen to see all the factories and their current production runs \n";
    //bringing in the file
    ifstream inFile("FactoryData.txt", ios::in);
    if (!inFile)
    {
        cout << "\nnot infile\n";
    }
    else
    {
        for(int y=0;y<6;y++)
        {
            for(int x=0;x<4;x++)
            {
                inFile >> factory[y][x];
                cout << factory [y][x] << "\t";
            }
        }
    }
    inFile.close();
}

Hey Dput,

I appreciate your replies. The program compiles just fine. However on line 76-79 is a check to see if the program finds the file and it does not find the file. When you compile does the program find the file? The file is just a bunch of random numbers. So when it shows the array it is all 0's.
You should place a return statement after line 78. If the file is not found, you don't want to fall through to the rest of the code.

Only suggestion I have regarding not finding the file is to make such the data file is in the same folder as the executable file, which I think you've already tried. This should be the default folder where your IDE builds the executable. If you use a different folder, you are going to have to copy the executable to the alternate folder every time you recompile.
closed account (j3Rz8vqX)
If you're trying to run from the IDE debugger, have the txt file at the same location as the project file; normally where your source file is.

If you're executing the executable, then have a readily txt file where your executable is at.
closed account (D80DSL3A)
Hi. One method for finding where the text file needs to be is to use ofstream to open a file for writing. The file will be created if it doesn't exist.
Then go find it. It should be where your input file also belongs.
Last edited on
closed account (j3Rz8vqX)
Smart idea =D

Locate where you believe your text file was being read from ^^.
Hey guys thanks for all your replies. I have written a code for it to place all these random numbers into the 2d array. However I run into the same issue so I am sure its this part. The format comes out correctly just there is no numbers only '0'.

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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;


int array[4][6];


int main()
{
	string stringtext;
	
	
	cout << "this will demonstrate a 2d array being filled by a for loop \n\n";

	// Writing to the file
	ifstream inFile;
	
	inFile.open("randomnumbers.txt", ios::out);
	
	
	if (!inFile)
	{
		cout << "\n\nThere was no file to be found " ;
	}
	
	cout << array[4][6];
	
	for (int row = 0; row <=6; row++)
	{
		for (int col = 0; col <= 4 ; col++)
		{
			
			inFile >> array[4][6];
			
			cout << array[col][row] << "\t";
		}
		
	cout << "\n";
	}
	
	getch();
	inFile.close();
}
closed account (j3Rz8vqX)
??:
1
2
3
			inFile >> array[4][6];//This will always write to the last index; no good.
			
			cout << array[col][row] << "\t";


Try this instead:
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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;


int array[4][6];


int main()
{
	string stringtext;
	
	
	cout << "this will demonstrate a 2d array being filled by a for loop \n\n";

	// Writing to the file
	ifstream inFile;
	
	inFile.open("randomnumbers.txt", ios::out);
	
	
	if (!inFile)
	{
		cout << "\n\nThere was no file to be found " ;
	}
	
	//cout << array[4][6];//Only prints the last index item.
	
	for (int row = 0; row <6; row++)//Less than, not equal, else undefined behavior
	{
		for (int col = 0; col < 4 ; col++)//Less than, not equal, else undefined behavior
		{
			
			//inFile >> array[4][6]; //This isn't useful.
			
			inFile >> array[col][row];
			cout << array[col][row] << "\t";
		}
		
	cout << "\n";
	}
	
	getch();
	inFile.close();
        return 0;
}
Dput thanks!! it works now. can you tell me what the infile >> array [4][6]; was doing?
closed account (j3Rz8vqX)
It was printing the value at that location.

[4][6] is the last element in your array; whatever that was, it was printing it.
Pages: 12