skitzFist

Untitled

Nov 13th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. // Lagrar: namn och ålder
  9. class Person{
  10.  
  11. public:
  12. string name;
  13. int age;
  14.  
  15. Person(string _name, int _age);
  16. void setAttribute(string _name, int _age);
  17.  
  18. void print ();
  19. };
  20.  
  21.  
  22. //konstruktor
  23. Person::Person(string _name, int _age){
  24. name = _name;
  25. age = _age;
  26. }
  27.  
  28.  
  29. //Byta namn, ålder
  30. void Person::setAttribute(string _name, int _age){
  31. name = _name;
  32. age = _age;
  33. }
  34.  
  35.  
  36. //Metod för att printa ut namn och ålder.
  37. void Person::print(){
  38. cout << name << " : " << age << endl;
  39.  
  40. }
  41.  
  42.  
  43. //funktion för att lägga till nya familjemedlemmar
  44. void add (int &antal, vector<Person> &familj){
  45.  
  46. string tName; // temporary name
  47. int tAge; // Temporary age
  48.  
  49.  
  50. cout << "\nHow many family members do you want to add? --> ";
  51. cin >> antal; cout << endl;
  52.  
  53. for (int i=0; i < antal; i++){
  54. cout << "Enter name: "; cin >> tName;
  55. cout << "Age of " << tName << ": "; cin >> tAge; cout << endl << endl;
  56. familj.push_back(Person(tName, tAge)); // lägger till sist i listan och för över värden från tName, tAge till Person name; age;
  57. }
  58. }
  59.  
  60.  
  61. int linearSearch(int &antal, vector<Person> &familj, int &key){
  62.  
  63.  
  64. cout << "Which age are you looking for? --> ";
  65. cin >> key;
  66.  
  67. for (int i=0; i<antal; i++){
  68.  
  69. if (familj[i].age == key){
  70. return i;
  71.  
  72. }
  73.  
  74. return -1;
  75. }
  76.  
  77. }
  78.  
  79. int main(int argc, char** argv) {
  80.  
  81. string choice1;
  82. int antal = 0;
  83.  
  84.  
  85. vector<Person> familj;
  86.  
  87. do{
  88. cout << "[A]dd family\n[P]rint family members\n[L]inear search\n[S]ort\n[B]inary search\n[Q]uit\n--> ";
  89. cin >> choice1;
  90.  
  91. if (choice1 == "a" || choice1 == "A"){
  92. add(antal, familj);
  93. system("PAUSE");
  94. system ("cls");
  95. }
  96. else if (choice1 == "p" || choice1 == "P"){
  97. cout << endl;
  98. for (int i = 0; i < antal; i++){ // tyckte detta var en rätt smart lösning :P Innan hade jag for-loopen inuti print
  99. familj[i].print();
  100. }
  101. system("PAUSE");
  102. system ("cls");
  103. }
  104. else if (choice1 == "l" || choice1 == "L"){
  105. int key;
  106. cout << endl;
  107. int index = linearSearch(antal, familj, key);
  108. if (index == -1){
  109. cout << "Could not find person\n";
  110. }
  111. else {
  112. cout << "Name of family member with age " << key << ": " << familj[index].name << "\nIndex number: " << index << endl;
  113. }
  114. system("PAUSE");
  115. system ("cls");
  116. }
  117. }
  118. while (true);
  119.  
  120.  
  121.  
  122.  
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment