#include <iostream>
// Create count as a static variable and initialize it to 0
static int count = 0;
// Function that increases count by ten
static void IncrementByTen()
{
std::cout<<count<< std::endl;
count+= 10;
}
int main()
{
// As long as count is less or equal to 100
while ( count <= 100 )
{
// Print and increment
IncrementByTen();
}
// Wait for user to hit enter
std::cin.ignore();
return 0;
}
count
is now a static variable, and can be accessed from any function. You could also have IncrementByTen()
call itself, and add check for if it's more than 100 in the function itself, kinda like this
#include <iostream>
// Function that increases count by ten
static void IncrementByTen()
{
// Create count as a static variable and initialize it to 0 the first time this function is called
static int count = 0;
std::cout<<count<< std::endl;
count+= 10;
if ( count <= 100 )
IncrementByTen();
else
return;
}
int main()
{
// Print and increment
IncrementByTen();
// Wait for user to hit enter
std::cin.ignore();
return 0;
}