Small Steps VI — Growing into C++
Greeting without Small
Write a greeting without Small.
What you will learn
Using only the standard tools you have learned lets you do the same work without Small.
Try it
Example · example1.cpp
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "Name: ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "\n";
return 0;
}
Use main instead of small_main, std::string instead of String, getline instead of input, and cout instead of print. Entering Alex produces Hello, Alex.
Because no Small features are used, small.h, initialize_small, and shutdown_small are unnecessary too.
Exercise
Write a main program that adds all values in a std::vector<int> and prints the sum with std::cout, using no Small API.
Exercise starter · exercise1_starter.cpp
#include <iostream>
#include <vector>
int main()
{
std::vector<int> numbers = {10, 20, 30};
int total = 0;
// sum and print using only standard C++.
return 0;
}
Hint
Write #include <iostream> and #include <vector> yourself.
show solution
#include <iostream>
#include <vector>
int main()
{
std::vector<int> numbers = {10, 20, 30};
int total = 0;
for (int value : numbers)
total = total + value;
std::cout << total << "\n";
return 0;
}