Thread: Functions that compares strings

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    4

    Functions that compares strings

    My problem is in the display skiers time function I am not sure how to compare strings.
    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    
    
    int displayFastestSkier(string name[], double time[])
    {
    int x=0;
    int fastestTime = 1000;
    for (x=0; x<5; x++)
    if (time[x] < fastestTime)
    {
    fastestTime = time[x];
    }
    return fastestTime;
    }
    
    
    
    
    int displayAvgTime(double time[])
    {
    int total, x, avg = total/5;
    time[x] += total;
    return avg;
    }
    
    
    int displaySkiersTime(string name[],double time[])
    {
    int x;
    string skier;
    for (x=0; x<5; x++)
    {
    if (skier != name[x])
    cout << "Name not found!" << endl;
    else if (skier == name[x])
    return name[x], time[x];
    }
    }
    
    
    int displaySkiersAndTimes(string name[],  double time[])
    {
        int x = 0;
    for (x=0; x<5; x++)
    {
       cout << "Name "<< "      " << "Time" << endl;
       cout << name[x] << "        " << time[x] << endl;
    }
    
    
    }
    
    
    
    
    
    
    int main()
    {
        int x = 0, option;
        const int SIZE = 5;
        double time[SIZE];
        string name[SIZE];
    
    
        for (x=0; x<SIZE; x++)
        {
           cout << "Enter the skier's name: " << endl;
           cin >> name[x];
           cout << "Enter the skier's time: " << endl;
           cin >> time[x];
    
    
        }
        //menu
        cout << endl;
        cout << "*************   MENU   *************" << endl;
        cout << "1: Find the fastest skier. " << endl;
        cout << "2: Calculate average time. " << endl;
        cout << "3: Find the time of a skier. " << endl;
        cout << "4: Display the skiers and thier times. " << endl;
        cout << "************************************" << endl;
        cout << "Enter choice";
        cin >> option;
        //logic
        if (option == 1)
        {
        cout << "Fastsest time is " << displayFastestSkier(name, time) << endl;
        }
        else if (option == 2)
        {
        cout << "The average time is " << displayAvgTime(time) << endl;
        }
        else if (option == 3)
        {
         cout << "Skier: " <<  name[x] << " Time: " << time[x] <<endl;
        }
        else if (option == 4)
        {
        displaySkiersAndTimes(name, time);
        }
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,364
    >> My problem is in the display skiers time function I am not sure how to compare strings.
    You are comparing them correctly in that function. That is not your problem. The good thing about the string type is that you can easily compare with != and ==.

    >> return name[x], time[x];
    This is not what you want.

    Also, think about all your functions named displayXXX. Are they all meant to display something? Or are some meant to lookup some value and return it? If it's the latter, maybe better naming will make things easier going forward.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    926
    Code:
    int displayFastestSkier(string name[], double time[])
        // How is 'name' used in this function?
    {
        int x = 0;
        int fastestTime = 1000; // Why 1000?
        for (x=0; x<5; x++) // Why 5?
        if (time[x] < fastestTime)
        {
            fastestTime = time[x];
        }
        return fastestTime;
    }
    Maybe this instead:

    Code:
    double fastestTime(const std::vector<double>& times)
    {
        return *std::max_element(times.begin(), times.end());
    }
    Last edited by CodeMonkey; 07-31-2016 at 10:46 PM. Reason: double return
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Algorithm that compares two images
    By a.mlw.walker in forum C++ Programming
    Replies: 21
    Last Post: 10-08-2012, 08:52 PM
  2. Replies: 2
    Last Post: 12-14-2009, 07:42 AM
  3. compares 2 arguments and replaces characters
    By help123 in forum C Programming
    Replies: 9
    Last Post: 04-14-2005, 09:22 AM
  4. Compares C++ and Pascal
    By Jaguar in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-19-2002, 11:28 AM

Tags for this Thread