Calling Functions

Say I have a function that I have created that was to run a mathematical equation to find a certain number as follows:
1
2
3
4
int doStuff(int theBeginningNumber)
{
    return(floor((9+sqrt(81+(4*27*(18+theBeginningNumber))))/54));
}


So then I come down to my main, and I want to call on it.

1
2
3
4
5
6
7
int main()
{
    int number1 = 12305, number2 = 23405, number3 = 84722;
    cout << doStuff(number1) << endl;
    cout << doStuff(number2) << endl;
    cout << doStuff(number3) << endl;
}


So I called on that function three times on separate occasions which I feel the way that it was done was inefficient and a waste. I know I have kind of beat around the bush to this point, but is there an easier way to call a function multiple times?
1
2
for( int n : { 12305, 23405, 84722 } ) 
    std::cout << doStuff(n) << '\n' ;


http://www.stroustrup.com/C++11FAQ.html#init-list
http://en.cppreference.com/w/cpp/utility/initializer_list
How would I use variables in the situation though. As if the number1, number2, and number3 were changing so I would never know the number at every given point to put into the function. How would I set it up so it would use whatever the variables are equal to?
1
2
3
4
5
6
7
8
9
const std::size_t N = 3 ; // three numbers
int numbers[N] ; // array of N int

for( int& i : numbers ) std::cin >> i ; // for example, read in N numbers
for( int n : numbers ) std::cout << doStuff(n) << '\n' ;

int a, b, c ;
std::cin >> a >> b >> c ;
for( int n : { a, b, c } ) std::cout << doStuff(n) << '\n' ;
Whenever I attempt to run this I get the error: Range-based loops are not allowed in C++98 mode. Am I doing something wrong?
Enable C++11 mode. Compile with -std=c++11 and these too: -Wall -Wextra -pedantic-errors

This page has instructions (at the bottom): http://www.cplusplus.com/doc/tutorial/introduction/
Topic archived. No new replies allowed.