Confused as to how to pass back to main

This is a discussion on Confused as to how to pass back to main within the C Programming forums, part of the General Programming Boards category; As the title states I'm a little confused as to how I'd pass some of the values within a module ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    14

    Confused as to how to pass back to main

    As the title states I'm a little confused as to how I'd pass some of the values within a module back to main. Someone please help me ASAP!

    Here is my main (it is incomplete) and a module within it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #define    pi 3.146
    #define    brakeFactor 0.7
    double readVal (char *carModel,double *rSlope, double *sLength);
    double calculateSpeed (double rSlope, double sLength);
    double averageSpeed(double sLengthArray,double speedArray);
    void dataSort(double sLengthArray,double rSlopeArray,double speedArray)
    void listValues ()
    int main ()
    {
       char option;
       
       listValue (); /* calling module, not sure what to put for the arguments*/
       while(option != d)
       {   
        printf ("Please enter the letter of your chosen option.\n);
        printf("a. Display the historical data\nb. Find the historical data for a specific car.\nc. Display the average speed.\nd. END");
        scanf ("%c", &option);
        if (option == a)
        {
                  dataSort(double sLengthArray,double rSlopeArray,double speedArray)
                  printf ("|\n");
                  printf ("|\tHISTORICAL DATA FROM SPEEDING CARS\n");
                  printf ("|\tSkidlength\tRoadSlope\tSpeed\n");
                  for (i=1; i<=count; i++)
                  {
                    printf("|\t%.3lf\t\t%0.lf\t\t%.2lf\n", sLengthArray[i], rSlopeArray[i], speedArray[i]);
                  }
                  printf ("|\n");
        }
        if (option == b)
        {
                  
        }
        if (option == c)
        {
                   double averageSpeed(sLengthArray, speedArray);
        }
        if (option == d)
        {
                   printf("You have chosen to end the program");
                   break;
        }
       }
        system ("pause");
        return 0;
    }
    
    
    listValue () /* Called module,need to return the the arrays and sum and count for use in main*/
    {
        double slopeRad, slopeTangent, speedArray[200]={0}, sum=0, speed, sLength, *sLengthP, sLengthArray[200]={0},rSlope, *rSlopeP,rSlopeArray[200]={0}; 
        char carModel[1000], carModelArray[200][1000],*carModelP;
        int count=0, i; 
        carModelP=carModel;
        sLengthP = &sLength;
        rSlopeP = &rSlope;
        
        
        while (sLength != 999)
        {
          readVal(carModel,&rSlope,&sLength);
          if (sLength == 999)
          {
             break;
          }
          else
          {
           speed = calculateSpeed(rSlope,sLength);
           sum = sum + speed;
           count = count + 1;
           for (i=0;i<count;i++)
            {
                sLengthArray[count]= sLength;
                rSlopeArray[count]= rSlope;
                strcpy(carModelArray[count],carModel);
                speedArray[count]= speed;
            }
           }
        }
        printf ("|\n");
        printf ("|\tHISTORICAL DATA FROM SPEEDING CARS\n");
        printf ("|\tSkidlength\tRoadSlope\tSpeed\n");
        for (i=1; i<=count; i++)
        {
          printf("|\t%.3lf\t\t%0.lf\t\t%.2lf\n", sLengthArray[i], rSlopeArray[i], speedArray[i]);
        }
        printf ("|\n");
    }
    
    
    
    double readVal (char *carModel,double *rSlope, double *sLength)
    {
          double all [300];
          int i;
          printf ("Please enter the car model.\n");
          scanf ("%s", carModel);
          do
          {
      			printf ("Please enter a skid length.If 999 is entered, no calculations will be done.\n");
    			scanf ("%lf", sLength);
          }
          while(sLength <0);            
          do
          {
      			printf ("Please enter a road slope between -30 and 30.\n");
                scanf ("%lf",rSlope);
          }
          while (*rSlope<-30||*rSlope>30);
    }
    
    double calculateSpeed (double rSlope, double sLength)
    {
         double slopeRad, slopeTangent, speed;
         slopeRad = rSlope*(pi/180);
         slopeTangent= sin(slopeRad)/cos(slopeRad);
         speed = sqrt(30*sLength*(slopeTangent + brakeFactor));
         return speed;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't do that. ListValues is a function that has local variables. Once you return from the function, those local variables are GONE when your function returns (that is: The space those variables use when you are in the function is no longer available once you leave the function).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    can I just declare the values that i need in main and them pass them to the module by reference?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Nope, but you can pass them by address/pointer.
    No such thing as references in C...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There are 3 ways to pass values outside a functions

    So you want this (for count and carModel):
    1) Global variables
    Code:
    char carModel[1000];
    int count; //automatically count initialized to 0
    ...
    void listValues ();
    int main ()
    {
     ....
      listValues ();
    }
    
    void listValues ()
    {
        //carModel and count are already declared
    }
    alternatively you can do this:
    Code:
    2) Passing with pointers
    void listValues (char CharModel[1000], int* count);
    int main ()
    {
     ....
      char charModel[1000];
      int count = 0;
      listValues (charModel, &count);
    }
    void listValues (char CharModel[1000], int* count)
    {
        //carModel and count are already declared in main
    }
    Lastly,
    3) returning values with return. Only for one variable (except if you use a struct). But you have a lot, so I you don't want this. Just keep in mind if you wanted only count:
    Code:
    int listValues();
    int main ()
    {
     ....
      int count = listValues();
      
    }
    int listValues ()
    {
        //declare all variables as you do
       return count;
    }
    Last edited by C_ntua; 11-28-2008 at 07:20 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Your 3rd example is broken, since the function takes no arguments, yet you pass two... and count is nowhere to be found, nor is it necessary since the count is returned...
    Also, know that global variables should be avoided where possible because they can easily create difficult code to debug. Code that does not run as expected.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    fixed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. " void main() is evil. Use int main(). "
    By Brw_Abhi in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2007, 01:16 PM
  2. This is going to be very easy, loop back to main?
    By mrbump2004 in forum C Programming
    Replies: 11
    Last Post: 12-05-2004, 10:05 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  4. getting a function to return to main??
    By Neildadon in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2002, 10:24 AM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 04:11 AM

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