Help creating function for simple program

Feb 18, 2013 at 7:33am
hey guys, im trying to make a function out of the following code

for(Z=0; Z < 3; Z++)
printf(".");
fflush(stdout);
sleep(1); }

From what i understand, because there is no return value, i need to declare this function i want as void. I want the function to be called periods.
I have used

void periods ()
{
periods()
}

before my main() in an attempt to declare the function

i declare int Z from the for loop right after main()

i finally attempt to create the function with

periods(); {
for(Z=0; Z < 3; Z++)
printf(".");
fflush(stdout);
sleep(1); }

im returning 0 at the very end.
Currently, the program crashes when it reaches the part where i attempt to create periods(). I have tried without the ; but then i get an debug expectation.

Thanks for the help! Also im a self taught nub so go easy on me!
Feb 18, 2013 at 7:34am
here is my entire program so far just in case

#define _cpp_stdc
#include <stdio.h>
#include <iostream>
#include <unistd.h>

void SLEEP(int x)
{
sleep(x);
}

void periods ()
{
periods();
}

int main()
{
using namespace std;

int Z;
int i;


for(i=0; i < 10; i++)
printf("hello world!\n");

{
cout << "awawawawawawa";
cout << endl;
fflush(stdout);
sleep(3);
cout << endl;

cout << "Ahem";
fflush(stdout);
sleep(1);
}

{
periods(); {
for(Z=0; Z < 3; Z++)
printf(".");
fflush(stdout);
sleep(1); }
}


cout << endl;
{
cout << "testing"; fflush(stdout); sleep(1); cout << " testing"; cout << endl;
fflush(stdout);
sleep(2);
cout << "Well"; fflush(stdout); sleep(1); cout << " hello"; fflush(stdout); sleep(1); cout << ", buttlicker."; cout << endl;
}

return 0;

}
Feb 18, 2013 at 9:18am
you calling function periods() in itself, so you have an infinite recursion and program crashes with stack overflow. Why are you doing that?

what are you need:
1
2
3
4
5
6
7
8
void periods()
{
    for(Z=0; Z < 3; Z++) {
        printf(".");
        fflush(stdout);
        sleep(1);
    }
}
Last edited on Feb 18, 2013 at 9:20am
Feb 18, 2013 at 8:43pm
Great! i figured it out messing around with your suggestion! Thanks! After that code, periods() calls the function! yay!

Topic archived. No new replies allowed.