C program variable problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asdasdaasdalsdjas
    New Member
    • Feb 2008
    • 2

    C program variable problem

    Hey,

    Im trying to create a program that will ask the user to input a set of numbers (measurments =length, width, height; using formula for surface area) for a room, and then will be prompted if any other room measurments are to be calculated, at which point if they answer yes, the program will run through again.
    At the end of it, (when they answer no at the prompt to measure any other rooms) the program will caluclate the total amount of area for all the rooms put together (adding surface area for one room, to another, etc.).

    Now, Ive been racking my brain over this for the past few hours, and I feel like Im just going in a circle.

    My main question is, how do I get the computer to recognize one length (ex length1) and allocate it for future use, and recognize another length (ex length2)
    without the previous variable being over-allocated?
    I was thinking to create local variables within a function, but Im still left with this problem ultimately as even if I do that, ill still be left with variables of the same name that somehow have to be added to one another.Would pointers be an option?

    Its simply because of the dependancy on whether or not the person chooses to do multiple roooms that's grating on me. I cant see a clear way to foresee a way that can save variables, without overwriting variables of the same name, in the process.

    Ex. If total_surface_a rea for my first loop of measurements resulted in 550 m,
    and the next loop (because the user wanted another room to be calculated), total_surface_a rea measurements resulted in 600 m...

    How am I supposed to add 550 and 600 if they're allocated within the same space, while still making sure if a person wants 1 room or 10 rooms measured, ultimately all those areas can be caluclated in a total?

    Im not looking for someone to give me the straight out answer, but, if I could even be pointed in the right direction or given a suggestion it would help me out.

    Thanks a lot
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Use a struct:

    [code=c]
    struct Room
    {
    int length;
    int height;
    int width;
    };
    [code=c]

    Then you can create Room variables and use the members:
    [code=c]
    Room study;
    study.length = 10;
    Room dining;
    dining.length = 10;
    etc...
    [/code]

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      You could also use an additional variable, sum, that would start at 0 and add the surface area totals together each time a room was calculated. This could be a global variable, or simply allocated before your calculations/loops.

      Comment

      • asdasdaasdalsdjas
        New Member
        • Feb 2008
        • 2

        #4
        Originally posted by Ganon11
        You could also use an additional variable, sum, that would start at 0 and add the surface area totals together each time a room was calculated. This could be a global variable, or simply allocated before your calculations/loops.
        I kind of understand what youre saying, in the sense that, i KNOW ive done stuff like that before, and the answer is right in front of me, so, it makes sense for me to use the sum variable, but something isnt connecting still... :$

        I made up a quick program to kind of illustrate what Im talking about, and where my problem lies...

        As you'll see, my sum function will calculate the total sum of the area already given (by adding c_area with itself), and not of two separate areas, and combining the two together, as Im trying to make it do.

        Like what I want, is so c_area will calculate once, the loop will go through, Ill do the loop again, c_area will be calculated again, Ill end the loop, and then that first c_area and the second c_area will be added together.
        And so it can be applicable whether I want to do the loop twice, or 10 times, and get an area accordingly and add the areas together at the end.

        Code:
        #include <stdio.h>
        
        void total ();
        
        main()
        { 
        
        int sum = 0, a, b, c_area, loop;
        
        
        /*********Loop prompts for  2 numbers, stores in a & b variables respectively and calculates area******/
             do {
                printf("\nEnter a value: ");
                scanf("%d", &a);
        
                printf("Enter b value: ");  
                scanf("%d", &b); 
                
                c_area = a*b;
                
                printf("Area is:%d", c_area);
        
              
                printf("\nWould you like to enter more values? 1- yes 2-no:");
                scanf("%d", &loop); 
          
          void total();
        {
           sum = c_area + c_area;
           printf("Total area is equal to: %d", sum);  
        }
          
              }while (loop == 1);
            
        }
        
        /***Program ends***/

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          I understand that this is a sample program, but you've got the idea wrong. Total should not be a function - if it was, it wouldn't be able to see your sum variable declared in main. You would have to make a new variable inside the function, which would disappear once the function ended. Since you want to have the value in main, not in that function, this idea cannot work.

          Think about this: You will not change your program very much. Leave alone all the calculations you use to get surface area. All you need is one more variable that will start at 0 (int sum = 0), and, once you have the surface area, add it to the total before you start the loop over again. Can you get this?

          If you have any more questions, let us see your source code and we can go from there.

          Comment

          Working...