Jump to content

Help needed With files in C++..

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
paul2cool

paul2cool

    Newbie

  • Members
  • Pip
  • 1 posts
Can anyone modify the following code ,so that it uses files to store information about the "members" as well as display information from the file ,I dont know how to do this ,pls pls help...
THE code(it is error free):


#include<iostream.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<bios.h>

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

#include<fstream.h>

#include<ctype.h>


int menu(int,int,int,char a[25],char b[25],char c[25],char d[25],char e[25],char sl1[25],char sl2[25],char title[40],char under[2]);

void main_menu();

void main();

int records=0;

struct s_date

{

int dd,mm,yyyy;

};


//DEFINING OF Stuctures

struct member

{

int redgno;

s_date dob;

char sex[6];

char name[20];

char mt_code[10];

int amountpaid;


}m[10];


/****************************Function To Add Data***************************/

void add()

{

int i=0;

char ch='y';

char fn[20];


do

{

records++;

ss:

clrscr();

int st;

int t_redg,mark;


cout<<"Members Data Collector";


cout<<"=======================";


cout<<"Enter The Following";


cout<<"Registaration No:";gotoxy(36,8);cin>>t_redg;

m[i].redgno=t_redg;


cout<<"Name :";gotoxy(35,10);gets(m[i].name);


cout<<"Sex :";gotoxy(35,12);gets(m[i].sex);



cout<<"Date Of Birth :DD MM YYYY";gotoxy(35,18);

cin>>m[i].dob.dd>>m[i].dob.mm>>m[i].dob....


sa:

clrscr();


cout<<"Member Data Entry Form";


cout<<"=======================";


cout<<"Enter The Membership type: \n1.Platinum membership \n2. Premium membership \n3. Gold membership";


cin>>st;

switch(st)

{

case 1:strcpy(m[i].mt_code,"Platinum Membership");

break;

case 2:strcpy(m[i].mt_code,"Premium Membership");

break;

case 3:strcpy(m[i].mt_code,"Gold Membership");

break;

default:cout<<"\n\t\t\tInvalid Type.\n\nDo you want to continue?(Y/N)...";

cin>>ch;

if (ch=='y'||ch=='Y')

{

clrscr();

goto sa;

}

else add();

}


clrscr();


cout<<"Member Data Entry Form";


cout<<"=======================";


cout<<"Enter The Amount paid by the Member";



cin>>m[i].amountpaid;



i++;


cout<<"Want More Record To Be Written?(Y/N)......";

cin>>ch;

}while(ch=='y'||ch=='Y');

if(ch=='n'||ch=='N') {



main();

}

}

/****************Member View Information******************

**************************************...

void view()

{

int opt,i,reg,flag=0;

char n[20],ch;

view:

cout<<"1.View All"<<" 2.Search Reg No"<<" 3.Back";

cin>>opt;

switch(opt)

{

case 1: //Shows All Member Data

for(i=0;i<10;i++)

{

clrscr();

if(m[i].redgno!=NULL)

{

cout<<" Report Of Member"<<m[i].name<<"\nRegistration Number:"<<m[i].redgno<<"\nDate Of Birth:"<<m[i].dob.dd<<"-"<<m[i].dob.mm<<... type:"<<m[i].mt_code<<"\n Amount Paid:"<<m[i].amountpaid<<"\nSex :"<<m[i].sex;

cout<<"\n\nPress Any Key To Continue..";

getch();

}

else break;

}


break;

case 2:clrscr(); //Search By Reg No

cout<<"\n\n\tEnter Number To Be Searched:";cin>>reg;

for(i=0;i<10;i++)

{

if(m[i].redgno==reg)

{

cout<<" Report Card Of "<<m[i].name<<"\nRegistration Number:"<<m[i].redgno<<"\nDate Of Birth:"<<m[i].dob.dd<<"-"<<m[i].dob.mm<<... type:"<<m[i].mt_code<<"\n Amount Paid:"<<m[i].amountpaid<<"\nSex :"<<m[i].sex;

flag=1;

getch();

}

}

if(flag==0)

{

cout<<"Record Not Found";

getch();

view();

}

break;


case 3:main(); //Exit

default:main();

}

goto view;

}

/* ***************************DELETE */

void deletemem()

{int t_redg,flag=0;

char na[20];


//Delete Member

cout<<"Which Member Do you want to Delete\nEnter Reg No:";

cin>>t_redg;

for(int i=0;i<10;i++)

{

if(m[i].redgno==t_redg)

{

strcat(na,m[i].name);

strcat(na,".txt");

if(remove(na)) cout<<"The Record of "<<m[i].name<<" is removed.";

else cout<<"Internal Error. "<<m[i].name<<" could not be removed.";

m[i].redgno = -1;

flag=1;

break;

}

}

if(flag==0)

cout<<"Record not found";

main();

}

/* ******************Modify Member Information********/

void modifymember()

{member modify;

char ch,membertype[10];

int rg,flag=0,i;

cout<<"Enter the registration no. of the member to be modified";


cin>>rg;

for(i=0;i<10;i++)

{

if(m[i].redgno==rg)

{

flag=1;

cout<<"Modify Data of the member";

puts(m[i].name);

cout<<"\nMembership Type :";

puts(m[i].mt_code);

cout<<"\nDo you want to change this?(y/n)";

cin>>ch;

if(ch=='y')

{

cout<<"\nEnter New Value:";


gets(m[i].mt_code);

}


cout<<"Amount Paid by the member : "<<m[i].amountpaid;

cout<<"Do you want to change this (y/n)";

cin>>ch;

if(ch=='y')

{ cout<<"\nEnter New Value:";

cin>>modify.amountpaid;

m[i].amountpaid=modify.amountpaid;

}

}

}

if(flag==0)

cout<<"Invalid Reg no.";


}


/*************************MainMenu ****************************************...

void main()

{ clrscr();

int ch;

cout<<"Members data collector testing"<<'\n';

cout<<"Do you want to "<<"\n 1.collect data"<<"\n2. View Member data"<<"\n 3. Delete Existing members"<<"\n 4.Modify Existing Members"<<"\n 5.Exit if you want"<<"\n (beta version) ";

cin>>ch;

if(ch==1)

add();

else

if(ch==2)

view();

else

if(ch==3)

deletemem();

else

if(ch==4)

modifymember();

if(ch==5)

cout<<"Exiting...";

delay(1000);

exit(0);

}



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Do you know how to create an fstream? If so, just create one and use it instead of cout.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
moon reflection

moon reflection

    Newbie

  • Members
  • Pip
  • 4 posts
Hi paul , in order to solve this , you need to know how to creat file I/O in c++ .
first include <fstream> library , then you have to declare the input and output files through classes ifstream (inputfile) and ofstream (outputfile), example :

ifstream inputfile("inputFile.dat",ios::in);
ofstream outputfile("outputFile.dat", ios::out);

(note that you will creat the input file by writing the information in the notepab and save it as .dat ,so inside the program you will modify those information and display them in the output file .

also thier is another way , instead of creating the input file by yourself , you can do this inside the program by using this code :
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}
you can check this link

cplusplus.com/doc/tutorial/files/


so I can that the input and output files work as the database of our program .

I hope this will help you .......

thanks.



Edited by WingedPanther, 02 June 2009 - 06:09 PM.
add code tags, reduce size to readable level.