-
Simple storage question
Lets say you have the user input a number. Then you perform a an equation that changes this number. Then you want to start another equation, and you want to use the original defined typed in number and not the new number that comes out of the equation & you dont want to have the user retype this original number. How can i store this Inputted number so it can be used again later?
Sample Dummy Code that helps explain my question:
Code:
#include <stdio.h>
int main(void)
{
int number_1, number_2, number_3;
printf("Input a number: \n");
scanf("%d", &number_1);
number_1 = number_1 * 3;
printf("The # you entered times 3 = %d\n", number_1);
printf("Input another number: ");
scanf("%d", &number_2);
number_1 * number_2 = number_3
printf("Your original input times your 2nd input equals %d", number_3)
//
So how does one store the original input number_1 and use it again, since it was affected by the line (number_1 = number_1 * 3;) So if the user inputed the number 2, it would change to 2*3 = 6... and for the second equation i want to use 2 again and not 6...
Some of you may say, why are you redefining number_1 why not just use a new variable? - i cant do that on the project im working on... I tried a line like number_1 = number_4; and i thought number_4 would store the original number (so that i could use number_4 in the second equation but that did not work.)
-
Use a temporary variable :
Code:
#include <stdio.h>
int main(void)
{
int number_1, temp;
printf("Input a number: \n");
scanf("%d", &number_1);
temp = number_1 * 3;
printf("The # you entered times 3 = %d\n", temp);
temp = number_1 / 2;
printf("The # you entered divided by 2 = %d\n", temp);
return 0;
}
or just print the value that you want printed,
Code:
#include <stdio.h>
int main(void)
{
int number_1;
printf("Input a number: \n");
scanf("%d", &number_1); //Note the '&'
printf("The # you entered times 3 = %d\n", number * 3);
printf("The # you entered divided by 2 = %d\n", number / 2);
return 0;
}
You have to pass the memory location (the address) of the data to scanf(). The ampersand, '&', indicates an address.
Also, return 0 at the end of main.
-
Thanks for the time, i accidently posted before completing my question/code - so i never got to ask what i really wanted...
But I figured out what i was doing wrong with my main project - mixing floats and integers %f %d produces BAD outputs!!!