I cant seem to get this piece of code to work properly i am not sure how the iterators work
i am looking for the smallest number but i can't seem to get it
#include <iostream>
#include <string>
#include <list>
usingnamespace std;
void sort(list<int>&);
int main()
{
list<int> numbers;
numbers.push_back(3);
numbers.push_back(6);
numbers.push_back(5);
numbers.push_back(9);
sort(numbers);
list < int>::iterator pos = numbers.begin();
for (pos = numbers.begin(); pos != numbers.end(); pos++)
{
cout << *pos;
cout << endl;
}
cout << "ran:" << endl;
return 0;
}
void sort(list<int>& numbers)
{
list < int>::iterator pos = numbers.begin();
list < int>::iterator second;
list < int>::iterator third;
int smallest = *pos; // the first position of the list is the smallest so far
int counter = 0; // counter is used to swap locations with the pointer when the smallest is found
int total = numbers.size();
pos++;
second = pos;
while (counter < total)
{
for (pos = numbers.begin(); pos != numbers.end(); pos++) // loop to go through the list
{
int current = *pos;
cout << current << smallest << endl;
if (current < smallest);// if the current values is smaller then the current smallest that is now the smallest
{
smallest = current;
}
cout << smallest << endl;
}
counter++;
}
}
You have a semicolon after your if statement on line 54; you probably do not intend it to be there. It is causing 55~57 to be always executed rather than being the body of the if statement.