Thread: Higest and Lowest

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Few arguments
    By caroundw5h in forum C Programming
    Replies: 8
    Last Post: 07-22-2004, 11:08 PM