Simple storage question

This is a discussion on Simple storage question within the C Programming forums, part of the General Programming Boards category; Lets say you have the user input a number. Then you perform a an equation that changes this number. Then ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    16

    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.)
    Last edited by zipfur; 10-05-2002 at 12:03 PM.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    16
    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!!!
    Last edited by zipfur; 10-05-2002 at 01:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  4. Data Storage Question, and Dynamic variables?
    By Zeusbwr in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2004, 11:01 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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