File does not work.Does not record information.Please help!
-Enter of a keyboard and a file into an array (by adding) data to 30 girls

#include <iostream>
#include<fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
#define N 30 

 FILE *fp;

 struct girl
{
char number[10];
int age;
int bust ;
char height[20]; 
char hip[20]; 
char waist[20]; 
char weight[20];
  }
gr[N];
int top=0;

int menu() 
{ 
 int ch; 
    cout<<"\n_______________MENU________________"; 
   cout<<"\n 1. Input number of girls"; 
   cout<<"\n 2. Display list of all girls"; 
   cout<<"\n 3. Display girls under 20"; 
   cout<<"\n 4. Girl largest bust "; 
   cout<<"\n 5. Search by number:"; 
   cout<<"\n 6. Exit"; 
   do 
   { 
           cout<<"\n Choice: "; 
           cin>>ch; 
   } 
   while(ch<1||ch>6); 
           return(ch); 
}


void load()
{
if ((fp=fopen("my.txt","rb"))==NULL)return;
if (fread(&top, sizeof top, 1, fp)!=1)
{cout<<"\n Error"; exit(1);}
if(fread(gr, sizeof gr, 1, fp)!=1)
{cout<<"\n Error"; exit(1);}
fclose(fp);
}
void save()
{
if ((fp=fopen("my.txt", "wb"))==NULL)
{cout<<"\n error"; exit(1);}
if(fwrite (&top,sizeof top, 1, fp)!=1)
{cout<<"\n Error"; exit(1);}
if(fwrite (gr, sizeof gr, 1, fp)!=1)
{cout<<"\n Error"; exit(1);}
fclose(fp);}
void disp(int n)
{
       cout<<"\n Number: "<<gr[n].number;
       cout<<"\n Age: "<<gr[n].age;
       cout<<"\n Waist: "<<gr[n].waist;
       cout<<"\n Hip: "<<gr[n].hip;
       cout<<"\n buist: "<<gr[n].bust;
       cout<<"\n weight:"<<gr[n].weight;
       cout<<"\n height:"<<gr[n].height;
}
void info()
{
int i;
cout<<"\n Spisyk: \n";
for(i=0; i<top; i++)
disp(i);
}

void enter()
{
    int count=0,n, i, ost;
    ost=N-top;
if(ost>0)
{cout<<"\n There is room for more"<<ost<<" contestants";
{
cout<<"\n New contestants:";cin>>count;}
while (count<0 || count>ost);
for (i=top; i<top+count; i++)
   {


       cout<<"\n Number: ";
       cin>>gr[i].number;
       cout<<"\n Age: ";
       cin>>gr[i].age;
       cout<<"\n hip: ";
       cin>>gr[i].hip;
       cout<<"\n waist: ";
       cin>>gr[i].waist;
       cout<<"\n bust: ";
       cin>>gr[i].bust;
       cout<<"\n weight:";
       cin>>gr[i].weight;
       cout<<"\n height:";
       cin>>gr[i].height;
   }
   top=i;
}
else cout<<"Error.";
}


void list() 
{
   int i;
   cout<<"\n List\n";
   for(i=0;i<top;i++)
   disp(i);
}
void teen() 
{ 
     int i; 
      cout<<"\n Girls under 20\n"; 
         for(i=0;i<top;i++) 
         {gr[i].age*=1; 
           if(gr[i].age<20) 
                   disp(i); 
         } 
} 
void showgirl(girl gr[N])

{
char t_number[10];

int i, found;


fflush(stdin);
cout<<"\n Search by number \n ";
cout<<"\n number \n";
cin>>t_number;;     
found=0;
for(i=0; i<top; i++){
if(!strcmp(t_number, gr[i].number)) {
disp(i);
found=1;

}
}
if(!found){
cout<<"\n Error. ";
}
}
void bust()
{int fb=0,pos=0,i;

    {
    string im;
        string ifn;
    int max=500;
    for(int r=0;r<fb;r++)
    {
    if(gr[r].bust<max)
    {
        max=gr[r].bust;
        im=gr[r].number;


    }
    }
    cout<<"\n Girl largest bust :"<< "\n "<<gr[i].number<<"\t"<<gr[i].age<<"\t"<<gr[i].waist<<"\t"<<gr[i].hip<<"\t"<<gr[i].bust<<"\t"<<gr[i].height<<"\t"<<gr[i].weight<<"\t"<<endl;
    for(i=0;i<pos;i++)
        for(int j=0;j<pos-i-1;j++)
            if(gr[j].bust>gr[j+1].bust)
            {
                girl c=gr[j];
                gr[j]=gr[j+1];
                gr[j+1]=c;
            }
            cout<<"\n "<<endl;
            for(int y=0;y<pos;y++)
                cout<<gr[y].number<<" "<<gr[y].bust<<endl;
}

}



int main()
{ int ch;
load();
do
{ ch=menu();
switch(ch)

           { 
           case 1: enter();break; 
           case 2: list();break; 
           case 3: teen();break; 
           case 4: showgirl(gr);break; 
           case 5: bust( );break;

}}
while (ch!=6);
}

Dani AI

Generated

Brief diagnosis and immediate fix (ties to earlier replies): was correct — the program never persists new records because the save routine is never invoked after data entry. ’s suggestion to use ofstream is valid as an alternative to raw binary fwrite/fread. The quickest reliable fix is to (1) correct the broken input loop, (2) call the save routine after successful inserts and before exit, and (3) verify the file actually opens and is written in the program’s working directory.

Key bugs to address (concise):

  • The enter() input loop is malformed (misplaced braces / do–while); top must be incremented correctly after adding entries.
  • save() is never called after data changes (no persistence). Call it after enter() and once more on clean exit.
  • Menu option handling is switched: the switch/case mapping does not match the printed menu (largest bust vs. search).
  • bust() uses uninitialised counters so the “largest” computation never works. Initialize max/index and scan all valid entries.
  • fflush(stdin) is undefined; replace with cin.ignore()/getline() and prefer std::string for text fields to avoid buffer overflow.
  • Writing raw structs with fwrite is platform-dependent (padding/endian). For portability prefer a simple text (CSV) format or write individual fields.

Minimal actionable snippets (examples only — integrate into existing code base):

/* call save after adding records and before exit */
case 1: enter(); save(); break;

/* text-file example (human-readable, portable) */
std::ofstream out("my.txt");
for (int i = 0; i < top; ++i)
  out << gr[i].number << ',' << gr[i].age << ',' << gr[i].bust << '\n';

Practical next steps: fix the enter() loop so count is validated, update top, add save() calls, replace fflush(stdin), and add file-open error checks (print errno or throw). After those changes, test by adding one or two records, confirm my.txt size/timestamp changes, then restart the program to confirm load() returns the saved entries. For longer-term maintenance, migrate text fields to std::string and store records in std::vector for safety and clarity.

Recommended Answers

All 2 Replies

It doesn't work because it never saves the data to the file.

just try ofstream procedure to record data. just try this one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.