Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 02-08-2005, 03:42 AM   #1 (permalink)
Upright
 
[C] problems with storing varilbes

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()
steve52 is offline  
Old 02-08-2005, 04:23 AM   #2 (permalink)
Insane
 
Location: Rio Grande Valley, Texas
The problem here is that you are making a a call to the getInput function by copy, not by reference.

If you replace this line:

void getInput(int input1, int input2)

with this:

void getInput(int& input1, int& input2)

and change the prototype as well, you might be in better shape. I'm not a a computer with a compiler right now, so I can't check it.
__________________
"I can't understand why people are frightened of new ideas. I'm frightened of the old ones." -- John Cage (1912 - 1992)
strcrssd is offline  
Old 02-08-2005, 09:48 AM   #3 (permalink)
Crazy
 
Location: Salt Town, UT
in order to do this without using pointers, you can't modify the variables that you are passing into the functions.

My recommendation would be to have getInput() return the integer that was input. Just call it twice, asking for one integer each time.

int getInput( void )
{
int my_input;

printf("Input a number: ");
scanf("%d",&my_input);
printf("\n");

return my_input;
}
Rawb is offline  
Old 02-28-2005, 08:18 AM   #4 (permalink)
Junkie
 
Rawb has the right idea if you are truely using C and not C++. Pass by refrence is a C++ thing, so to do the same in C you need to use pointers. Or just call the function twice and have it return 1 number each time.
Rekna is offline  
 

Tags
problems, storing, varilbes


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 12:01 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76