arrays and functions -changin the array

Pages: 12
Ah that makes sense thanks for all your help!
So I have a question involving this 2d array. Why when I try to change the value of one of the values it always changes the value to 0 and places it in the last place in the array? Can someone help me figure this out?

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
	cout << "\n\nYou have selected to change the production value of a factories shift ";
	cout << "\n\nWhat factory would you like to change? ";
	cout << "\nSelect factory <1-6> ";
	cin >> factorynum;
	factorynum = factorynum - 1;
	
	cout << "\n\nNext pick what shift you would like to change ";
	cout << "\nSelect shit <1-4> ";
	cin >> shiftnum;
	shiftnum = shiftnum - 1;
	
	cout << "\n\nWhat would you like to change the production rate to? ";
	cout << "\nPlease enter a whole number (i.e. 400 or 90) ";
	cin >> number;

	cout << factory[factorynum][shiftnum];
	
	ofstream outfile;
	
	outfile.open("FactoryData.txt" , ios::out|ios::app);
	
	factory[factorynum][shiftnum] = number;
	
	outfile << factory[factorynum][shiftnum];
	
	cout << factory[factorynum][shiftnum];
	
	
	getch();
	infile.close();
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
27
28
29
30
	cout << "\n\nYou have selected to change the production value of a factories shift ";
	cout << "\n\nWhat factory would you like to change? ";
	cout << "\nSelect factory <1-6> ";
	cin >> factorynum;
	factorynum = factorynum - 1;
	
	cout << "\n\nNext pick what shift you would like to change ";
	cout << "\nSelect shit <1-4> ";
	cin >> shiftnum;
	shiftnum = shiftnum - 1;
	
	cout << "\n\nWhat would you like to change the production rate to? ";
	cout << "\nPlease enter a whole number (i.e. 400 or 90) ";
	cin >> number;

	//cout << factory[factorynum][shiftnum]; //Initialized on line 22
	
	ofstream outfile;
	
	outfile.open("FactoryData.txt" , ios::out|ios::app);
	
	factory[factorynum][shiftnum] = number;
	
	outfile << factory[factorynum][shiftnum];
	
	cout << factory[factorynum][shiftnum];
	
	
	getch();
	infile.close();
Hey Dput thanks for your replies today. I was using line 16 just as a check to see if it found what I wanted it to. After I commented it out I can only change the last value in the array so factory 6 shit 4 is the only one I can change and It wont stay saved after I restart the program.
closed account (j3Rz8vqX)
Hmm, shouldn't be:

You prompted for index#1 factorynum @ line 4; -1 to count from 0.

You prompted for index#2 shiftnum @ line 9; -1 to count from 0.

You assigned "number" a value of between(90-400) on line 14.

File process looks okay...

