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() |
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) |
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; } |
Tags |
problems, storing, varilbes |
|
|