Like Tree1Likes
  • 1 Post By std10093

Passing variable from function - main - function

This is a discussion on Passing variable from function - main - function within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> // number of salesman #define SIZE 100 // salesman_type structure definition typedef struct{ ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    Passing variable from function - main - function

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    // number of salesman 
    #define SIZE 100
    // salesman_type structure definition
    typedef struct{
            int ID;
            char id_txt[6], name[50];
            double hor_total;
        }salesman_type; // end structure salesman_type
    // define salesman_type variable 
    salesman_type salesman[SIZE];
    
    
    // function prototype
    int nextId(); 
    int sale_menu(); 
    void AddSalesMan(); 
    
    
    // function to generate id
    // currentId and isReset are parameters
    int nextId(int currentId, int isReset)
    {
        // initializes elements to 0 first time function is called
        static int lastId = 0;
        // if isReset is x; assign x to lastId; return lastId with adding 1
        if (isReset)
        {
            lastId = currentId;
        }
        return ++lastId;
        
    }
    // sales processing system menu function
    int sale_menu()
    {
        int menu;
        printf("Sales Processing System\n");
        printf("-----------------------\n\n");
        printf("1. Add Salesman Records\n");
        printf("2. Reports Generation\n");
        printf("3. Modify Salesman Records\n");
        printf("4. Delete Salesman Records\n\n");
        printf("0. Exit\n");
        printf("Your choice: ");
        scanf("%d", &menu);
        return menu;
        printf("\n");
    }
    // function to add salesman
    void AddSalesMan()
    {
        FILE *inSales;
        int a, b;
        double sale[SIZE][4]; 
    
    
        if ( (inSales = fopen("sales.txt","a")) == NULL)
            printf("Cannot open sales.txt file.");
        else
        {
            for(a=0; a<SIZE; a++)
            {
                fprintf(inSales,"S%04d|",salesman[a].ID);
                //printf("Name: ");
                //scanf(" %[^\n]", salesman[a].name);
    
    
                for (b=0; b<4; b++)
                {
                    printf("Quarter %d: ",b+1);
                    scanf(" %lf", &sale[a][b]);
                    fprintf(inSales,"%lf|",sale[a][b]);
                }    
                fputc('\n',inSales);
                printf("\n");
            }    
        } 
        fclose(inSales);
    }
    
    
    // function main begins program execution
    void main()
    {
        // nextid.txt file pointer 
        FILE *idPtr;
        // variable declaration
        int a, b, c = 0, last; // counter
        int getMenu = 0, getReport = 0; // menu selector
        double ver_total = 0, max = 0; 
        int id = 0, lastId, resetId = 0;
    
    
        
        // fopen files the file; exits program if file cannot be opened
        if ( (idPtr = fopen("nextid.txt","r") ) == NULL)
        {
            printf("Cannot open nextid.txt file\n");
            system("pause");
            exit(-1);
        } // end if
        else
        {
            // read lastId from file
            fscanf(idPtr,"%d",&lastId);
            // Generate new id from previous salesman ID if the salesman is one or more
            // lastId and restId are arguments
            nextId(lastId, resetId);
        } // end else
        // fclose close the file
        fclose(idPtr);
    
    
        // Initialize salesman id from nextId function and salesman horizontal total value to zero
        for(a=0; a<SIZE; a++) 
        {
            salesman[a].hor_total = 0;
        }
        
        // call and assign sale_menu function to getMenu
        getMenu = sale_menu();
        printf("\n");
        // Execute command based on user input
        switch(getMenu)
        {
            // call AddSalesMan function
            case 1:
                AddSalesMan();
                break;
            // call and assign report_menu to getReport
            case 2:
                break;
        } // end switch
        system("pause");
    }
    How can I pass the lastId variable value to AddSalesMan() function?
    Last edited by ulti-killer; 11-01-2012 at 11:38 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Maybe you need to see the picture that salem has
    You should write int main and not void.Even better write int main(void) and at the last line of main add a line return 0 to inform the rest of the world that everything terminated as expected!
    I think your question is this
    Code:
    How do we pass parameters into a function?
    The syntax of a function's prototype is something like this
    Code:
    returnValueType functionName(listOfParameters);
    • the returnValueType is the type of the value to be returned.If the function does not have to return something then returnValueType is replaced be void.If you want to return a int you replace returnValueType with an int
    • functionName is the name of the function
    • listOfParameters is the parameters passed into the function.If no parameters are passed into a function then you replace listOfParameters with void.If you want one integer to be passed as a parameter then you replace listOfParameters with int.If you want two int parameters,then you replace listOfParameters with int,int


    Example
    In this example we are going to write a function named myFunction.It will receive as a parameter an integer and it will assign it the value 5.Then it will return this parameter,in order to let main know what happened to the value that we are going to return.You must bare in mind that the body of the function is a world parallel to the world of main.In order this two parallel words to communicate with each other,we use the parameters to go from the world of main to the world of the function, and keyword return to go from the world of function to the world of the main.What i called world, is what is called scope. (not exactly, but this is ok for now )
    Code:
    #include <stdio.h>
    
    /*returnValueType functionName(listOfParameters);*/
    int myFunction(int firstParameter)
    {
         /* Body of the function */
         firstParameter = 5;
         return firstParameter;
    }
    
    int main(void)
    {
        int myVariable;
        myVariable = 10;
        myVariable = myFunction(myVariable);
        printf("myVariable = %d\n",myVariable); /* What is the value of myVariable? */
        return 0;
    }
    Hope this helps
    Salem likes this.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,265
    You appear to just want to get a value back from nextId.
    Just use the return value, the same way you did when calling the sale_menu() function.

    You've probably been told 20 times by now, but it's int main(void), not void main().
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 11:32 AM
  2. Passing array back to main function
    By greg677 in forum C Programming
    Replies: 11
    Last Post: 05-01-2010, 04:27 PM
  3. passing variables from main to another function
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-06-2006, 06:30 PM
  4. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 04:19 PM
  5. Big Problem With Passing an Matrix to Main Function
    By Maragato in forum C Programming
    Replies: 4
    Last Post: 06-14-2004, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21