Higest and Lowest

This is a discussion on Higest and Lowest within the C++ Programming forums, part of the General Programming Boards category; i have a text file that i am reading from! This is what the text file looks like:- 01 633.79 ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    2

    Higest and Lowest

    i have a text file that i am reading from! This is what the text file looks like:-

    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

    i have manager to read from the file and get it to display but now i need some code, so i can view just the highest or just the lowset!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    if ( current > max ) max = current;

    Just do that as you loop through your array of values.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    2
    i am not too sure what you mean because i am very new to c++! i only started doing it about 3 months ago! this is my university project! Maybe if i show you my code you might beable to giv me more help!

    Code:
    #include <iostream> 
    #include <fstream> 
    
    using namespace std; 
    
    int sales_data();   //Sales data function
    int save_data();   // Save data function
    
    int main() 
    { 
        int num; 
        
        cout << "MAIN MENU" << endl;        //Main Menu
        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;    //Department ID numbers
       double sales; //Sales Figures
       
       ifstream salesfile ("ica_sales.txt");   //Open sales file
             if (salesfile.is_open())
             {           
             cout << "SALES DATA\n" << endl;           
             while (! salesfile.eof() )
             {      
             salesfile >> depid >> sales;
             cout << depid << "\t" << sales << endl; //Output department IDs and sales figures
             }
             salesfile.close();
             }
             else cout << "Unable to open file" << endl;   
       
       cout << "\nSALES MENU" << endl; // Sales Menu
       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; 
    }
    Last edited by peachy900757; 01-14-2007 at 03:14 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    > salesfile >> depid >> sales;
    Maybe create a struct containing deptID and sales?

    Maybe create an array of that struct and read the file into that array (or better yet, use a std::vector)

    You're behind the curve IMO if you don't know about structures and arrays after 3 months.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Few arguments
    By caroundw5h in forum C Programming
    Replies: 8
    Last Post: 07-23-2004, 12:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21