C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-08-2005, 05:59 AM   #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
Attached Files
File Type: c proj2.1.c (4.1 KB, 21 views)

Last edited by stodd04; 02-08-2005 at 06:03 AM.
stodd04 is offline   Reply With Quote
Old 02-08-2005, 06:10 AM   #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
andyhunter is offline   Reply With Quote
Old 02-08-2005, 06:13 AM   #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() */
gsoft is offline   Reply With Quote
Old 02-08-2005, 06:19 AM   #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
stodd04 is offline   Reply With Quote
Old 02-08-2005, 06:22 AM   #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
stodd04 is offline   Reply With Quote
Old 02-08-2005, 06:27 AM   #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
stodd04 is offline   Reply With Quote
Old 02-08-2005, 09:04 AM   #7
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 5,006
>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.*
Dave_Sinkula is offline   Reply With Quote
Old 02-08-2005, 11:56 AM   #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
Attached Files
File Type: c proj2.1.c (4.1 KB, 14 views)
stodd04 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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