Thread: problems storing values in varibles

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16

    problems storing values in varibles

    I have a simple project for my intro to C class inwich we are suppose to pratice passing varibles through functions. It is a bit over modulized but that is the point of the exercies. The problem I am having is that when the user inputs two integers they are not being stored in the varibles. We have not gotten to pointers yet so I dont need to worry about that. I dont know what else to say so here is the code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void getInput(int input1, int input2);
    void calc(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);
    void intOps(int input1, int input2, int sum, int iDiv, int iMod);
    void floatOps(int input1, int input2, float half1, float half2, float frac);
    double raiseToPower(int input1, int input2);
    void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);
    
    int main(void) 
    {
        int input1;
        int input2;
        int sum, iDiv, iMod;
        float half1, half2, frac;
        double power;
    
       
       getInput(input1, input2);
       calc(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
       display(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
       
       
       system("pause");
       return 0;
       
    }//end of main
    
    /*******************************************************************************
    Name: getInput()
    Purpose: This function prompts the usr for two integers and stores them in memory
    *******************************************************************************/
    void getInput(int input1, int input2) 
    {
       
       
       printf("%20s", "Sean Todd");
       
       printf("\nPlease enter two integers: ");
       
       scanf("%d%d", &input1, &input2);
       
       printf("%20d%20d", input1, input2);
       
       
    }//end of getInput()
    
    /*******************************************************************************
    Name: calc()
    Purpose: This function takes the data the usr entered and calculates the sum,
     the half of each number entered
    *******************************************************************************/
    
    void calc(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power)
    {
       printf("\n%20d %20d", input1, input2);
    
       intOps(input1, input2, sum, iDiv, iMod);
       floatOps(input1, input2, half1, half2, frac);
       power = raiseToPower(input1, input2);
       
    }//end of calc()
    
    /****************************************************************************
    Name: intOps()
    Purpose: perform all integer calculations
    ****************************************************************************/
    void intOps(int input1, int input2, int sum, int iDiv, int iMod) 
    {      
    
           sum = input1 + input2;
           iDiv = input1 / input2;
           iMod = input1 % input2;
           
    }//end of intOps
       
    /****************************************************************************
    Name: floatOps()
    Purpose: perform all integer calculations
    ****************************************************************************/
    void floatOps(int input1, int input2, float half1, float half2, float frac) 
    {
        half1 = (float) input1 / (2.0);
        half2 = (float) input2 / (2.0);
        frac = (float) input1 / (float) input2;
    }//end of floatOps()
       
    /****************************************************************************
    Name: raiseToPower()
    Purpose: perform all integer calculations
    ****************************************************************************/
    double raiseToPower(input1, input2) 
    {
       return pow(input1, input2);
       
    }//end of raiseToPower()
       
    /*******************************************************************************
    Name: display()
    Purpose: outputs the data to screen in report format
    *******************************************************************************/
    void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power) 
    {
        printf("\n%20s%20s", "Description", "Data");
        printf("\n%20s%20s", "-----------", "----");
        printf("\n%20s%20d", "Input1", input1);
        printf("\n%20s%20d", "Input2", input2);
        printf("\n%20s%20d", "Sum", sum);
        printf("\n%20s%20.1f", "Half of Input1", half1);
        printf("\n%20s%20.1f", "Half of Input2", half2);
        printf("\n%20s%20d", "Quotient", iDiv);
        printf("\n%20s%20d", "Remainder", iMod);
        printf("\n%20s%20.4f", "Fraction", frac);
        printf("\n%20s%20.0f", "Power", power);
        
        printf("\n\n");
    
    }//end of display()
    thanks in advance
    Last edited by stodd04; 02-08-2005 at 06:03 AM.

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    You are confused. This
    Code:
    void getInput(int input1, int input2);
    Says that you are going to be passing this function two variables by value, aka copies of the variables. Thus what you are doing doesn't work, in order to perform what you are intending to do you need to pass the variables by reference.
    Hopefully this little sample will clear some stuff up:
    Code:
    #include <stdio.h> 
    
    void myFunc1(int, int); //by value
    void myFunc2(int&, int&); //by reference
    
    int main(void) {
    
    	int x=1, y=1;
    
    	printf("In main() x= %d, y= %d", x, y);
    	printf("\nCalling myFunc1\n");
    	myFunc1(x, y); //pass by value
    
    	printf("\nIn main() x= %d, y= %d", x, y);
    	printf("\nCalling myFunc2\n");
    	myFunc2(x, y); //pass by reference
    
    	printf("\nIn main() x= %d, y= %d\n", x, y);
    	return 0;
    }
    void myFunc1(int a, int b) {
    
    	//we have copies of x and y here
    	a = a + 1;
    	b = b + 1;
    
    	printf("In myFunc1() x= %d, y= %d", a, b);
    }
    void myFunc2(int& c, int& d) {
    
    	c = c + 1;
    	d = d + 1;
    
    	printf("In myFunc2() x= %d, y= %d", c, d);
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    What Compiler you using I got choked by warnings and errors.

    pow requres doubles
    Code:
    double pow(double x, double y);
    Also your getInput function will require to use the int inputs as pointers Ive fixed that, so try this

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void getInput(int *input1, int *input2);
    void calc(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);
    void intOps(int input1, int input2, int sum, int iDiv, int iMod);
    void floatOps(int input1, int input2, float half1, float half2, float frac);
    double raiseToPower(int input1, int input2);
    void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);
    
    int main(void) 
    {
        int input1;
        int input2;
        int sum, iDiv, iMod;
        float half1, half2, frac;
        double power;
    
       
       getInput(&input1, &input2);
       calc(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
       display(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
       
       return 0;
       
    }/* end of main */
    
    /**************************************************  *****************************
    Name: getInput()
    Purpose: This function prompts the usr for two integers and stores them in memory
    **************************************************  *****************************/
    void getInput(int *input1, int *input2) 
    {
       
       
       printf("%20s", "Sean Todd");
       
       printf("\nPlease enter two integers: ");
       
       scanf("%d%d", input1, input2);
       
       printf("%20d%20d", *input1, *input2);
       
       
    }/* end of getInput() */
    
    /**************************************************  *****************************
    Name: calc()
    Purpose: This function takes the data the usr entered and calculates the sum,
     the half of each number entered
    **************************************************  *****************************/
    
    void calc(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power)
    {
       printf("\n%20d %20d", input1, input2);
    
       intOps(input1, input2, sum, iDiv, iMod);
       floatOps(input1, input2, half1, half2, frac);
       power = raiseToPower(input1, input2);
       
    }/* end of calc() */
    
    /**************************************************  **************************
    Name: intOps()
    Purpose: perform all integer calculations
    **************************************************  **************************/
    void intOps(int input1, int input2, int sum, int iDiv, int iMod) 
    {      
    
           sum = input1 + input2;
           iDiv = input1 / input2;
           iMod = input1 % input2;
           
    }/* end of intOps */
       
    /**************************************************  **************************
    Name: floatOps()
    Purpose: perform all integer calculations
    **************************************************  **************************/
    void floatOps(int input1, int input2, float half1, float half2, float frac) 
    {
        half1 = (float) input1 / (2.0);
        half2 = (float) input2 / (2.0);
        frac = (float) input1 / (float) input2;
    }/* end of floatOps() */
       
    /**************************************************  **************************
    Name: raiseToPower()
    Purpose: perform all integer calculations
    **************************************************  **************************/
    double raiseToPower(int input1, int input2) 
    {
       return pow((double)input1, (double)input2);
       
    }/* end of raiseToPower() */
       
    /**************************************************  *****************************
    Name: display()
    Purpose: outputs the data to screen in report format
    **************************************************  *****************************/
    void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power) 
    {
        printf("\n%20s%20s", "Description", "Data");
        printf("\n%20s%20s", "-----------", "----");
        printf("\n%20s%20d", "Input1", input1);
        printf("\n%20s%20d", "Input2", input2);
        printf("\n%20s%20d", "Sum", sum);
        printf("\n%20s%20.1f", "Half of Input1", half1);
        printf("\n%20s%20.1f", "Half of Input2", half2);
        printf("\n%20s%20d", "Quotient", iDiv);
        printf("\n%20s%20d", "Remainder", iMod);
        printf("\n%20s%20.4f", "Fraction", frac);
        printf("\n%20s%20.0f", "Power", power);
        
        printf("\n\n");
    
    }/* end of display() */

  4. #4
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16
    thanks for your responses... I am using bloodshed dev-c++ in c mode... for the class it has to compile on gcc which in past projects has not been a problem

  5. #5
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16
    for some reason I cannot pass a reference in the function prototype. so I am going to give the pointers a shot

  6. #6
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16
    pointers didnt do the trick either... gonna try gcc to see if there is a difference

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >pointers didnt do the trick either

    You need to do the same in a lot more places.
    Code:
    void getInput(int *input1, int *input2);
    void calc(int input1, int input2, int *sum, int *iDiv, int *iMod, float *half1, float *half2, float *frac, double *power);
    void intOps(int input1, int input2, int *sum, int *iDiv, int *iMod);
    void floatOps(int input1, int input2, float *half1, float *half2, float *frac);
    double raiseToPower(int input1, int input2);
    void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);
    
    int main(void)
    {
        int input1;
        int input2;
        int sum, iDiv, iMod;
        float half1, half2, frac;
        double power;
    
    
       getInput(&input1, &input2);
       calc(input1, input2, &sum, &iDiv, &iMod, &half1, &half2, &frac, &power);
       display(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
    
       return 0;
    
    }/* end of main */
    Plus the corresponding changes to the function definitions.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16
    thank ya'll very much for all of your help. I finally figured out passing by reference with pointers with great help from the many wonderful examples. attached is the final code. thanks for the support

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values in memory
    By Opel_Corsa in forum C Programming
    Replies: 5
    Last Post: 09-29-2006, 03:10 AM
  2. Storing a String with values
    By dgibson2004 in forum C Programming
    Replies: 2
    Last Post: 05-10-2006, 11:39 AM
  3. Help with storing letters in varibles
    By cgsarebeast in forum C++ Programming
    Replies: 9
    Last Post: 05-09-2006, 06:03 PM
  4. Storing values in arrays
    By Ripper1 in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2003, 11:04 AM
  5. help storing and calculating hex values
    By anzas in forum C++ Programming
    Replies: 4
    Last Post: 07-14-2003, 01:41 PM