The last post I made was awful, I'm sorry. But I will not make the same mistake twise... I hope =)
Now this is what I got:
A class named Film.
A vector with the data-type Film (I hope) called Arkiv
A function allowing the user to create a new object from the class Film,
#include <iostream>
#include <vector>
usingnamespace std;
class Film
{
public:
string Titel;
string Media;
void skrivFilm() // This function prints the Film
{
cout << "Titel: " << Titel << endl;
cout << "Media: " << Media << endl;
}
};
int main()
{
// This is a vector of the data-type Film, named Arkiv
// or this is what I think this is
vector<Film> Arkiv;
Film nyFilm; // I create a new object and let the user define it below
cout << "Ange filmens Titel: ";
getline(cin, nyFilm.Titel);
cout << endl;
cout << "Ange filmens Media: ";
getline(cin, nyFilm.Media);
cout << endl;
nyFilm.skrivFilm(); // I print the new object with this function.
return 0;
}
This works just as it's supposed to.
Now I want to add the new Film object in my vector Arkiv. But I don't know how?
#include <iostream>
#include <vector>
usingnamespace std;
class Film
{
public:
string Titel;
string Media;
void skrivFilm() // This function prints the Film
{
cout << "Titel: " << Titel << endl;
cout << "Media: " << Media << endl;
}
};
int main()
{
// This is a vector of the data-type Film, named Arkiv
// or this is what I think this is
vector<Film> Arkiv;
Film nyFilm; // I create a new object and let the user define it below
cout << "Ange filmens Titel: ";
getline(cin, nyFilm.Titel);
cout << endl;
cout << "Ange filmens Media: ";
getline(cin, nyFilm.Media);
cout << endl;
Arkiv.push_back(nyFilm);
nyFilm.skrivFilm(); // I print the new object with this function.
return 0;
}
Just use the member function push_back() to 'push' the object into the vector.
#include <iostream>
#include <vector>
#include <windows.h>//För att använda system "cls" och sleep
#include <limits> //För att använda numeric_limits
usingnamespace std;
class Film
{
public:
// Deklarerar variablerna för de data som behövs
string Titel;
string Media;
// Denna funktion skriver ut filmen på skärmen
void skrivFilm()
{
cout << endl;
cout << "Titel: " << Titel << endl; // Skriver ut filmens titel
cout << "Media: " << Media << endl; // Skriver ut filmens media
}
};
int main()
{
setlocale(LC_ALL, "swedish"); // Denna funktion låter mig använda svenska
vector<Film> Arkiv; // Skapa en vector av typen Film
char menyVal; // Denna variabel håller reda på vilket val användaren anger i huvudmenyn
Film nyFilm; // För att skapa en film behöver vi ett objekt att spara den i
do
{
system("cls"); // Se till att rensa skärmen
cout << " ** ARKIV ** " << endl;
cout << "*******************" << endl;
cout << "1: Ny film" << endl;
cout << "2: Radera film" << endl;
cout << "3: Sök efter film" << endl;
cout << "4: Visa alla" << endl;
cout << "0: Avsluta" << endl;
cout << "___________________" << endl;
cout << "Ange val: ";
cin >> menyVal;
cin.ignore( numeric_limits <streamsize>::max(), '\n' );
switch(menyVal)
{
case'1':
{
system("cls");
cout << "\n\n1: Ny film";
Sleep(1000);
cout << endl << endl;
// Vi ger objektets titel-variabel ett värde
cout << "Ange filmens Titel: ";
getline(cin, nyFilm.Titel);
cout << endl;
// Vi ger objektets media-variabel ett värde
cout << "Ange filmens Media: ";
getline(cin, nyFilm.Media);
cout << endl;
// Vi lägger vårt filmobjekt i vektorn
Arkiv.push_back( nyFilm );
break; // Undvik fall-through
}
case'2':
{
break;
}
case'3':
{
// <---- I WANT A SEARCHFUNCTION HERE
break; //
}
case'4':
{
system("cls");
for(int i=0; i<Arkiv.size(); i++) // Räkna upp alla filmer i Arkiv
{
Arkiv[i].skrivFilm(); // Skriv ut dem med hjälp av funktionen skrivFilm();
}
cout << "\n\nTryck på ENTER för Huvudmeny.";
cin.ignore();
break;
}
case'0':
{
break;
}
default : cout << "Felaktigt val, försök igen." << endl;
Sleep(1000);
system("cls");
}
}
while( menyVal != '0');
}
Can I print all the objects in the vector using the function void skrivFilm() declared in the class Film?
In this case, you should use an iterator to call the member function 'skrivFilm()' for each element of the vector. You would use a const_iterator if your member function 'skrivFilm()' was a constant function (which it should be).
1 2 3
vector<Film>::iterator iter;
for(iter = Arkiv.begin(); iter != Arkiv.end(); ++iter)
iter->skrivFilm(); // I print the new object with this function.
This is optional, you could use array subscripts to access the elements of a vector.
1 2
for(int i = 0; i < Arkiv.size(); ++i)
cout << Arkiv[i].skrivFilm(); // I print the new object with this function.