23Zone

Hey, I'm new here and new to C++ and I've been learning it pretty quickly. Today my teacher gave the class a problem to do involving loops. It was, for me, pretty simple because we had just went over it. I completed the problem, well that is what I thought. He told me now I had to insert the functions or prototypes or whatever for while, do while, and for loop. I didn't have time to stay in class so I told him I was going to turn it in tomorrow. I am seriously lost.

Here's, according to him, half the problem. We basically had to countdown from 5,4,3,2,1 followed by Blast off on each statement.

#include <stdio.h>

int main(void)
{

// Local Declarations
int loopcount;

// Statements
loopcount = 5;
printf("while loop :");
while (loopcount > 0)
printf("%3d", loopcount--);

printf("\t\tBlast Off!!!\n\n");

loopcount = 5;
printf("do...while loop: ");
do
printf("%3d", loopcount--);
while (loopcount > 0);

printf("\tBlast Off!!!\n\n");

printf("for loop: ");

for(loopcount=5;loopcount > 0;loopcount--)
{
printf("%3d", loopcount);
}

printf("\t\tBlast Off!!!\n\n");

return 0;
} // main

If you have any comment on my situation, I would appreciate your comments.

Thank You,

23Zone

Comments
First 2 threads on the forum didn't catch your eye? Tag - you're IT

The most basic function prototypes in C++ look like this:

type1 foo(type2 bar);

where type1 is the return type (int, char, double, void etc.)
foo is the name of the function
type2 is the type of the variable being passed
and bar is the name of the variable as it will appear in the function.
The function prototype is different from the function header in that it looks like this:

type1 foo(type2 bar){
}

note that the curly braces surround the body of the function and there is no semicolon at the end of the header - just the opening curly brace.

See if you can do the function for the next two loops.
Take notice how and where to prototype.

#include <stdio.h>

void while_blasting(int counter); /* prototype  */

int main(void)
{
    int loopcount = 5;

    while_blasting(loopcount); /* call to the function */
    
    getchar();
    return 0;
}
/* function using a while loop */
void while_blasting(int counter)
{
    printf("while loop :");
    while (counter > 0)
    {
        printf("%3d", counter--);
    }
    printf("\t\tBlast Off!!!\n\n");
}
This article has been dead for over six months. Start a new discussion instead.