Created
September 3, 2018 16:01
-
-
Save Otteri/0bb8ba65b316cb9a60ae5ea4dc7aae21 to your computer and use it in GitHub Desktop.
C++ basics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "examples.h" | |
#include <vector> | |
bool changeAge(std::vector<Person>& people) { | |
// Create people | |
for (int i = 0; i < 50; i++) { | |
Person new_person; | |
new_person.gender = Gender::Male; | |
people.push_back(new_person); | |
} | |
// Set age | |
for (auto &it = people.begin(); it != people.end(); it++) { | |
it->age = 20; | |
} | |
return true; | |
} | |
bool changeName(std::vector<Person>& people) { | |
for (auto &it = people.begin(); it != people.end(); it++) { | |
it->name = "Jenna"; | |
} | |
return true; | |
} | |
void displayPeopleData(std::vector<Person>& people) { | |
for (unsigned int i = 0; i < people.size(); ++i) { | |
fprintf(stdout, "%d | Name: %s, Age: %d, Gender %d\n", i, people[i].name.c_str(), people[i].age, people[i].gender); | |
} | |
std::cin.ignore(std::numeric_limits <std::streamsize>::max(), '\n'); // Wait user to press enter. | |
} | |
int main() | |
{ | |
std::vector<Person> people; | |
people = people; | |
changeAge(people); | |
people = people; | |
changeName(people); | |
people = people; | |
displayPeopleData(people); | |
return EXIT_SUCCESS; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
enum class Gender { | |
Female = 0, | |
Male = 1, | |
}; | |
struct Person { | |
unsigned int age; | |
std::string name; | |
Gender gender; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment