// Native_Ranged-For_Loop.cpp : Defines the entry point for the console application. /* ________________________________________________ Learning About Range-For Loops Ron Kessler Created 2/13/2017 Native Version Use CTL-F5 to run This little program shows how to use simple for each loops to cycle through a collection of integers. A collection is a group of similar objects. In native C++ this loop is called a Ranged-For loop I store my numbers in a vector which is a data structure that holds multiple things much like a variable. But variables hold one value and a vector is like a variable that can hold multiple values but all under one name. I also show how to sort the numbers in the vector _________________________________________________ */ #include "stdafx.h" #include #include //to use vectors #include //to sort vectors using namespace std; int main() { //---show nice message cout << "Working with Ranged For Loops\n\n"; cout << "I will add up these numbers to get a sum for you. Press any key.\n\n"; //---add items to a new vector vector myNumbers{ 23, 55, 654, 213, 90,67,12,14,92,45}; //--you can sort them also sort (begin(myNumbers), end(myNumbers)); //---cycle through the numbers and display them for (int whichNumber : myNumbers) { cout << whichNumber << endl; } cin.get (); //pause //******************************************* //---lets get the sum of the numbers int GrandTotal = 0; //---cycle through each number and get the grand total using a Range-For Loop for (int eachNumber : myNumbers) { GrandTotal += eachNumber; } //---display it cout << "\n\nThe grand total of your numbers is " << GrandTotal << endl; return 0; }