Thread: Parameter pass

  1. #16
    Registered User
    Join Date
    Aug 2001
    Posts
    207

    Question header file

    About including the calculations in a header file, is it correct?

    Or should I do something like a library?

    What I want to have, is a shorter code, so I thought about dividing it in parts; like menus and calculations.

    Am I trying to do the correct thing?

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > About including the calculations in a header file, is it correct?
    No - header files are for storing definitions only - like #defines, structures, typedefs, and function prototypes.

    > Or should I do something like a library?
    Probably not - unless you intend to re-use parts of it in other projects.

    > What I want to have, is a shorter code, so I thought about dividing it in parts; like menus and calculations.
    This is a good idea - what you would end up with is several source files containing the code, and perhaps a single .h file describing the interfaces.

    Have you done a design yet?

    I think the menu function would be better like this, with the actual switch statement being in main
    Code:
    int menu ( ) {
        char buff[100];
        int choice;
        do {
            printf("\n\nPlease choose one of the following options: \n\n\n");
            printf("     1) New project\n");
            printf("     2) Existing project \n");
            printf("     3) Materials library \n");
            printf("     4) Instructions \n");
            printf("     5) Exit \n");
            fgets( buff, sizeof(buff), stdin );
            choice = buff[0];
        }
        while ( choice < '1' || choice > '5' );
        return choice;
    }
    That way, when you come to pass parameters to
    old_project();
    caracteristics();
    you wont have to pass them all to menu, just to pass them onto your other functions.

  3. #18
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    > Have you done a design yet?

    Yes I have, but I don't post it because I've done it by hand on paper. More or less, I've divided the whole thing in:

    1) new and existing project

    you can create, load and modify projects

    2) results

    the computer shows the results and offer options like modify,
    print on paper, print to file, and I might think of something else


    3) materials

    list all the existing materials, create, modify


    4) calculations

    I intend to insert all the calculations that go before >2) results,
    maybe all in a huge function (doesn't sound good), I'll see later



    I'm leaving the calculations for the end. I think that if I'm able to have an idea of how to do it, and I undersand it, it won't be that difficult.

    My next step right now is to apply what you've told me now.

    Thanks a lot for your help

  4. #19
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    The examples for parameter passing I've done are not similar to what you're saying.

    I've modified the MENU() function as you said, and included the switch in the main(). My problem is how to make the switch to get the value of choice. Should I declare choice as a global variable?

    Or what am I supposed to do?


    int
    main(void)
    {


    MENU();

    switch (choice) {
    case '1':
    clrscr();
    new_project();
    caracteristics();
    break;

    case '2':
    clrscr();
    old_project();
    caracteristics();
    break;

    case '3':
    clrscr();
    materials();
    break;

    case '4':
    clrscr();
    instructions ();
    break;

    case '5':
    clrscr();
    EXIT ();
    break;

    default:

    MENU();
    break;

    }


    return (0);
    }



    int
    MENU( )
    {

    char buff[100];
    int choice;
    clrscr();
    do {
    printf("\n\nPlease choose one of the following options: \n\n");
    printf(" 1) New project\n");
    printf(" 2) Existing project \n");
    printf(" 3) Materials library \n");
    printf(" 4) Instructions \n");
    printf(" 5) Exit \n");
    fgets( buff, sizeof(buff), stdin );
    choice = buff [0];


    }
    while ( choice < '1' || choice > '5' );
    return (choice);


    }




    I don't really understand how you use buff, but it doesn't really matters right now.

  5. #20
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    I've declared choice as global and it works.

    But I thought I had to try to keep the number of global variables to a minimun, trying to use more local variables.

    Is there another way to do it?

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > My problem is how to make the switch to get the value of choice. Should I declare choice as a global variable?
    No!
    In main, you have this

    Code:
    int main ( ) {
        int choice;
        while ( 1 ) {
            choice = menu();
            if ( choice == '5' ) break;  // from the infinite while loop
            switch ( choice ) {
                case 1: ....
            }
        }
        return 0;
    }

  7. #22
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Sorry, I had in mind that I could declare local variables in any function but "main".
    So I have declared choice as a local at main, but then I have to declare it in MENU() as well.

    I've done this so far, and it compiles OK and it runs OK (except that the menu is displayed twice when I return from the function caracteristics, I'll solve this later).

    Is it OK then if I just declare the variable twice as local? I mean, I know it works, but is it correct?




    double LOA, I, J, P, E, D, GM, H1, H2, H3, S1, S2, S3;


    int main( )
    {


    int choice;
    while ( 1) {
    choice = MENU();
    if ( choice == '5' ) break; // from the infinite while loop
    switch (choice) {
    case '1':
    clrscr();
    new_project();
    caracteristics();
    break;

    case '2':
    clrscr();
    old_project();
    caracteristics();
    break;

    case '3':
    clrscr();
    materials();
    break;

    case '4':
    clrscr();
    instructions ();
    break;

    case '5':
    clrscr();
    EXIT ();
    break;

    default:

    MENU();
    break;

    }

    }
    return (0);

    }


    int
    MENU( )
    {

    char buff[100];
    int choice;
    clrscr();
    do {
    printf("\n\nPlease choose one of the following options and press enter: \n\n\n");
    printf(" 1) New project\n");
    printf(" 2) Existing project \n");
    printf(" 3) Materials library \n");
    printf(" 4) Instructions \n");
    printf(" 5) Exit \n");
    fgets( buff, sizeof(buff), stdin );
    choice = buff [0];

    }
    while ( choice < '1' || choice > '5' );
    return (choice);


    }




    void
    new_project(void)
    {

    char file_name[PATH_MAX];

    FILE *output;

    printf("Write the name of the new project \n");
    scanf("%s", file_name);
    strcat( file_name, ".pro" );
    output = fopen(file_name, "w");

    printf("Please enter the characteristics of the new project \n");
    printf("All the values are to be entered in meters, except the weight");
    printf("\nwhich is in kilograms");

    printf("\nLOA: ");
    scanf("%lf", &LOA);
    fprintf(output, "%.2f\n", LOA);
    printf("\nI: ");
    scanf("%lf", &I);
    fprintf(output, "%.2f\n", I);
    printf("\nJ: ");
    scanf("%lf", &J);
    fprintf(output, "%.2f\n", J);
    printf("\nP: ");
    scanf("%lf", &P);
    fprintf(output, "%.2f\n", P);
    printf("\nE: ");
    scanf("%lf", &E);
    fprintf(output, "%.2f\n", E);
    printf("\nDisplacement (in kilograms): ");
    scanf("%lf", &D);
    fprintf(output, "%.2f\n", D);
    printf("\nGM: ");
    scanf("%lf", &GM);
    fprintf(output, "%.2f\n", GM);
    printf("\nH1: ");
    scanf("%lf", &H1);
    fprintf(output, "%.2f\n", H1);
    printf("\nH2: ");
    scanf("%lf", &H2);
    fprintf(output, "%.2f\n", H2);
    printf("\nH3: ");
    scanf("%lf", &H3);
    fprintf(output, "%.2f\n", H3);
    printf("\nS1: ");
    scanf("%lf", &S1);
    fprintf(output, "%.2f\n", S1);
    printf("\nS2: ");
    scanf("%lf", &S2);
    fprintf(output, "%.2f\n", S2);
    printf("\nS3: ");
    scanf("%lf", &S3);
    fprintf(output, "%.2f\n", S3);

    fclose(output);

    printf("\n\n Press any key to continue");
    getch();
    MENU();

    }




    void
    old_project(void)

    {
    // Open an existing project


    char file_name[PATH_MAX];
    FILE *input;

    struct ffblk ffblk;
    int done;

    clrscr();
    printf("Directory listing of existing projects \n");
    done = findfirst("*.pro",&ffblk,0);
    while (!done)
    {
    printf(" %s\n", ffblk.ff_name);
    done = findnext(&ffblk);
    }


    printf("\nName of the project you want to load: ");
    scanf("%s", file_name);
    input = fopen(file_name, "r");


    if (( input = fopen(file_name, "r+")) == NULL ) // check it for errors
    {
    printf("There was an error reading from the file!! \n");
    printf("Press any key to continue ");
    getch();
    old_project();

    }

    fscanf(input, "%lf\n", &LOA);
    fscanf(input, "%lf\n", &I);
    fscanf(input, "%lf\n", &J);
    fscanf(input, "%lf\n", &P);
    fscanf(input, "%lf\n", &E);
    fscanf(input, "%lf\n", &D);
    fscanf(input, "%lf\n", &GM);
    fscanf(input, "%lf\n", &H1);
    fscanf(input, "%lf\n", &H2);
    fscanf(input, "%lf\n", &H3);
    fscanf(input, "%lf\n", &S1);
    fscanf(input, "%lf\n", &S2);
    fscanf(input, "%lf\n", &S3);

    fclose(input);





    }


    void
    caracteristics(void)
    {

    clrscr();

    printf("The caracteristics of the actual project are: \n");

    printf("1) LOA: %.2f\n", LOA);
    printf("2) I: %.2f\n", I);
    printf("3) J: %.2f\n", J);
    printf("4) P: %.2f\n", P);
    printf("5) E: %.2f\n", E);
    printf("6) Displacement: %.0f\n", D);
    printf("7) GM: %.3f\n", GM);
    printf("8) H1: %.2f\n", H1);
    printf("9) H2: %.2f\n", H2);
    printf("10) H3: %.2f\n", H3);
    printf("11) S1: %.2f\n", S1);
    printf("12) S2: %.2f\n", S2);
    printf("13) S3: %.2f\n", S3);


    printf("\n\n Press any key to continue");
    getch();

    MENU();


    }

  8. #23
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Sorry, I had in mind that I could declare local variables in any function but "main".
    Thats a new one - most newbies just declare it wrong by saying it returns void.
    The only 'special' rule for main is that you can't call it recursively - apart from that, its just like any other function.

    > Is it OK then if I just declare the variable twice as local? I mean, I know it works, but is it correct?
    Very correct.

    > double LOA, I, J, P, E, D, GM, H1, H2, H3, S1, S2, S3;
    Now to get rid of all these global variables, by putting the declaration inside new_project.

    Code:
    printf("\nLOA: ");
    scanf("%lf", &LOA);
    fprintf(output, "%.2f\n", LOA);
    printf("\nI: ");
    scanf("%lf", &I);
    fprintf(output, "%.2f\n", I);
    You only need one local variable here - since all you are doing is reading input, and writing it out to a file
    Code:
    double inputval;
    printf("\nLOA: ");
    scanf("%lf", &inputval);
    fprintf(output, "%.2f\n", inputval);
    printf("\nI: ");
    scanf("%lf", &inputval);
    fprintf(output, "%.2f\n", inputval);
    > (except that the menu is displayed twice...
    Because it still calls MENU();
    It should just return to main, where it will fall out of the switch statement, then back round the while(1) loop into a call to MENU().

  9. #24
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    >> Sorry, I had in mind that I could declare local variables in any >function but "main".
    >Thats a new one - most newbies just declare it wrong by saying >it returns void.
    >The only 'special' rule for main is that you can't call it recursively -> apart from that, its just like any other function.

    I'm glad you've found something new because of me, even it's not something positive.


    >You only need one local variable here - since all you are doing is >reading input, and writing it out to a file

    I've done it. It's a great idea. I only have a problem with it. The function old_project. What I used to do before, was to read the data from an existing .pro file, and copy all the values into the global variables (LOA, B...). No I can't as these variables don't exist anymore.
    So I thought:
    1) that maybe I should create a file called actual (for example); this file would take the values of the project I want to open. This way, the functions that I'm going to create for the calculations, would take the values reading from this file.

    or

    2) might be a better idea, just to have a global variable, that keeps the name of the actual project, and when I'm going to use its values (in the calculations functions), I'd just read strait away from the file.

    I think that 2) is a better option because to handle a variable will be better than a whole file.

    Am I right this time?

  10. #25
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    OK, now I've included a char called actual. The name of the project the user is using, is saved into this variable.

    I've compiled it and it works. It runs as well.

    I still have to fix the menu appearing twice when I come back from a function (I'll do it later).

    I attach the code I have till now, in case anyone wants to have a look.

  11. #26
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    >> (except that the menu is displayed twice...
    >Because it still calls MENU();
    >It should just return to main, where it will fall out of the switch >statement, then back round the while(1) loop into a call to MENU().

    I've changed it to main(), and I still have the same problem.

    I've posted this specific question at:

    http://www.cprogramming.com/cboard/s...&threadid=5560

  12. #27
    Former Member
    Join Date
    Oct 2001
    Posts
    955
    I think I got it, it the function MENU, you have fgets on stdin, it's supposed to work, but for some strange reason it doesn't.

    use scanf instead!

    try

    Code:
    scanf("%s",buff);
    I tried it here and it works!

    Oskilian

  13. #28
    Unregistered
    Guest
    Your teacher means to say that the parameters that u are passing to your functions are not ok...i mean to say that, if u have a function say name,


    name(param1,param2,......paramn)

    here u may not be using the correct params

  14. #29
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    >Your teacher means to say that the parameters that u are >passing to your functions are not ok...i mean to say that, if u >have a function say name,

    In fact my problem was that I didn't pass parameters at all.

    Now the whole code is changed, with much help from Big Salem and Osk.

    I'm still working on it, though. The output is about the same, but the code is a lot more efficient (I think).

    Well, I just wanted to say thanks again to Salem and Osk, because you've been quite patient with me.

    Salem: actually you've helped as well with my last question. I needed to count the number of lines in a text file. So I used the "search" in the C board, and ended up with some code from you. That's the function I've done:

    Code:
    int count_lines ( ) {
    
       char buff[BUFSIZ];
       int contador = 0;
       char  file_name[PATH_MAX];
       FILE *input;
    
       strcpy (file_name, material);
       input = fopen(file_name, "r");
       while ( fgets( buff, BUFSIZ, input ) != NULL ) {
             contador++;
       }
       fclose( input );
       return (contador);
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  2. Passing "this" as function parameter
    By pgavigan in forum C++ Programming
    Replies: 15
    Last Post: 07-13-2007, 10:06 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problems generating terrain with OpenGL
    By OnionKnight in forum Game Programming
    Replies: 8
    Last Post: 04-26-2007, 05:05 AM
  5. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM