Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modules, using local scope

Status
Not open for further replies.

ZoneDead

Programmer
Sep 5, 2004
3
US
How do you get the var's to move from main to a module, return that data and pass it on to the next module?

This is the code that i have come up with so far, I am attempting to make a program that calculates the area and perimeter of a rectangle.


#include <stdio.h>

//prototypes
void main();

void getUserInput();

void calculateArea();

void calculatePerimeter();

void outputToScreen();
////////////////////////////


void main(void)
{
int perimeter=0;

int length=0;

int width=0;

int area=0;
printf ("This program was created by John Low\n");
printf ("Please enter the width: ");

getUserInput();
//return 0;


} // end of main



void getUserInput(void)
{
scanf("%d", &width);
printf("Enter the length: \n");
scanf("%d", &lenght);

} // end of getUserInput


/*
void calculateArea(void)
{ &width * &length = area;

} // end of calculateArea



void calculatePerimeter(void)
{ &width * 2 + &length * 2= perimeter;

} // end of calculate Perimeter


void outputToScreen(void)
{ printf ("The area is: \n")
printf ("The perimeter is: \n")

}*/ // end of outputToScreen

I have nulled out the last little bit, because I was having difficulty getting the first module to work as it should.
 
For example
Code:
#include <stdio.h>

int input ( int *width, int *height ) {
    char buff[BUFSIZ];
    printf( "Enter width and height > " );
    fflush( stdout );
    if ( fgets( buff, BUFSIZ, stdin ) != NULL ) {
        if ( sscanf( buff, "%d %d", width, height ) == 2 ) {
            return 1;
        } else {
            fprintf( stderr, "Bad input\n" );
            return 0;
        }
    } else {
        return 0;
    }
}

int area ( int width, int height ) {
    return width * height;
}

int main ( ) {
    int width, height;
    if ( input( &width, &height ) ) {
        int result = area( width, height );
        printf( "Area of %dx%d is %d\n", width, height, result );
    }
    return 0;
}

--
 
Salem, that does get me area, but is that modulated?
 
Sure, with a couple of prototypes, you can put input() in input.c and area() in area.c and put main() in main.c



--
 
/*
The final code completed
*/

#include <stdio.h>

//prototypes
void main(void);

int getUserInput(void);

int calculateArea(length, width);

int calculatePerimeter(width, length);

int outputToScreen(area, perimeter);
////////////////////////////


void main(void)
{
int perimeter=0;

int length=0;

int width=0;

int area=0;

printf ("This program was created by John Low\n");
printf ("-------------------------------------------------------\n");
printf ("Lab01 Calculating The Area and Perimeter of a Rectangle\n");

printf ("-------------------------------------------------------\n");
printf ("\n");
printf ("Please enter the width in whole numbers: ");

width=getUserInput();

printf ("Please enter the length in whole numbers: ");
length=getUserInput();


area= calculateArea(length, width);

perimeter= calculatePerimeter(length, width);

outputToScreen(area, perimeter);


} // end of main

/*********************************getUserInput************/
int getUserInput(void)
{ int x;

scanf("%d%*c",&x);
return x;
} // end of getUserInput


/*********************************calculateArea**********/
int calculateArea(int length, int width)
{ return width * length;

} // end of calculateArea


/********************************calculatePerimeter*******/
int calculatePerimeter(int width, int length)
{ return width * 2 + length * 2;

} // end of calculate Perimeter


/*************************outPutToscreeen*****************/
int outputToScreen(int area, int perimeter)
{ printf ("The area is: %d\n", area);
printf ("The perimeter is: %d\n", perimeter);
return 0;
} // end of outputToScreen

 
> void main(void);
1. There is no need to prototype main()
2. main returns an int - see my example

> int calculateArea(length, width);
You don't specify any types here
Try
int calculateArea(int length, int width);

> int outputToScreen(area, perimeter);
You may as well return void here, since you don't do anything with the answer.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top