external data file

Below is my code, its a running code however its not writing the user input to the EXTERNAL DATA FILE. Please help



// Name: Class:
// Purpose of the program...
// is to manage an array of apps
// providing a way to read and display
// using structures and functions

#include <iostream>
#include <fstream>
using namespace std;
#include <cstring>
#include <cctype>

//Create constants used as array sizes
const int SIZE_TITLE = 131;
const int SIZE_DESC = 300;
const int SIZE_PAGE = 6;


//This creates a grouping for an individual app
struct app
{
char name[SIZE_TITLE];
char description[SIZE_DESC];
char page[SIZE_PAGE];
int length;
};

void displayall(app &); //display all members
void getfilename(char filename[]); //function for getting the external data file
void readall(app &); //reads the input
bool save_app(char filename[], app[], int num);
bool load_app(char filename[], app[], int & num);
bool more();


int main ()
{
app user[20]; //collection of apps for the user
char filename[40]; //holds the file name
int num_apps=0; //number of apps in the array
char rerun;
int max_num_apps = 200;


getfilename(filename);
if(!load_app(filename, user, num_apps));
cout<<"we are starting from stratch!"<<endl<<endl;


int i = num_apps;
do
{
readall(user[i]);
displayall(user[i]);

}while(more() && i < max_num_apps);

save_app(filename, user, i);
cout<<"please try agian";

//display everything
cout<<"The entire file contains these apps: ";
for (int j = 0; j < i; ++j)
displayall(user[j]);






return 0;
}



//Ask the user what file they want to work with
void getfilename(char array[])
{
cout<<"Please enter the name of a file limited to 31 characters: ";
cin.get(array,35,'\n');
cin.ignore(100,'\n');
strcat(array,".txt");

}

//read in all the users apps
void readall(app & an_app)
{
cout<<"Please enter the title: ";
cin.get(an_app.name,131,'\n');
cin.ignore(131,'\n');
}

//write all of the movies OUT to a file
bool save_app(char filename[], app apps[], int num)
{
bool success = true;
ofstream write;
write.open(filename);
write <<"this is a test1.";
if (!write)
{
cout <<"Can't save, try agian \n\n";
success = false;
}
else
{
for(int i=0; i < num; ++i)
{
write <<"this is a test.";
write <<apps[i].name <<"|" <<endl;
}
write.close();
write.clear();
}
return success;
}

//load all movies FROM an external file
bool load_app(char filename[], app apps[], int & num)
{
ifstream read;
read.open(filename);

if(!read)
return false;

read.get(apps[num].name,131,'|');
read.ignore(131,'|');
read.close();
return true;
}



//later be used for a loop
bool more()
{
char response;
cout <<"Would you like to enter another? Y/N ";
cin >>response;
cin.ignore(100,'\n');
if (toupper(response) == 'Y')
return true;
return false;
}

void displayall(app & program)
{
cout << "\n\n" <<program.name<<'\t'<<endl;
}
Please could you enclose your code in code tags, to make it easier to read?

You can get code tags using the "<>" button to the right of the edit window.
It's because you call save_app() with i equal to 0.
In your main, a "++i;" is missing at the end of your do/while loop.
When it askes me if i want to enter more data and I enter N it just closes out the program instead of displaying all of the data. Any Ideas?
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
// Name:   Class:  
// Purpose of the program...
// is to manage an array of apps 
// providing a way to read and display
// using structures and functions

#include <iostream>
#include <fstream>
using namespace std;
#include <cstring>
#include <cctype>

//Create constants used as array sizes
const int SIZE_TITLE = 131;
const int SIZE_DESC = 300;
const int SIZE_PAGE = 6;


//This creates a grouping for an individual app
struct app
{
    char name[SIZE_TITLE];
    char description[SIZE_DESC];
    char page[SIZE_PAGE];
};

void displayall(app &); //display all members
void getfilename(char filename[]); //function for getting the external data file
void readall(app &);  //reads the input
bool save_app(char filename[], app[], int num); 
bool load_app(char filename[], app[], int & num);
bool more();


int main ()
{
    app user[20]; //collection of apps for the user
    char filename[40]; //holds the file name 
    int num_apps=0; //number of apps in the array
    char rerun;
    int max_num_apps = 200;
    

    getfilename(filename);
    if(!load_app(filename, user, num_apps))
                           cout<<"No file found, starting from scratch\n";
    cout<<endl<<endl;                     
    
    
    int i = num_apps; 
    do 
    {
    readall(user[i]);
    displayall(user[i]);
    ++i;
    }while (more() && i < max_num_apps);

     save_app(filename, user, i);
 
       
    //display everything
    cout<<"The entire file contains these apps: ";
    for (int j = 0; j < i; ++j)
        displayall(user[j]);   
    


    
    

    return 0;
}



//Ask the user what file they want to work with
void getfilename(char array[])
{
    cout<<"Please enter the name of a file limited to 31 characters: ";
    cin.get(array,35,'\n');
    cin.ignore(100,'\n');
    strcat(array,".txt");
 
}

//read in all the users apps
void readall(app & an_app)
{
     cout<<"Please enter the title: ";
     cin.get(an_app.name,131,'\n');
     cin.ignore(131,'\n');
     cout<<"Please enter the description: ";
     cin.get(an_app.description,300,'\n');
     cin.ignore(301,'\n');
     cout<<"Please enter what page the app is on: ";
     cin.get(an_app.page,6,'\n');
     cin.ignore(7,'\n');
}

//write all of the movies OUT to a file
bool save_app(char filename[], app apps[], int num)
{
     bool success = true;
     ofstream write;
     write.open(filename);
     if (!write)
     {
                cout <<"Can't save, try agian \n\n";
                success = false;
                }
     else
     {
         for(int i=0; i < num; ++i)
         {
                 
                 write <<apps[i].name <<"|";
                 write <<apps[i].description <<"|";
                 write <<apps[i].page <<endl;
                 }
     write.close();
     write.clear();
     }
     return success;
}

//load all movies FROM an external file
bool load_app(char filename[], app apps[], int & num)
{
     ifstream read;
     read.open(filename);
     
     if(!read)
     return false;
     
     
     
     read.get(apps[num].name,131,'|');
     read.ignore(131,'|');
     while(read && !read.eof())
     {
     read.get(apps[num].description,300,'|');
     read.ignore(300,'|');
     read.get(apps[num].page,6,'|');
     read.ignore(6,'|');
     ++num;
     read.get(apps[num].name,131,'|');
     read.ignore(131,'|');
     }
     read.close();
     return true;
}
     
     

//later be used for a loop
bool more()
{
    char response;
    cout <<"Would you like to enter another? Y/N ";
    cin >>response;
    cin.ignore(10,'\n');
    if (toupper(response) == 'Y')
       return true;
    return false;
}

void displayall(app & program)
{
     cout << "\nApp name: " <<program.name<<'\t'<<endl;
     cout << "\nDescription of program: " <<program.description<<'\t'<<endl;
     cout << "\nPage: " <<program.page<<'\t'<<endl;
     
}
How are you executing your program ?
By double-clicking on the .exe file or by calling it from the console ?
Topic archived. No new replies allowed.