Loading Array data after cosole closed?Possible or not?

I am working on a program that allows you to input data into the console, and then it will take it and match it up in a wordperfect file(I am doing this for a person who is wordperfect fanatic), and it works great, but i close the app and all data is lost except for the file, is there a way to load the data and continue on with the point that you left off at... and any other suggestions for the code will be helpful too, thanks in advance for reading this, and for help.


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
#include <iostream> 
#include <fstream>
#include <string>

using namespace std;

int main()
{
	cout << "warning do not enter a space ever!!"<<endl;

	int index1 = 1;
	int index2 = 1;
	int count = 0;
	int loop =1;
	int loop1;
	int index1a =1;
	int index2a =1;
	int number = 0;
	string names [3000];
	string caseNumber[3000];
	string confirmation;

	for(loop =1; loop < 3001;loop ++)
	{
		index1++;
		index2 ++;
		count ++;
		reset:
		confirmation == "a";
		cout << "Client Name "<<count<<": ";
		cin >> names[index1];
		cout <<"Enter Correlating Case Number: ";
		cin >> caseNumber[index2];
		cout<<"Client Name "<<count<<": " <<names[index1]<<endl;
		cout << "Case Number "<< count << " "<< caseNumber[index2]<<endl;
		//---------------------------------------------------
		cout << "Is this Correct ^^ ?, enter yes if correct, no for no,";
		cout << "and if you wish to terminate"<<endl;
		cout <<"the program type /end."<<endl;
		cin >> confirmation;
		if(confirmation =="yes")
		{
			cout << "Confirmed"<<endl;
		}
		if(confirmation == "no")
		{
			goto reset;
		}
		if( confirmation  == "/end")
		{
			goto end;
		}

	}
		
		end:
		ofstream myfile;
		myfile.open("CaseData.wpd");
	
		
		for( loop1 =1; loop1 <3001;loop1 ++)
	{
		index1a ++;
		index2a ++;
		number ++;
		myfile <<number <<".)" <<caseNumber[index1a]<< " = "<<names[index2a]<<endl;  
	}
	
	return 0;
}
Last edited on
First things first. Array indexing in c++ starts from 0. So all the loops should also start at 0. In your case valid indices are 0-2999 and if you try to access element 3000 - the behavior is undefined - your program may crash or output something unexpected.

There are many things that are not nice in your code. If you really want to write better code - I made some changes to your code that make it somewhat better, so please compare it with yours and try to understand what has changed and why it is better. Having said that, there are a lot of other things that can be improved: you can get rid of the limitation of 3000 elements (by using std::vector), make the code more fail-safe (in case the user makes a mistake in input), and a million other things. If you really want to become a better C++ programmer you need to get a grasp of all these things.


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
int main() {
    cout << "warning do not enter a space ever!!"<<endl;

    string names [3000];
    string caseNumber[3000];
    string confirmation;

    int index = 0;
    for(; index < 3000; ++index)
    {
        confirmation = "a";
        cout << "Client Name "<< index + 1 <<": ";
        cin >> names[index];
        cout <<"Enter Correlating Case Number: ";
        cin >> caseNumber[index];
        cout<<"Client Name "<< index + 1 <<": " <<names[index]<<endl;
        cout << "Case Number "<< index + 1 << " "<< caseNumber[index]<<endl;
        //---------------------------------------------------
        cout << "Is this Correct ^^ ?, enter yes if correct, no for no,";
        cout << "and if you wish to terminate"<<endl;
        cout <<"the program type /end."<<endl;
        cin >> confirmation;
        if(confirmation =="yes")
        {
            cout << "Confirmed"<<endl;
        }
        if(confirmation == "no")
        {
            --index; // to cancel off the index++ at the end of the "for" loop
            continue;
        }

        if( confirmation  == "/end")
        {
            break; // exits the loop
        }
    }

    ofstream myfile("CaseData.wpd");

    for(int index2 = 0; index2 < index + 1; ++index2) // index contains the number of the last valid element in the array
    {
        myfile <<index2 + 1 <<".)" <<caseNumber[index2]<< " = "<<names[index2]<<endl;  
    }

    return 0;
}



As for your main question - the simple answer is "no". If you want to do that you need to parse the "CaseData.wpd" file before asking for user input to restore the state of the program. However, in your case the only dependency on the previous input is the case number. You can save it to a separate file and load it on startup or count the number of strings in a file and then add it to a local case number upon output (e.g. myfile << index2 + 1 + previousNumber << ..., where previousNumber is the number of cases in the output file before this run). If you do so, you would need to append data to the "CaseData.wpd" file which can be done by providing a flag to the ofstream constructor:

ofstream myfile("CaseData.pwd", ios_base::app);

Hope I rather helped than confused you :)

Last edited on
Topic archived. No new replies allowed.