program not reading from file properly

my read in function is load_plans and is on line 132

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
   #include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;

const int ACTIVITY = 50;
const int TYPE = 25;
const int DATE = 15;
const int INFO = 150;
const int REOCCURRING = 5;
const int OPTIONAL_INFO = 130;


struct planning
{
   char activity[ACTIVITY];
   char type_activity[TYPE];
   char date[DATE];
   char info[INFO];
   char reoccurring[REOCCURRING];
   char optional_info[OPTIONAL_INFO];
};

void readall(const char prompt[], int numb_activities, char result[]);
void getting_plan(planning *pointer, int &count);
void display(planning *pointer);
bool file_write(planning *pointer);
bool load_plans(planning *pointer, int &count);



int main()
{   
   char filename[32]; // array to hold filename
   int num_activities = 0; //integer to hold number of activites
   planning * pointer= new planning[20]; //array of my struct 
   //pointer = NULL;
   char character1; //character to set my loop up
   int count;
   count=0;   
   char choice;

   cout<<"would you like to view any files previously written? either y or n.";
   cin>>choice;
   cin.ignore(100, '\n');
   if(choice == 'y')
   {
      load_plans(pointer, count);
      cout<<pointer[0].activity; 
      display(pointer);
   }
   getting_plan(pointer,count); //calling function 
   display(pointer);
   file_write(pointer);
   load_plans(pointer,count);
   delete pointer;
}

void readall(const char prompt[], int size, char result[])
{
	cout  <<prompt << '\n';
	cin.get(result, size, '\n');
	cin.ignore(100, '\n');
}	


void getting_plan(planning *pointer, int &count)
{
   char character; 		
   //do {
   for(int i=0;(i < 20) && character != 'y';++i){
      readall("please enter what the activity is:", ACTIVITY, pointer[i].activity);
      readall("please enter the type of activity this is: ", TYPE, pointer[i].type_activity);
      readall("please enter the date of the activity: ", DATE, pointer[i].date);
      readall("please enter info to prepare for activity: ", INFO, pointer[i].info);
	  readall("please enter if this activity is reoccurring(yes or no): ", REOCCURRING, pointer[i].reoccurring);
	  readall("please enter any other info or comments", OPTIONAL_INFO, pointer[i].optional_info);
          
      cout <<"Is this all the information you want? Enter either y or n." << '\n';
      cin >>character;
      cin.ignore();
      cout <<'\n';
      ++count;
      }//} while((character == 'n') || (character == 'N'));
}


void display(planning *pointer)
{
   for(int i=0;(*pointer[i].date != '\0');++i)
	{
	cout <<"The name of the activity is: "<<pointer[i].activity << endl;
	cout << "The type activity is: "<<pointer[i].type_activity <<endl;
	cout <<"The date of activity is: "<<pointer[i].date <<endl;
	cout << "The information for activity is: "<<pointer[i].info <<endl;
	cout << "Is this activity reoccurring: "<<pointer[i].reoccurring <<endl;
	cout << "Additional info: "<<pointer[i].optional_info <<endl;
    }
}


bool file_write(planning *pointer)
{
	bool sucess = true;
	ofstream write;
	write.open("filename.txt");
	if (!write) //true -- connect; false -- problem
    {
       cout <<"CAN'T SAVE...\n\n";
       sucess = false;
    }
    else
    {

       for(int i=0; (*pointer[i].date != '\0'); ++i)
       {
        write  <<"The name of the activity is: "<<pointer[i].activity << endl
               << "The type of activity is: "<<pointer[i].type_activity <<endl
	           <<"The date of activity is: "<<pointer[i].date <<endl
	           << "The information about this activity is : "<<pointer[i].info <<endl
	           << "Is the activity reoccurring ?: "<<pointer[i].reoccurring <<endl
               << "Additional info for activity: "<<pointer[i].optional_info <<endl<<endl;
       }
	}
    write.close();
    write.clear();
    return sucess;
}


bool load_plans(planning *pointer,int &count)
{ 
    ifstream read;
    read.open("filename"); //connecting
    
    if (!read) //not connected
       return false;
    
    int i =0;   
    //for(int i =0;i < count; ++i)
    //{
    //Connected to the file and ready to read
    read.get(pointer[i].activity,ACTIVITY,'|');
    read.ignore(100,'|');
    while(read && !read.eof()) //previous read is successful
    {
           read.get(pointer[i].activity, ACTIVITY, '|');
           read.ignore(100, '|');
           read.get(pointer[i].type_activity, TYPE, '|');
           read.ignore(100,'|');
           read.get(pointer[i].date, DATE, '|');
           read.ignore(100,'|');
           read.get(pointer[i].info, INFO, '|');
           read.ignore(100, '|');
           read.get(pointer[i].reoccurring,REOCCURRING,'|');
           read.ignore(100,'|');
           read.get(pointer[i].optional_info, OPTIONAL_INFO, '|');
           read.ignore(100, '|');
        }
    //}
    read.close();
    return true;
}
you need to ask for the filename and then pass that to load plans
changed it to this, i am passing a set filename. I forgot the .txt

it is still having reading problems

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <iostream>
#include <cctype>
#include <cstring>
#include <fstream>
using namespace std;

const int ACTIVITY = 100;
const int TYPE = 25;
const int DATE = 15;
const int INFO = 150;
const int REOCCURRING = 215;
const int OPTIONAL_INFO = 30;


