program not saving values to variables? help?


T

Tybo93

Hello!

This program is meant to take two integers inputted by the user, and
calculate the sum, quotient, remainder, quotient in decimal form, half
of each integer, and an algebraic expression using the two integers.
I know it is probably over-modularized, but the program in itself is a
practice in transferring information through different functions.

My current problem is that the values for sum, quotient, remainder,
fraction, etc. are not saving and, therefore, are not being displayed
when the program prints the results (it comes out as all zeros... with
the exception of quotient and algebra... those come out as random
numbers). However, the program is saving the two integers, because
those are being printed to the screen.

Any and all help is greatly appreciated. My current code is as
follows. Thank you so much!



#include <stdio.h>

void getInput(int* pInput1, int* pInput2);
void calc(int input1, int input2);
void intOps(int input1, int input2, int* pSum, int* pQuotient, int*
pRemainder);
void doubleOps(int input1, int input2, double* pHalf1, double* pHalf2,
double* pFrac);
int algebra(int input1, int input2);
void display(int input1, int input, int sum, int quotient, int
remainder, double half1, double half2, double fraction, int
algebraic);

int main(void)
{
int input1, input2, sum, quotient, remainder, algebraic;
double fraction, half1, half2;

getInput(&input1, &input2);
calc(input1, input2);
display(input1, input2, sum, quotient, remainder, half1, half2,
fraction, algebraic);

return 0;
}

void getInput(int* pInput, int* pInput)
{
printf("\nPlease enter two integers: ");
scanf("%d%d", pInput1, pInput2);
}

void calc(int input1, int input2)
{
int sum, quotient, remainder, algebraic;
double half1, half2, fraction;

intOps(input1, input2, &sum, &quotient, &remainder);
doubleOps(input1, input2, &half1, &half2, &fraction);
algebraic = algebra(input1, input2);
}

void intOps(int input1, int input2, int* pSum, int* pQuotient, int*
pRemainder)
{
*pSum = input1 + input2;
*pQuotient = input1 / input2;
*pRemainder = input1 % input2;
}

void doubleOps(int input1, int input2, double* pHalf1, double* pHalf2,
double* pFrac)
{
*pHalf1 = input1 * 0.5;
*pHalf2 = input2 * 0.5;
*pFrac = input1 / (double) input2;
}

int algebra(int input1, int input2)
{
int algebraic;

algebraic = 2 * input1 + 4 * input2 + input1 * input2 - input1 /
input2;

return algebraic;
}

void display(int input1, int input2, int sum, int quotient, int
remainder, double half1, double half2, double fraction, int algebraic)
{
//insert print statements here;
}
 
Ad

Advertisements

C

Chïna Blüe Öyster Cult

Tybo93 said:
int main(void)
{
int input1, input2, sum, quotient, remainder, algebraic;
double fraction, half1, half2;

These variables are local to main.
getInput(&input1, &input2);
calc(input1, input2);
display(input1, input2, sum, quotient, remainder, half1, half2,
fraction, algebraic);

return 0;
}
void calc(int input1, int input2)
{
int sum, quotient, remainder, algebraic;
double half1, half2, fraction;

These variables are local to calc. They are separate from the local variables in
main, despite any similarity in the names. Assigning to these variables will not
change the variables in main.

You have to either pass in the address of the variables or use global variables
visible to both functions.
intOps(input1, input2, &sum, &quotient, &remainder);
doubleOps(input1, input2, &half1, &half2, &fraction);
algebraic = algebra(input1, input2);
}

/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
#include <stdio.h>

void getInput(int* pInput1, int* pInput2);
void calc(int input1, int input2);
void intOps(int input1, int input2, int* pSum, int* pQuotient, int*
pRemainder);
void doubleOps(int input1, int input2, double* pHalf1, double* pHalf2,
double* pFrac);
int algebra(int input1, int input2);
void display(int input1, int input, int sum, int quotient, int
remainder, double half1, double half2, double fraction, int
algebraic);

static int sum, quotient, remainder, algebraic;
static double half1, half2, fraction;

int main(void)
{
int input1, input2;

getInput(&input1, &input2);
calc(input1, input2);
display(input1, input2, sum, quotient, remainder, half1, half2,
fraction, algebraic);

return 0;
}

void getInput(int* pInput1, int* pInput2)
{
printf("\nPlease enter two integers: ");
scanf("%d%d", pInput1, pInput2);
}

void calc(int input1, int input2)
{
intOps(input1, input2, &sum, &quotient, &remainder);
doubleOps(input1, input2, &half1, &half2, &fraction);
algebraic = algebra(input1, input2);
}

