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 :)