473,473 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C program variable problem

2 New Member
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_area 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_area 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
Feb 12 '08 #1
4 1803
weaknessforcats
9,208 Recognized Expert Moderator Expert
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:
Expand|Select|Wrap|Line Numbers
  1. Room study;
  2.          study.length = 10;
  3. Room dining;
  4.          dining.length = 10;
  5. etc...
  6.  
Feb 12 '08 #2
Ganon11
3,652 Recognized Expert Specialist
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.
Feb 12 '08 #3
asdasdaasdalsdjas
2 New Member
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4. void total ();
  5.  
  6. main()
  7.  
  8. int sum = 0, a, b, c_area, loop;
  9.  
  10.  
  11. /*********Loop prompts for  2 numbers, stores in a & b variables respectively and calculates area******/
  12.      do {
  13.         printf("\nEnter a value: ");
  14.         scanf("%d", &a);
  15.  
  16.         printf("Enter b value: ");  
  17.         scanf("%d", &b); 
  18.  
  19.         c_area = a*b;
  20.  
  21.         printf("Area is:%d", c_area);
  22.  
  23.  
  24.         printf("\nWould you like to enter more values? 1- yes 2-no:");
  25.         scanf("%d", &loop); 
  26.  
  27.   void total();
  28. {
  29.    sum = c_area + c_area;
  30.    printf("Total area is equal to: %d", sum);  
  31. }
  32.  
  33.       }while (loop == 1);
  34.  
  35. }
  36.  
  37. /***Program ends***/
  38.  
Feb 12 '08 #4
Ganon11
3,652 Recognized Expert Specialist
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.
Feb 12 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Rigga | last post by:
Hi I would like to pass a variable to an external program i.e. os.system('cd $variable') However this doesnt work, I cant figure it out, any ideas? Cheers
2
by: zhouhaiming | last post by:
On our test machine I can compile my c source code on AIX5.1(ML4) by "- q32" option and "OBJECT_MODE=32" environment variable, and it can correctly running. My operation system environment is:...
5
by: cj | last post by:
Thanks to everyone that has helped me. Now I'm trying to write my first program. I have an example of one that I need to write about. Any help getting me started is appreciated. I'm having trouble...
14
by: electrician | last post by:
While running a program that exceeds the array limits it issues an alert. I want to then stop the program after filling in the output boxes with blanks. How do you stop the program? I have...
1
by: sylsau | last post by:
Hello, I wrote a JAVA program which uses the JAVA API JDOM 1.0 (of this site www.jdom.org) I put the archive jdom.jar in the directory /usr/share/java/jdom.jar and I added this path in the...
5
by: Allen | last post by:
I wrote many classes. In a class, there is a member variable which is declared as char szMTreeBuf. On both Windows XP and VxWorks, it cannot work. Then I try to declare the member variable as...
4
by: canteyn | last post by:
Ok here is my problem. I am coding a program that uses 3 different functions, and the end result is that the program reads in (x,y) points from a file, and then outputs those points to another file,...
3
by: googlinggoogler | last post by:
Hi This should all be pretty standard C stuff, but I'm going to use terms like mouse callback to communicate what Im tyring to do. Basically I have my program whirling around in an infinite...
9
by: Keith G Hicks | last post by:
I'm having a lot of trouble with "file in use" errors in my "folder watcher" project. Starting and stopping the watcher and reading my XML file work fine. Once the watcher is started, I'm reading...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.