Write a program that tests maxLoc(). You can read a data file into a list and call the function maxLoc to find the largest element.

HERE IS WHAT IS IN THE TEXT FILE(Name):
Josh
King
Louis
Tony
Angel


here is the code:

#include <iostream>
#include <fstream>
#include <list>
using namespace std;

// return an iterator pointing to the largest element in the list.
template <class T>
list<string>::iterator maxLoc(list<string>& aList)
{
    list<string>::iterator iter; //declare a variable
    list<string>::iterator max;

    //point to the begin of a list
    max = aList.begin();
    iter  = max;
    iter++;

    while(iter !=aList.end())
    (
        if( *max<*iter)
            max=iter;
        iter++;
        return max;
    )
}

int main()
{
    ifstream read("Name.txt");
    string Name;
    list<string> l;
    list<string>::iterator large, temp;
    temp=l.bein();
    while (read>>Name)
    (
        l.push_back(Name);
    )

    large= maxLoc(1)
    cout<<"Contents of the lists are: ";


    while(temp !=l.end())
    (
       cout<<*temp<< " ";
    )

system("pause"); 
return 0;

}

here is the errors:

List.cpp: In function `std::_List_iterator<std::string> maxLoc(std::list<std::string, std::allocator<std::string> >&)':
List.cpp:20: error: expected primary-expression before "if"
List.cpp:20: error: expected `)' before "if"
List.cpp:24: error: expected primary-expression before ')' token
List.cpp:24: error: expected `;' before ')' token

List.cpp: In function `int main()':
List.cpp:33: error: 'class std::list<std::string, std::allocator<std::string> >' has no member named 'bein'
List.cpp:36: error: expected `)' before ';' token
List.cpp:37: error: expected primary-expression before ')' token
List.cpp:37: error: expected `;' before ')' token
List.cpp:45: error: expected `)' before ';' token
List.cpp:46: error: expected primary-expression before ')' token
List.cpp:46: error: expected `;' before ')' token

make.exe: *** [List.o] Error 1

Execution terminated

Correction:

while(iter !=aList.end())
    {
        if( *max<*iter)
            max=iter;
        iter++;
        //return max;
    }
 return max;

Correction:

while(iter !=aList.end())
    {
        if( *max<*iter)
            max=iter;
        iter++;
        //return max;
    }
 return max;
#include <iostream>
#include <fstream>
#include <list>
using namespace std;

// return an iterator pointing to the largest element in the list.
template <class T>
list<string>::iterator maxLoc(list<string>& aList)
{
    list<string>::iterator iter; //declare a variable
    list<string>::iterator max;

    //point to the begin of a list
    max = aList.begin();
    iter  = max;
    iter++;

    while(iter !=aList.end())
    {
        if( *max<*iter)
            max=iter;
        iter++;
        //return max;
    }
 return max;
}

int main()
{
    ifstream read("Name.txt");
    string Name;
    list<string> l;
    list<string>::iterator large, temp;
    temp=l.begin();
    while (read>>Name)
    (
        l.push_back(Name);
    )

    large= maxLoc(1)
    cout<<"Contents of the lists are: ";


    while(temp !=l.end())
    (
       cout<<*temp<< " ";
    )

system("pause"); 
return 0;

}

Thanks. I still got errors.
I"C:/Dev-Cpp/include"

