why my program don't execute?

void Supplier::AddDetails()
{
 char ch='y';
 int end;
 fstream suppauto;
 suppauto.open("Supplier.txt",ios::app);
 suppauto.seekg(0,ios::end);
 while (ch=='y')
  {
  char s[12];
  system("cls");
  cout<< "  Adding Supplier Details"<<endl;
  cout<<"         ****************************************"<<endl<<endl;
  suppauto.seekg(0,ios::end);
  end=suppauto.tellg();
  if (end==0)
  {
   suppauto.close();
   strcpy(suppcode,"S0001");
  }
  else
  {
   suppauto.close();
   for(int i=0;i<6;i++)
       suppcode[i] = '\0';
   fstream suppauto1("Supplier.txt",ios::in);
   while(!suppauto1.eof())
   {
   suppauto1.read( (char *)this, sizeof(class Supplier) );
   }
   suppauto1.close();

   int supcode;
   char supp[6];
   supp[0]=suppcode[1];
   supp[1]=suppcode[2];
   supp[2]=suppcode[3];
   supp[3]=suppcode[4];
   supp[4]='\0';
   supcode=atoi(supp);
   supcode++;
   if (supcode<=9)
   {
   strcpy(suppcode,"S000");
   strcat(suppcode,itoa(supcode,s,20));
   }
   if (supcode>9 && supcode<=99)
   {
   strcpy(suppcode,"S00");
   strcat(suppcode,itoa(supcode,s,10));
   }
   if (supcode>=99 && supcode<=999)
   {
   strcpy(suppcode,"S0");
   strcat(suppcode,itoa(supcode,s,10));
   }
   if (supcode>=999 && supcode<=9999)
   {
   strcpy(suppcode,"S");
   strcat(suppcode,itoa(supcode,s,10));
   }
  }
  cout<<"The supplier code: "<<" "<<suppcode<<endl;
  cout<<endl<<"Enter the first name of the supplier: ";
     cin.get();
  cin.getline(suppfname,20);
   do
    {
         if (strlen(suppfname)==0)
     {
      cout<<endl<<"The first name of the supplier cannot be empty. Please enter the first name of the supplier. ";
  cin.getline(suppfname,20);
     }
     else
     {
      break;
     }
  } while(strlen(suppfname)==0);
  cout<<endl<<"Enter the last name of the supplier: ";
  cin.getline(supplname,20);
  do
    {
         if (strlen(supplname)==0)
     {
      cout<<"The last name of the supplier cannot be empty. Please enter the last name of the supplier.  ";
  cin.getline(supplname,20);
     }
     else
     {
      break;
     }
  } while(strlen(supplname)==0);
  cout<<endl<<"Enter the address of the supplier: ";
  cin.getline(suppaddress,20);
  do
    {
         if (strlen(suppaddress)==0)
     {
      cout<<"The address of the supplier cannot be empty. Please enter the address of the supplier."<<endl;
  cin.getline(suppaddress,20);
  }
     else
     {
      break;
     }
  } while(strlen(suppaddress)==0);
  cout<<endl<<"Enter the city name of the supplier: ";
  cin.getline(suppcity,20);
  do
    {
         if (strlen(suppcity)==0)
     {
      cout<<"The city name of the supplier cannot be empty. Please enter the city name of the supplier."<<endl;
  cin.getline(suppcity,20);
  }
     else
     {
      break;
     }
  } while(strlen(suppcity)==0);
  cout<<endl<<"Enter the country name of the supplier: ";
  cin.getline(suppcountry,20);
  do
    {
         if (strlen(suppcountry)==0)
     {
      cout<<"The country name of the supplier cannot be empty. Please enter the country name of the supplier."<<endl;
  cin.getline(suppcountry,20);
  }
     else
     {
      break;
     }
  } while(strlen(suppcountry)==0);
  cout<<endl<<"Enter the zip code of the supplier: ";
  cin.getline(suppzipcode,20);
  do
    {
         if (strlen(suppzipcode)==0)
     {
      cout<<"The zip code of the supplier cannot be empty. Please enter the zip code  of the supplier."<<endl;
  cin.getline(suppzipcode,20);
      }
     else
     {
      break;
     }
  } while(strlen(suppcity)==0);
  cout<<endl<<"Enter the email address of the supplier: ";
  cin.getline(suppemailaddress,20);
  if (strlen(suppemailaddress)==0)
     {
      cout<<endl<<"Do you want leave the e-mail address blank(y/n): ";
  cin>>r;
   if(r=='y')
   {
    strcpy(suppemailaddress," ");
        }
  }
     cout<<endl<<"Enter the contact number of the supplier: ";
    
  cin.getline(suppcontactnumber,20);
  do
    {
         if (strlen(suppcontactnumber)==0)
     {
      cout<<"The contact number of the supplier cannot be empty. Please contact number  of the supplier."<<endl;
  cin.getline(suppcontactnumber,20);
  }
     else
     {
      break;
     }
  } while(strlen(suppcontactnumber)==0);
  cout<<endl<<"Entering the following record in the Supplier module"<<endl;
  cout<<"____________________________________________________"<<endl<<endl;
  cout<<suppfname<<"  "<<supplname<<"  "<<suppaddress<<"  "<<suppcity<<" "<<suppcountry<<"  "<<suppzipcode<<"  "<<suppemailaddress<<"  "<<suppcontactnumber;
 fstream addsupp("Supplier.txt",ios::out|ios::app);
 addsupp.write( (char *)this, sizeof(class Supplier) );
 addsupp.close();
 cout<<endl<<endl<<"Do you want to enter another record (y/n)"<<endl;
 cin>>ch;
  }//closing the while loop.
 suppauto.close();
}//closing the adddetails function.

Recommended Answers

All 2 Replies

you will have to be a little more specific. Do you get compile errors? If yes, that are the errors?

> while(!suppauto1.eof())
1. You shouldn't use eof() in a while loop. You need to test the result of the read function itself.
2. The read function (being in the loop) reads to the same data area each time, so at best you end up with just the last record of the file.

3. Your indentation needs work. All statements at the same nesting level should have the same indent.

4. if (supcode<=9)
The next 20 lines can all be replaced with sprintf( suppcode, "S%05d", supcode ); > AddDetails()
Rather than making the function ramble on, focus on the specific task of creating a new record to add to the data.
Functions for reading the file, writing the file should be elsewhere.

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.