void intOps(int input1, int input2, int* pSum, int* pQuotient, int*
pRemainder)
{
*pSum = input1 + input2;
*pQuotient = input1 / input2;
*pRemainder = input1 % input2;
}

void doubleOps(int input1, int input2, double* pHalf1, double* pHalf2,
double* pFrac)
{
*pHalf1 = input1 * 0.5;
*pHalf2 = input2 * 0.5;
*pFrac = input1 / (double) input2;
}

int algebra(int input1, int input2)
{
int algebraic;

algebraic = 2 * input1 + 4 * input2 + input1 * input2 - input1 /
input2;

return algebraic;
}

void display(int input1, int input2, int sum, int quotient, int
remainder, double half1, double half2, double fraction, int algebraic)
{
printf("1=%d 2=%d +=%d /=%d %%=%d\nh1=%g h2=%g fr=%g al=%d\n",
input1, input2,
sum, quotient, remainder,
half1, half2, fraction, algebraic);

}
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

cc t.c; a.out

Please enter two integers: 15 7
1=15 2=7 +=22 /=2 %=1
h1=7.5 h2=3.5 fr=2.14286 al=161
 
L

luserXtrog

Hello!

This program is meant to take two integers inputted by the user, and
calculate the sum, quotient, remainder, quotient in decimal form, half
of each integer, and an algebraic expression using the two integers.
I know it is probably over-modularized, but the program in itself is a
practice in transferring information through different functions.

My current problem is that the values for sum, quotient, remainder,
fraction, etc. are not saving and, therefore, are not being displayed
when the program prints the results (it comes out as all zeros... with
the exception of quotient and algebra... those come out as random
numbers).  However, the program is saving the two integers, because
those are being printed to the screen.

Any and all help is greatly appreciated.  My current code is as
follows.  Thank you so much!

#include <stdio.h>

void getInput(int* pInput1, int* pInput2);
void calc(int input1, int input2);
void intOps(int input1, int input2, int* pSum, int* pQuotient, int*
pRemainder);
void doubleOps(int input1, int input2, double* pHalf1, double* pHalf2,
double* pFrac);
int algebra(int input1, int input2);
void display(int input1, int input, int sum, int quotient, int
remainder, double half1, double half2, double fraction, int
algebraic);

int main(void)
{
   int input1, input2, sum, quotient, remainder, algebraic;
   double fraction, half1, half2;

   getInput(&input1, &input2);
   calc(input1, input2);
   display(input1, input2, sum, quotient, remainder, half1, half2,
fraction, algebraic);

   return 0;

}

void getInput(int* pInput, int* pInput)

Missing 1 and 2?
{
   printf("\nPlease enter two integers: ");
   scanf("%d%d", pInput1, pInput2);

There is no object in scope named either pInput1 or pInput2.
Why not use 'x' and 'y'? Short, informative, difficutl to
mistypo.
}

void calc(int input1, int input2)
{
   int sum, quotient, remainder, algebraic;
   double half1, half2, fraction;

   intOps(input1, input2, &sum, &quotient, &remainder);
   doubleOps(input1, input2, &half1, &half2, &fraction);
   algebraic = algebra(input1, input2);

}

void intOps(int input1, int input2, int* pSum, int* pQuotient, int*
pRemainder)
{
   *pSum = input1 + input2;
   *pQuotient = input1 / input2;
   *pRemainder = input1 % input2;

}

void doubleOps(int input1, int input2, double* pHalf1, double* pHalf2,
double* pFrac)
{
   *pHalf1 = input1 * 0.5;
   *pHalf2 = input2 * 0.5;
   *pFrac = input1 / (double) input2;

}

int algebra(int input1, int input2)
{
   int algebraic;

   algebraic = 2 * input1 + 4 * input2 + input1 * input2 - input1 /
input2;

   return algebraic;

}