List.cpp: In function `int main()':
List.cpp:37: error: expected `)' before ';' token
List.cpp:38: error: expected primary-expression before ')' token
List.cpp:38: error: expected `;' before ')' token

List.cpp:46: error: expected `)' before ';' token
List.cpp:47: error: expected primary-expression before ')' token
List.cpp:47: error: expected `;' before ')' token

make.exe: *** [List.o] Error 1

Execution terminated

Again more corrections :

while (read>>Name)
    {
        l.push_back(Name);
    {
//...

  while(temp !=l.end())
    {
       cout<<*temp<< " ";
    {

For loops you use the curly braces not the parenthesis.

#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;

// return an iterator pointing to the largest element in the list.
template <class T>
list<string>::iterator maxLoc(list<string>& aList)
{
    list<string>::iterator iter; //declare a variable
    list<string>::iterator max;

    //point to the begin of a list
    max = aList.begin();
    iter  = max;
    iter++;

    while(iter !=aList.end())
    {
        if( *max<*iter)
            max=iter;
        iter++;
        //return max;
    }
 return max;
}

int main()
{
    ifstream read("Name.txt");
    string Name;
    list<string> l;
    list<string>::iterator large, temp;
    temp=l.begin();
    while (read>>Name)
    {
        l.push_back(Name);
    }

    large = maxLoc(1);
    cout<<"Contents of the lists are: ";


    while(temp !=l.end())
    {
       cout<<*temp<< " ";
    }

system("pause"); 
return 0;

}

ERRORS:
In function `int main()':
no matching function for call to `maxLoc(int)'

NEW UPDATE:

#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;

// return an iterator pointing to the largest element in the list.
template <class T>
list<string>::iterator maxLoc(list<string>& aList)
{
    list<string>::iterator iter; //declare a variable
    list<string>::iterator max;

    //point to the begin of a list
    max = aList.begin();
    iter  = max;
    iter++;

    while(iter !=aList.end())
    {
        if( *max<*iter)
            max=iter;
        iter++;
    }
 return max;
}

int main()
{
    ifstream read("Name.txt");
    string Name;
    list<string>l;
    list<string>::iterator large, temp;
    temp=l.begin();
    while (read>>Name)
    {
        l.push_back(Name);
    }

    large = maxLoc(l);
    cout<<"Contents of the lists are: ";


    while(temp !=l.end())
    {
       cout<<*temp<< " ";
    }

system("pause"); 
return 0;

}

ERRORS:
no matching function for call to `maxLoc(std::list<std::string,
std::allocator<std::string> >&)'

Take out the template<class T> in line 8.

Take out the template<class T> in line 8.

if I do that than I have nothing after I compile.

this is what i was supposed to do:

Implement the function maxLoc(), which return an iterator pointing at the largest element in a list.

// return an iterator pointing to the largest element in the list.
template <class T>
list<T>::iterator maxLoc(list<T>& aList);

Write a program that tests maxLoc(). You can read a data file into a list and call the function maxLoc to find the largest element.

Why doesn't your function prototype look like this then :

template <class T>
list<T>::iterator maxLoc(list<T>& aList);

we were told to change the T to string

What does it mean to say *max > *iter ? This is comparing two strings and asking which is greater. I'm not sure that you can do this. What would be the result of "hello" > "Monday", for example? Do you need to use (*max).length() > (*Iter).length() instead?

Also, I think that ++iter is more efficient than iter++ , for some reason to do with the compiler creating tempory copies of things.

This code for the main function worked:

int main()
{
    ifstream read("Name.txt");
    string Name;
    list<string> names; // List used to store all the names
    // Read the names in the list
    while (read >> Name)
    {
        names.push_back(Name);
    }

    cout<< "Contents of the lists are:\n\n";

    list<string>::iterator it = names.begin();
    for(; it != names.end(); ++it) // Write them
    {
        cout << *it << endl;
    }
    
    list<string>::iterator large = maxLoc(names); // Get the longest string
    cout << endl;
    cout << "Longest String: " << *large << endl;

    cin.get();
    return 0;
}

Also I changed the maxLoc() function:

list<string>::iterator maxLoc(list<string>& aList)
{
    list<string>::iterator iter;
    list<string>::iterator max;

    // point to the begin of a list
    max = aList.begin();
    iter  = max;
    iter++;

    while(iter !=aList.end())
    {
        if( max->length() < iter->length())
            max=iter;
        iter++;
    }
 return max;
}

Don't declare variables unless you want to use them to make the code more readable, and also give your variables obvious names to indicate their usage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.