|
-
January 13th, 2007, 03:08 PM
#1
Uni Help!
i am doing very basic c++ at uni but i am finding it quite difficult!
i have an assessment but i am slightly stuck on a part of it!
what i need to do is create a basic sales program. what it needs to do is read a file, write to a file, find highest and lowest data! i have managed to get to the reading of the file but finding the highest and lowest sales figure i am struggling on!
this is the code i have so far!
Code:
#include <iostream>
#include <fstream>
using namespace std;
int sales_data();
int save_data();
int main()
{
int num;
cout << "MAIN MENU" << endl;
cout << "\nEnter a number between 1 and 3" << endl;
cout << "\n-----------------------------" << endl;
cout << "1. View Sales Details" << endl;
cout << "2. Export and Save Sales Details" << endl;
cout << "3. Exit" << endl;
cout << "-----------------------------" << endl;
cin >> num;
switch(num)
{
case 1:
system("CLS");
sales_data();
break;
case 2:
system("CLS");
save_data();
break;
case 3:
system("exit");
break;
default:
cout << "Incorrect Number" << endl;
break;
}
}
int sales_data()
{
int num;
int depid;
double sales;
ifstream salesfile ("ica_sales.txt");
if (salesfile.is_open())
{
cout << "SALES DATA\n" << endl;
while (! salesfile.eof() )
{
salesfile >> depid >> sales;
cout << depid << "\t" << sales << endl;
}
salesfile.close();
}
else cout << "Unable to open file" << endl;
cout << "\nSALES MENU" << endl;
cout << "\nEnter a number between 1 and 4" << endl;
cout << "\n-----------------------------" << endl;
cout << "1. View Highest Department" << endl;
cout << "2. View Lowest Department" << endl;
cout << "3. Back to main" << endl;
cout << "4. Exit" << endl;
cout << "-----------------------------" << endl;
cin >> num;
switch(num)
{
case 1:
break;
case 2:
break;
case 3:
system("CLS");
main();
break;
case 4:
system("exit");
break;
default:
cout << "Incorrect Number" << endl;
break;
}
return 0;
}
int save_data()
{
int num;
cout << "Enter a number between 1 and 4" << endl;
cout << "-----------------------------" << endl;
cout << "1. Dont no what i need in here yet!!!" << endl;
cout << "2. Back to Main" << endl;
cout << "3. Exit" << endl;
cout << "-----------------------------" << endl;
cin >> num;
switch(num)
{
case 1:
break;
case 2:
system("CLS");
main();
break;
case 3:
system("exit");
break;
default:
cout << "Incorrect Number" << endl;
break;
}
return 0;
}
---------------------------
this is what the text file looks like:-
Code:
01 633.79
02 2242.03
06 4587.12
08 2318.98
05 565.32
07 865.99
03 6591.96
04 1974.90
09 453.33
what i need to do is display the highest and the lowest figures (which are going to change regulary)!
Cheers peachy
srry if its a bit confusing
Last edited by cilu; January 14th, 2007 at 06:31 AM.
Reason: code tags
-
January 14th, 2007, 06:32 AM
#2
-
January 14th, 2007, 06:37 AM
#3
Re: Uni Help!
What you have to do is reading the values in a vector or something, and then sort the vector according to some logic, like the greater sale.
Code:
struct Data
{
int depid;
double sales;
Data():depid(0), sales(0) {}
};
bool is_lower(const Data& d1, const Data& d2)
{
return d1.sales < d2.sales;
}
std::vector<Data> listData;
// ...
while (! salesfile.eof() )
{
Data data;
salesfile >> data.depid >> data.sales;
listData.push_back(data);
}
// ...
// and now sort the vector
std::sort(listData.begin(), listData.end(), is_lower);
For comparing floating point values, see this FAQ.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|