i posted this question and i got replies which solved my problem... partly
here is the link
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20646266.htmli am bringing this up because i deparately need a solution.
thanks for all ur efforts..
i tried to impliment reborn_assassin's program in to my program which is below
--------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----
// AUTHOR--SAM
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
//include <stdlib.h>
fstream datafile;
// declaring the file object so that it can communicate with the operating system
int max=0,min=9999,i;
float ave,sum2;
long sum=0;
int* contents; // declares variable to read data from file
char filename[16],words[80];
double input;
int keypress,anykey, count=0;
void exit_prog()
{
cout<<"program terminated"<<endl;
}
//------------------- Welcom Screen Function------------------
-//
int welcome()
{
gotoxy(15,5);
cout<<"*******************
**********
**********
*";
gotoxy(15,6);
cout<<"*\t\t ------- COLLEGE\t *";
gotoxy(15,7);
cout<<"*\t FILE PROCESSING SYSTEM *";
gotoxy(15,8);
cout<<"*\t\t PRESS C TO CONTINUE\t *";
gotoxy(15,9);
cout<<"* *";
gotoxy(15,10);
cout<<"*******************
**********
**********
*";
gotoxy(34,9);
keypress=getche();
stop:
return 0;
}
//----------------------Me
nu Screen Function------------------
----//
int menu()
{ clrscr();
gotoxy(15,5);
cout<<"*******************
**********
**********
*"<<endl;
cout<<"\t *\t\tFile Processing System\t *"<<endl;
cout<<"\t * \t Please input option *"<<endl;
cout<<"\t * \t *"<<endl;
cout<<"\t * 1----------Create new data file *"<<endl;
cout<<"\t * 2----------Input data to a file *"<<endl;
cout<<"\t * 3----------View numbers form a file *"<<endl;
cout<<"\t * 4----------Display maximum\t *"<<endl;
cout<<"\t * 5----------Display minimum\t *"<<endl;
cout<<"\t * 6----------Display total\t *"<<endl;
cout<<"\t * 7----------Display the Average value *"<<endl;
cout<<"\t * 8----------Ascending sort \t *"<<endl;
cout<<"\t * 9----------EXIT \t\t *"<<endl;
cout<<"\t * \t *";
gotoxy(15,18);
cout<<"*******************
**********
**********
*"<<endl;
gotoxy(34,8);
keypress=getche();
return 0;
}
//-------------OPTION 1 CREATE FILE----------------------
-----//
int create_file()
{
// asking user to input name of the file to create
cout<<"please enter the name of the file that you wish to create"<<endl;
cout<<"use .txt as your file extension\n";
cin>>filename;
cout<<"\nA file named "<<filename<<" was created"<<endl;
datafile.open(filename,ios
::out);
datafile.close(); // close file
return 0;
}
//-------------OPTION 2 INPUT DATA TO A FILE----------------------
-----//
int input_data()
{
// asking user to input name of the file to input data to
cout<<"please enter the name of the file that you wish to input data to"<<endl;
cout<<"please use the correct extension\n";
cin>>filename;
do
{
count++;
datafile.open(filename,ios
::app);
clrscr();
cout<<"input number";
cin>>input;
datafile<<input<<endl;
datafile.close();
cout<<("do you want to input another number... press anykey to continue or N to exit\n");
keypress=getch();
}while(keypress!=110&&keyp
ress!=78);
clrscr ();
cout<<"You entered "<<count<<" numbers\n"<<endl;
return 0;
}
//-------------OPTION 3 VIEW DATA FORM A FILE----------------------
-----//
int view_file()
{
// asking user to input name of the file from which the data needs to reviewed
cout<<"please enter the name of the file which you wish to view the contents"<<endl;
cout<<"please use the correct extension\n";
cin>>filename;
datafile.open(filename,ios
::in); // open numbers.txt file for reading
while(!datafile.eof()) // keeps reading till the end of file
{
datafile.getline(words,80,
'\n'); // read character by character form file ito words
cout<<words<<endl; // displays characters on the screen
};
datafile.close(); // close file
return 0;
};
//-------------OPTION 4 Maximum-------------------
--------//
int maximum()
{ contents= new int[count];
datafile.open(filename,ios
::in); // opens numbers.txt file for reading
// displaying the contents of the file
for(i=0;i<count;i++) // keeps reading till the end of file
{
datafile>>contents[i]; // read character by character form file ito words
cout<<contents[i]<<endl; // displays characters on the screen
};
// find and display max
for(i=0;i<count;i++)
{
if(contents[i]>max)
max=contents[i];
}
cout<<"max is "<<max<<"\n";
datafile.close();
return 0;
}
//-------------OPTION 5 MINIMUM-------------------
--------//
int minimum()
{ contents= new int[count];
datafile.open(filename,ios
::in); // opens numbers.txt file for reading
// displaying the contents of the file
for(i=0;i<count;i++) // keeps reading till the end of file
{
datafile>>contents[i]; // read character by character form file ito words
cout<<contents[i]<<endl; // displays characters on the screen
};
//find and display min
for(i=0;i<count;i++)
{
if(contents[i]<min)
min=contents[i];
}
cout<<"min is "<<min<<endl;
datafile.close(); // close file
return 0;
}
//-------------OPTION 6 TOTAL---------------------
------//
int total()
{
contents= new int[count];
datafile.open(filename,ios
::in); // opens numbers.txt file for reading
// displaying the contents of the file
for(i=0;i<count;i++) // keeps reading till the end of file
{
datafile>>contents[i]; // read character by character form file ito words
cout<<contents[i]<<endl; // displays characters on the screen
};
// calcualte and display sum
for(i=0;i<count;i++)
sum=sum+contents[i];
cout<<"sum is "<<sum<<"\n";
datafile.close(); // close file
return 0;
}
//-------------OPTION 7 AVERAGE-------------------
--------//
int average()
{ contents= new int[count];
datafile.open(filename,ios
::in); // opens numbers.txt file for reading
// displaying the contents of the file
for(i=0;i<count;i++) // keeps reading till the end of file
{
datafile>>contents[i]; // read character by character form file ito words
cout<<contents[i]<<endl; // displays characters on the screen
};
// calclate and display average
sum=sum2;
ave=sum2/count;
cout<<"average is "<<ave<<endl;
datafile.close(); // close file
return 0;
}
//-------------OPTION 8 SORT----------------------
-----//
int sort()
{
char ans;
contents= new int[count];
int tp, i, j, total;
total = sizeof(contents)/4; // Meaning you can change the size of the array
for (i=1; i<=total-1; i++)
{
for (j=i+1; j<=total; j++)
{
if (contents[i] > contents[j])
{
tp = contents[i];
contents[i] = contents[j];
contents[j] = tp;
}
}
}
for (i=1; i<=total; i++)
cout << endl << contents[i];
cin >> ans;
return 0;
}
//-------------MAIN PROGRAM-------------------
--------//
int main()
{
welcome();
while(keypress!=67||keypre
ss!=99)
{
if (keypress==99||keypress==6
7)
{
menu();
while(keypress!=49||keypre
ss!=50||ke
ypress!=51
||keypress
!=52||keyp
ress!=53||
keypress!=54||keypress!=55
||keypress
!=56||keyp
ress!=57)
{
if(keypress==49||keypress=
=50||keypr
ess==51||k
eypress==5
2||keypres
s==53||
keypress==54||keypress==55
||keypress
==56||keyp
ress==57)
{
if(keypress==49)
{ //----- OPTION 1------//
clrscr();
create_file();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==50)
{ //----- OPTION 2------//
clrscr();
input_data();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==51)
{ //----- OPTION 3------//
clrscr();
view_file();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==52)
{ //----- OPTION 4------//
clrscr();
maximum();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==53)
{ //----- OPTION 5------//
clrscr();
minimum();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==54)
{ //----- OPTION 6------//
clrscr();
total();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==55)
{ //----- OPTION 7------//
clrscr();
average();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==56)
{ //----- OPTION 8------//
clrscr();
sort();
cout<<"\n\n\t\t\tPress Any Key To Return to Main Menu";
anykey=getch();
}
if(keypress==57)
{ //----- OPTION 9------//
clrscr();
cout<<"\a"<<endl;
cout<<"Thank you for using our program"<<endl;
cout<<"We hope you enjoyed it";
cout<<"\n\n\t\t\tPress Any Key exit the program";
anykey=getch();
break;
//exit(0);
}
}
cout<<"\a";
clrscr();
menu();
}// end while of menu validation
}
cout<<"\a";
clrscr();
welcome();
}//end while of welcome validation
return 0;
}
--------------------------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----------
----
there are 3 problem with my program...
1) the sort code provided by reborn_Assasin---- i ttried but i could'nt integrate his proram into mine.. i tried to look for the problem but iam having difficulty.
2) the exit function dosent work.. in the opt9.. i tried to puta break; but it only goes back to the welcome screenn. .
also i tried to put exit(0); there fore included stdlib.h as header.. but for no apparent reason it gives me 9 errors.. . please could soemone provide me with a solution for this.
3) the average option---. initially my ave was an int .which gave me an int value. .but when i changed it to float .all it gives me is 0..(i wanted a decimal value)
please help
thank you..