Assign factory[index#1][index#2] equal to value at "number"

You then wrote it to file. (this element, not the whole array table XD - is this it)

Then it printed what was in that factory.

Acquired a key from the user.

Closed file.

End.

---

Looks alright to me.
[quote]It wont stay saved after I restart the program[quote]

Are you intentions to read from the file during every start up as well?

Ideally, once you leave the scope of main-function all memory is cleared.
Last edited on
Well the intention of the program is to change the factory and shift value to whatever integer I like and then when i restart the program it keeps that number. I want it to append that specific point in the array to the "number" variable. However, the only thing I seem to get to change in the last value in the array. Then when I rerun the program the number is back to zero again.

the entire function for this issue is:

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
void change ()
{
	system("cls");
	
	int count = 0;
			
	cout << "Here is the production runs for each factory and shift\n\n";		
	
	for (int row = 0; row < 6; row++)
	{	
		count ++;
		
		if (count == 1)
		{
			int count2 = 0;
				
			for (int a = 0; a <=3; a++)
			{
				count2++;
				cout << "\t\tShift " << count2;
			} 
			
		cout << "\n";
		}
			
		
		cout << "Factory " << count;   
		for	(int col = 0; col < 4; col++) //must match what the array says (same order)
		{	 
			infile >> factory[row][col];
			

			cout << "\t" << factory[row][col] << "\t";	     	 	 
		}
			
	cout << "\n"; 
		
		
	}
	
		
	ofstream outfile;
	
	outfile.open("FactoryData.txt" , ios::out|ios::app);
	
	
	cout << "\n\nYou have selected to change the production value of a factories shift ";
	cout << "\n\nWhat factory would you like to change? ";
	cout << "\nSelect factory <1-6> ";
	cin >> factorynum;
	factorynum = factorynum - 1;
	
	cout << "\n\nNext pick what shift you would like to change ";
	cout << "\nSelect shit <1-4> ";
	cin >> shiftnum;
	shiftnum = shiftnum - 1;
	
	cout << "\n\nWhat would you like to change the production rate to? ";
	cout << "\nPlease enter a whole number (i.e. 400 or 90) ";
	cin >> number;

	//cout << factory[factorynum][shiftnum];

	factory[factorynum][shiftnum] = number;
	
	outfile << factory[factorynum][shiftnum];
	
	cout << factory[factorynum][shiftnum];
	
	
	getch();
	infile.close();
	outfile.close();

}

closed account (j3Rz8vqX)
I have not thoroughly looked at your code yet, but in response to your above question:

You did correctly write to one spot on your 2D array table.
It was prompt to the user on the lower half of the source provided.

And you did write it successfully to the file; although probably not in a way you'd want it to be - current a integer value.

What your missing is your file reading process.

How are you to return the data back into the table on the next start up?

It is really up to you.

Read by integer? read by line? read by string?

Also, will you map the whole table on the text file, or will you have two indexes plus the value at its location?

You've still got a bit of designing to do.

How ever you are planning to read from file, you should consider writing the same way.

Think about it.

(ZZzzZZzz)

(Edit)Example:
Table: (Prior to modification)
- 0 1 2 3
- --------
0 0 0 0 0
1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
4 0 0 0 0
5 0 0 0 0

Table: (After to modification) (say: factory:2-1, shift:4-1, integer:7(small for illustration purposes)
- 0 1 2 3
- --------
0 0 0 0 0
1 0 0 0 7
2 0 0 0 0
3 0 0 0 0
4 0 0 0 0
5 0 0 0 0

How are you to write this to file?

Currently:outfile << factory[factorynum][shiftnum];
Appends the integer at that spot to your file
File:7

My two quick judgement's are:
To write the who table to the file:
1
2
3
4
5
6
0 0 0 0 0
0 0 0 7
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Or:
To write the two indexes followed by the integer value.
[code]0 4 7[code]

Consider using space, tab, or newline as delimiters reading/writing delimiters.

If not, you'll have to create your own character delimiters and parse your strings >_<
Last edited on
hey yeah so the goal is to replace the number inside the array to whatever "number" they ask.

so for example

300 450 500 210
510 600 750 400
627 100 420 430
530 621 730 530
200 050 058 200
100 082 920 290

thats my array now say that they want to change the array value in row 3 col 2 (which is 100) to 200. The "number" variable is the number they wish to change it to (in my example its 200). I want to keep all the other values. Next time I run the program it should say 200 instead of 100.

I think that the way i save the file is the issue. If i drop the "ios::app" at the end of line 44 it works just fine. It will let me change all the values. When it restarts it only shows me the values that I changed. If I only changed the 100 value to 200 then all the other values in the array will be 0 except for the one I changed. If i keep both "ios::out|ios::app" it only lets me change the last value sometimes. Other times it changes it to 0 and leaves it that way.
closed account (j3Rz8vqX)
Yes

I think you should truncate the file.

-There isn't an easy way to insert data back into the file a specific indexes.

If your table successfully read the members, your array should be accurate.

-----
The below will solve the write method:
Rewrite the data members back into file; the full array.
If i drop the "ios::app" at the end of line 44 it works just fine. It will let me change all the values.


Now your concern is the read method:
double embeded Loops(x,y)//<--Index control behavior.

infile >> factory[factorynum][shiftnum];

Example: http://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm
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
#include <fstream>
#include <iostream>
using namespace std;
 
int main ()
{
    
   char data[100];

   // open a file in write mode.
   ofstream outfile;
   outfile.open("afile.dat");

   cout << "Writing to the file" << endl;
   cout << "Enter your name: "; 
   cin.getline(data, 100);

   // write inputted data into the file.
   outfile << data << endl;

   cout << "Enter your age: "; 
   cin >> data;
   cin.ignore();
   
   // again write inputted data into the file.
   outfile << data << endl;

   // close the opened file.
   outfile.close();

   // open a file in read mode.
   ifstream infile; 
   infile.open("afile.dat"); 
 
   cout << "Reading from the file" << endl; 
   infile >> data; 

   // write the data at the screen.
   cout << data << endl;
   
   // again read the data from the file and display it.
   infile >> data; 
   cout << data << endl; 

   // close the opened file.
   infile.close();

   return 0;
}


Just modify the reading method to write into your array, based on index.
ok I think I got it now. Thanks for all your help!
Topic archived. No new replies allowed.
Pages: 12