-5

Write a function incrementByTen that uses a static variable count. Each call to thefunction should displaythe value of count and then increment count by 10.Initialize count to be 0 at the start of the program.

I know how to do this by keep calling my main. but How do i do it using a function??

int main()

      {
         static int count = 0;
           count++;

               cout<<count<<endl;
               main();
               system("PAUSE");
               return 0;
       }
BaidNation
  • 387
  • 1
  • 2
  • 10
  • 1
    Make a new method ( at the text says, ) don't keep calling main. – olevegard Jun 16 '13 at 21:39
  • 1
    Do you know how to create another function? It looks a lot like `main()` (because `main()` is a function). – Code-Apprentice Jun 16 '13 at 21:40
  • Any [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on C++ will explain how to implement functions plus a whole lot more. I recommend you pick one up and start reading. – Captain Obvlious Jun 16 '13 at 23:13
  • I know how to make a function. however, I am just having a hard time implementing this one. I am not suppose to use loops. – BaidNation Jun 16 '13 at 23:49
  • # include using namespace std; # include # include using std::setw; int incrementByTen(int); int main() { int num1; incrementByTen(num1); cout< – BaidNation Jun 16 '13 at 23:49

2 Answers2

4

You rename it. (You may want to remove the call to system if that was meant to stay in main, but do note that it's not very portable and may or may not work on one OS. An alternative to system("pause") is std::cin.get(). Although this is only testcode, it's important to get used to good practices.)

Also, never call main.

§3.6.1.3 of the standard states:

The function main shall not be used within a program.

user123
  • 8,970
  • 2
  • 31
  • 52
0
#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;
}
olevegard
  • 5,294
  • 1
  • 25
  • 29
  • There's not much point to a global here. If `main` needs to know what it is, return the new value. – chris Jun 16 '13 at 22:19
  • @chris Agree, a local variable would suffice. But the text specified static. – olevegard Jun 16 '13 at 22:28
  • Yeah, a static local variable. – chris Jun 16 '13 at 22:49
  • @chris I updated the answer, but technically the local static variable is initialized at started, or rather; it's initialized to 0 at startup, the re-initialized when the function is called. – olevegard Jun 16 '13 at 22:58
  • The local one is initialized on first use to my knowledge. – chris Jun 16 '13 at 23:00
  • @chris Yeah, but it is also zero-initialized on startup. – olevegard Jun 16 '13 at 23:08
  • I invite you to look at [this](http://coliru.stacked-crooked.com/view?id=225ede0116ac38586e7b18fd136e3898-1dfa45f65786f4a26064bc85b3de325a). – chris Jun 16 '13 at 23:25
  • @chris Take a look at the standard concerning zero-initialization ( 8.5 - 5 ) It states that zero-initialization does not mean calling the constructor. I would copy the section, but it's too large. – olevegard Jun 17 '13 at 05:06
  • Interesting. I've read that section a few times before, but I don't think that ever registered. – chris Jun 17 '13 at 05:18