struct planning
{
   char activity[ACTIVITY];
   char type_activity[TYPE];
   char date[DATE];
   char info[INFO];
   char reoccurring[REOCCURRING];
   char optional_info[OPTIONAL_INFO];
};

void readall(const char prompt[], int numb_activities, char result[]);
void getting_plan(planning *pointer, int &count);
void display(planning *pointer);
bool file_write(planning *pointer);
bool load_plans(planning *pointer);
void display_read(planning *pointer);


int main()
{   
   char filename[32]; // array to hold filename
   int num_activities = 0; //integer to hold number of activites
   planning * pointer= new planning[20]; //array of my struct 
   //pointer = NULL;

   char character1; //character to set my loop up
   int count;
   count=0;   
   char choice;
   cout<<"would you like to view any files previously written? either y or n.";
   cin>>choice;
   cin.ignore(100, '\n');
   if(choice == 'y')
   {
      load_plans(pointer);
      display(pointer);
   }
   getting_plan(pointer,count); //calling function 
   display(pointer);
   file_write(pointer);
   //load_plans(pointer);
   delete pointer;
}

void readall(const char prompt[], int size, char result[])
{
	cout  <<prompt << '\n';
	cin.get(result, size, '\n');
	cin.ignore(100, '\n');
}	


void getting_plan(planning *pointer, int &count)
{
   char character; 		
   //do {
   for(int i=0;(i < 20) && character != 'y';++i){
      readall("please enter what the activity is:", ACTIVITY, pointer[i].activity);
      readall("please enter the type of activity this is: ", TYPE, pointer[i].type_activity);
      readall("please enter the date of the activity: ", DATE, pointer[i].date);
      readall("please enter info to prepare for activity: ", INFO, pointer[i].info);
	  readall("please enter if this activity is reoccurring(yes or no): ", REOCCURRING, pointer[i].reoccurring);
	  readall("please enter any other info or comments", OPTIONAL_INFO, pointer[i].optional_info);
          
      cout <<"Is this all the information you want? Enter either y or n." << '\n';
      cin >>character;
      cin.ignore();
      cout <<'\n';
      ++count;
      }//} while((character == 'n') || (character == 'N'));
}


void display(planning *pointer)
{
   for(int i=0;(*pointer[i].activity != '\0');++i)
	{
	cout <<"The name of the activity is: "<<pointer[i].activity << endl;
	cout << "The type activity is: "<<pointer[i].type_activity <<endl;
	cout <<"The date of activity is: "<<pointer[i].date <<endl;
	cout << "The information for activity is: "<<pointer[i].info <<endl;
	cout << "Is this activity reoccurring: "<<pointer[i].reoccurring <<endl;
	cout << "Additional info: "<<pointer[i].optional_info <<endl<<"\n";
    }
}


bool file_write(planning *pointer)
{
	bool sucess = true;
	ofstream write;
	write.open("filename.txt");
	if (!write) //true -- connect; false -- problem
    {
       cout <<"CAN'T SAVE...\n\n";
       sucess = false;
    }
    else
    {

       for(int i=0; (*pointer[i].date != '\0'); ++i)
       {
        write  <<pointer[i].activity << '|'
               <<pointer[i].type_activity <<'|'
	       <<pointer[i].date <<'|'
	       <<pointer[i].info <<'|'
	       <<pointer[i].reoccurring <<'|'
               <<pointer[i].optional_info <<'|';
       }
	}
    write.close();
    write.clear();
    return sucess;
}


bool load_plans(planning *pointer)
{ 
    ifstream read;
    read.open("filename.txt"); //connecting
    
    if (!read) //not connected
       return false;
    
    int i =0;   
    //for(int i =0;i < count; ++i)
    //{
    //Connected to the file and ready to read
    read.get(pointer[i].activity,ACTIVITY,'|');
    read.ignore(100,'|');
    while(read && !read.eof()) //previous read is successful
    {
    //  for( i=0;i < 1;++i)
//{
           read.get(pointer[i].activity, ACTIVITY, '|');
           read.ignore(100, '|');
           read.get(pointer[i].type_activity, TYPE, '|');
           read.ignore(100,'|');
           read.get(pointer[i].date, DATE, '|');
           read.ignore(100,'|');
           read.get(pointer[i].info, INFO, '|');
           read.ignore(100, '|');
           read.get(pointer[i].reoccurring,REOCCURRING,'|');
           read.ignore(100,'|');
           read.get(pointer[i].optional_info, OPTIONAL_INFO, '|');
//           read.ignore(100, '|');
//           read.get(pointer[i].activity, ACTIVITY, '|');
  //         read.ignore(100, '|');
           ++i;     
  // }   
}

    //}
    read.close();
    return true;
}


void display_read(planning *pointer)
{
   for(int i=0;i < 1;++i)
{//int i =0;//	{
	cout <<"The name of the activity is: "<<pointer[i].activity << endl;
	cout << "The type activity is: "<<pointer[i].type_activity <<endl;
	cout <<"The date of activity is: "<<pointer[i].date <<endl;
	cout << "The information for activity is: "<<pointer[i].info <<endl;
	cout << "Is this activity reoccurring: "<<pointer[i].reoccurring <<endl;
	cout << "Additional info: "<<pointer[i].optional_info <<endl;
    } 
}
Last edited on
On line 163 you have a random ++i;

Not going to fix your problem most likely though.

Also, when deleting dynamically allocated memory, it is good practice to assign the pointer to NULL after deleting, just in case it was pointing somewhere.
Last edited on
Topic archived. No new replies allowed.