Note: You will have to include vector for my examples to work. For example,
#include <vector>
Exceter
Main Topics
Browse All Topicsi posted this question and i got replies which solved my problem... partly
here is the link
http://www.experts-exchang
i 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
int menu()
{ clrscr();
gotoxy(15,5);
cout<<"*******************
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<<"*******************
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
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
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
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
while(!datafile.eof()) // keeps reading till the end of file
{
datafile.getline(words,80,
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
// 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
// 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
// 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
// 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
{
if (keypress==99||keypress==6
{
menu();
while(keypress!=49||keypre
keypress!=54||keypress!=55
{
if(keypress==49||keypress=
keypress==54||keypress==55
{
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..
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
If you are getting errors similar to these,
Error E2356 C:\CPP\BigNumbers\BigInteg
Error E2449 C:\CPP\BigNumbers\BigInteg
Error E2356 C:\CPP\BigNumbers\BigInteg
Error E2449 C:\CPP\BigNumbers\BigInteg
the problem is that you have created global varaibles named min and max which are conflicting with std::max and std::min. To fix this, you should rename them.
Exceter
hey Exceter!!
sorry for the late reply
i tried all ur above solutions
the average and exit functions are working fine.. thank you..
the average function works fine but..when compiled it gives me
1.)>>>> no module definition file specified:using defaults
at this line >>>>>>> count = sum = 0;
should i just ignore it..... or ....???
2.) whenever i try to compile with ur suggested sort program .. it gives me error messages..
this using namespace std; .. its not working with my compiler(it gives me syntax error mssg)..
and also that it cannot locate the vector.h header file.. its nowhere.. i tried searching for it by myself but i couldnt find it.
i tried putting #include<vector> as #include<vectro.h> with out namespace std.. but it says cant open/locate the header file
iam using borland c++ 4.52
i search for the vector.h on the net.. found it.. put it in header folder.. compiled it..
and it says " unable to open include file... matrix.h>
then i searched for it....... and compiled it now it wants GML.h and istream
what in the world is goin on.......
!£"$"£%^£"^£^£%^£$^£&(*^&
please advice......
thank you.
Sure, I can modify that example to use a bubblesort, which you were using before. For example,
int sort()
{
datafile.open(filename,ios
int* data = new int[100]; // If you know the size you will need, substitute 100 for that value
int num = 0, temp;
if( !datafile )
cout << "Error! The file was not found." << endl;
else
{
cout << "Before sort," << endl;
while( datafile >> data[num] )
{
cout << data[num] << endl;
num++;
}
cout << endl << "After sort," << endl;
for( int x = 0; x < num; x++ )
{
for( int y = 1; y < num; y++ )
{
if( data[y] < data[y - 1] )
{
temp = data[y];
data[y] = data[y - 1];
data[y - 1] = temp;
}
}
}
for( int x = 0; x < num; x++ )
cout << data[x] << endl;
}
delete[] data;
datafile.close();
return 0;
}
However, your problem with my examples is that your compiler appears to be an older version of Borland's C++ compiler which does not support the current standard. If this is the case, I would suggest you get a different compiler. If you decide to follow this path, you can download Borland's C/C++ v5.5 compielr for free.
Exceter
thanks man
the sort functiom works like a charm ;)
one last thing
could u please explain ur sort funciton's these line to me
for( int x = 0; x < num; x++ )
{
for( int y = 1; y < num; y++ )
{
if( data[y] < data[y - 1] )
{
temp = data[y];
data[y] = data[y - 1];
data[y - 1] = temp;
}
}
oh and what is the syntax to flush the contents of any array
eg. .this
int* data = new int[100];
or this
contents= new int[count];
how can i flush their data.. so when the loop starts again it is fresh...
once again thank you for all ur effort.. i really appreciate it :)
sam
>> for( int x = 0; x < num; x++ )
>> {
>> for( int y = 1; y < num; y++ )
>> {
>> if( data[y] < data[y - 1] )
>> {
>> temp = data[y];
>> data[y] = data[y - 1];
>> data[y - 1] = temp;
>> }
>> }
>> }
The first loop iterates once for every element in the array. The second loop, which runs every time the other loop iterates, starts at index 1 and continues looping until it has reached the last data element. Aloing the way, it compares the current value to the previous value. If the previous value is greater than the current value, they are swapped. Therefore, we can be assured that once the first loop iterates once, that the largest value has been moved to the highest index. On the second iteration of the first loop we can be ceratin that the second largest value has been sorted. Therefore, the sort can be optimized by saying,
for( int x = 0; x < num; x++ )
{
for( int y = 1; y < num - x; y++ )
{
if( data[y] < data[y - 1] )
{
temp = data[y];
data[y] = data[y - 1];
data[y - 1] = temp;
}
}
}
>> oh and what is the syntax to flush the contents of any array
>> eg. .this
>> int* data = new int[100];
>> or this
>> contents= new int[count];
You have to manually reset their values. You could reload the data from disk or you could simply assign an arbitrary value to each element. For example, to set all elements to the same value,
for( int x = 0; x < count; x++ )
contents[x] = 0;
or,
for( int x = 0; x < 100; x ++ )
data[x] = 0;
If you want the data read from disk again, simply repeat the process I used in the beginning of the sort function.
Exceter
Business Accounts
Answer for Membership
by: ExceterPosted on 2003-06-15 at 13:48:12ID: 8727987
You should consider using a global namespace instead of the .h headers as they are not standard. For example, instead of,
::in);
::in);
>> #include <iostream.h>
>> #include <fstream.h>
>> #include <conio.h>
you should say,
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
>> 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.
If you really want a sort you should use std::sort as it is easier to use. For example,
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
vector <int> data;
unsigned int x;
srand( (unsigned)time( NULL ) );
for( x = 0; x < 15; x++ ) // Fill the vector with 15 random values
data.push_back( rand() );
sort( data.begin(), data.end() ); // Sort the vector
for( x = 0; x < data.size(); x++ ) // Display the vector
cout << data[x] << endl;
return 0;
}
The problem with your current sort is that you are creating a new array inside the sort function and you are not populating it with the data from the file. I would suggest you change the sort function to look more like this,
int sort()
{
datafile.open(filename,ios
vector <int> data;
int num;
if( !datafile )
cout << "Error! The file was not found." << endl;
else
{
cout << "Before sort," << endl;
while( datafile >> num )
{
cout << num << endl;
data.push_back( num );
}
cout << endl << "After sort," << endl;
sort( data.begin(), data.end() );
for( unsigned int x = 0; x < data.size(); x++ )
cout << data[x] << endl;
}
datafile.close();
return 0;
}
>> 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.
Strange, exit(0); should be working. However, the code for option 9 is in main, the following will work.
if(keypress=='9')
{ //----- 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();
return 0;
}
What errors was it giving you?
>> 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)
This feature will give you an access violation if you do not specify a datafile, you have a memory leak inside the average function, and the reason you are not getting the averge is simply that you are not tallying up the values you read from the file. Therefore, the average function should look more like this,
int average()
{
datafile.open(filename,ios
count = sum = 0;
if( !datafile )
cout << "Error! The file was not found." << endl;
else
{
while( datafile >> sum2 )
{
cout << sum2 << endl;
sum += sum2;
count++;
}
ave = (float)sum / (float)count;
cout << endl << "The average is: " << ave << endl;
}
datafile.close();
return 0;
}
Cheers!
Exceter