Search code examples
c++vectorbubble-sort

Problems With linesearch and Bubblesort


Im making a program in c++ that takes a few persons puts them in a vector with name and age and i got all the code down for it but i cant get it to compile properly.

this is my code so far

#include <iostream>
#include <string>

using namespace std;

class person
{
public:
    string name;
    int age;
    void SetInfo(const string _name, int _age) //Här läggs den viktigaste informationen in
    {
        name = _name;
        age = _age;
    }
    int getAge(){ return age; }
};


void bubblesort(person mylist[], int n) // Här startar Bubblesort funktionen
{
    for (int i = 1; i<n; i++)
    {

        for (int j = 0; j<i - 1; j++)
        {
            if (mylist[j].getAge() > mylist[j + 1].getAge())
            {
                person temp;
                temp = mylist[j];
                mylist[j] = mylist[j + 1];
                mylist[j + 1] = temp;
            }
        }
    }
}
int main() //Program start
{
    person mylist[4]; //lista på personer
    mylist[0].SetInfo("Johan", 25);
    mylist[1].SetInfo("Nathalie", 20);
    mylist[2].SetInfo("Jessica", 60);
    mylist[3].SetInfo("Coco", 54);


    //anropar bubblesort()
    bubblesort(mylist, 4);


    int index = Linesearch(mylist, 25);

    if (index == -1)
        cout << "person ej funnen!";
    else
        cout << "personen du letade efter " << mylist[index].name;
    cin.get();
    return 0;
    system("pause");
}

The problem i think is my knowledge since im new to programming and has only done it for 4 weeks. I put this code together from myself and from examples found online. So every answer will help me learn more :)

EDIT: Still the same code added error message here. 50 39 C:\Skrivbordet\Skola\ccxcxcxcvx.cpp [Error] 'Linesearch' was not declared in this scope


Solution

  • A C++ program starts out fairly blank, without any function to use at all except for main. (There is more to say on that. I'm sure comments are going to point that out. ;)) You include headers for <iostream> and string, so you can use all of the functions declared in them, which is a good thing! Lots of stuff is already written for you; you don't have to find out how to write text out to your screen, for example.

    All other functions are to be written by yourself -- which is also a good thing! After all, what would be the fun of a programming language where you cannot do the stuff you want because "there is no function for it".

    You already wrote (or possibly copied) one custom function: bubblesort. From that point on, you can use it anywhere you want. It is not in the standard headers, but the compiler sees it as a general function nevertheless; all it has to do is read your code top to bottom, and store the names of new functions when it encounters them.

    On to your problem: Linesearch is the name of a function. The compiler assumes that because it is followed by a parenthesized argument list. So, it looks up Linesearch in the standard headers, finds it's not in there, then goes through your source code -- and still does not find it. Presto! error.

    The solution is to use an existing standard function that does what you need, if there is one (I cannot recall), or write the function yourself and place it in your source code somewhere above main. (It needs¹ to be above the first function in which it is used, and in this case that is main.)

    ¹ I'm telling only a half-truth there; but I guess before expanding on that in turn, you are better off reading a C++ primer first.