void display(int input1, int input2, int sum, int quotient, int
remainder, double half1, double half2, double fraction, int algebraic)
{
  //insert print statements here;

Is this glaring omission perhaps relevant
to the issue of missing input?

--
have I been trolled?
the handle was kinda suspicious.
this kind of thing really gives a bad name
to those of us who really are decended from apes.
tybo ..., i thought, ... shouldn't it be tai-bo?
as in, half- tai-chi and half- kick-boxing.
and 93. that was a terrible year.
oh well; fortune favors the clumsy.
 
A

August Karlstrom

#include<stdio.h>

void getInput(int* pInput1, int* pInput2);
void calc(int input1, int input2);
[...]

With your program saved to test.c:

$ gcc -Wall test.c
test.c:27: error: redefinition of parameter ‘pInput’
test.c:27: note: previous definition of ‘pInput’ was here
test.c: In function ‘getInput’:
test.c:30: error: ‘pInput1’ undeclared (first use in this function)
test.c:30: error: (Each undeclared identifier is reported only once
test.c:30: error: for each function it appears in.)
test.c:30: error: ‘pInput2’ undeclared (first use in this function)


/August
 
K

Keith Thompson

August Karlstrom said:
#include<stdio.h>

void getInput(int* pInput1, int* pInput2);
void calc(int input1, int input2);
[...]

With your program saved to test.c:

$ gcc -Wall test.c
test.c:27: error: redefinition of parameter ‘pInput’
test.c:27: note: previous definition of ‘pInput’ was here
test.c: In function ‘getInput’:
test.c:30: error: ‘pInput1’ undeclared (first use in this function)
test.c:30: error: (Each undeclared identifier is reported only once
test.c:30: error: for each function it appears in.)
test.c:30: error: ‘pInput2’ undeclared (first use in this function)

Which means that the code you posted is not the code you compiled.

When you post code here, please copy-and-paste the *exact* code that
you fed to the compiler. Don't try to re-type it. Please don't
make us guess which errors are in your actual code, and which are
transcription errors.
 
D

dk

Hello!

This program is meant to take two integers inputted by the user, and
calculate the sum, quotient, remainder, quotient in decimal form, half
of each integer, and an algebraic expression using the two integers.
I know it is probably over-modularized, but the program in itself is a
practice in transferring information through different functions.

My current problem is that the values for sum, quotient, remainder,
fraction, etc. are not saving and, therefore, are not being displayed
when the program prints the results (it comes out as all zeros... with
the exception of quotient and algebra... those come out as random
numbers).  However, the program is saving the two integers, because
those are being printed to the screen.

Any and all help is greatly appreciated.  My current code is as
follows.  Thank you so much!

#include <stdio.h>

void getInput(int* pInput1, int* pInput2);
void calc(int input1, int input2);
void intOps(int input1, int input2, int* pSum, int* pQuotient, int*
pRemainder);
void doubleOps(int input1, int input2, double* pHalf1, double* pHalf2,
double* pFrac);
int algebra(int input1, int input2);
void display(int input1, int input, int sum, int quotient, int
remainder, double half1, double half2, double fraction, int
algebraic);

int main(void)
{
   int input1, input2, sum, quotient, remainder, algebraic;
   double fraction, half1, half2;

   getInput(&input1, &input2);
   calc(input1, input2);
   display(input1, input2, sum, quotient, remainder, half1, half2,
fraction, algebraic);

   return 0;

}

void getInput(int* pInput, int* pInput)
{
   printf("\nPlease enter two integers: ");
   scanf("%d%d", pInput1, pInput2);

}

void calc(int input1, int input2)
{
   int sum, quotient, remainder, algebraic;
   double half1, half2, fraction;

   intOps(input1, input2, &sum, &quotient, &remainder);
   doubleOps(input1, input2, &half1, &half2, &fraction);
   algebraic = algebra(input1, input2);

}

void intOps(int input1, int input2, int* pSum, int* pQuotient, int*
pRemainder)
{
   *pSum = input1 + input2;
   *pQuotient = input1 / input2;
   *pRemainder = input1 % input2;

}

void doubleOps(int input1, int input2, double* pHalf1, double* pHalf2,
double* pFrac)
{
   *pHalf1 = input1 * 0.5;
   *pHalf2 = input2 * 0.5;
   *pFrac = input1 / (double) input2;

}

int algebra(int input1, int input2)
{
   int algebraic;

   algebraic = 2 * input1 + 4 * input2 + input1 * input2 - input1 /
input2;

   return algebraic;

}

void display(int input1, int input2, int sum, int quotient, int
remainder, double half1, double half2, double fraction, int algebraic)
{
  //insert print statements here;



}- Hide quoted text -

- Show quoted text -

What you mean by "inputted" ?
 
Ad

Advertisements

M

Malcolm McLean

My current problem is that the values for sum, quotient, remainder,
fraction, etc. are not saving

int main(void)
{
   int input1, input2, sum, quotient, remainder, algebraic;
   double fraction, half1, half2;

   getInput(&input1, &input2);
   calc(input1, input2);
   display(input1, input2, sum, quotient, remainder, half1, half2,
fraction, algebraic);

   return 0;
}

The reason for your problem is that sum, quotient, etc are in scope in
main(), but not in the other functions.

You need to either make them global, or pass their addresses by
pointers (the preferred solution).
 

Top