|
 |
|
 |
06-05-2005, 03:40 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 9
|
redhead another question
I am trying to make a chart showing lumber sizes and such.
The chart needs to show
Quote:
Lumber Cross-Sectional Moment of Section
Size Area Inertia Modulus
2 x 2 4 1.3 1.3
2 x 4 8 10.7 5.3
2 x 6
2 x 8
2 x 10
2 x 12
4 x 2
4 x 4
(and so up to 10x12)
|
I am doing something wrong beucase its not allowing me to enter but the base and then the program freezes.This is my code I was trying to put in a certain amount of spaced befor eit showed the chart.
Quote:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
float lumber;
char base[10];
char height[12];
int moment;
int sectionmod;
int y=12;
int z=10;
char lumbersize;
float crosssection;
char userresponse;
int main()
{
printf("please enter base");
scanf("userresponse");
while (userresponse<=10);
{
base[z]=userresponse;
printf("please enter height");
scanf("userresponse");
}
while (userresponse<=12)
{
height[y]=userresponse;
crosssection=base[z]*height[y];
moment=crosssection/12;
sectionmod=crosssection/6;
printf("\n\tLumber size%7dCross-sectional area %8dMomentof inertia%9dSection modulous");
printf("\n\t%2c%1d %9c %10c%14c,&base,&height,&crosssection,&moment,§ ionmod");
}
}
|
Thank you for any help you can give me.
~Valerie
|
|
|
06-05-2005, 10:49 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
|
Code:
scanf("userresponse");
This isn't a valid statement, if you'd read up on the scanf()From the C standard library
Quote:
int scanf(const char* format, ...);
Performs formatted input conversion, reading from stdin according to format format. The function returns when format is fully processed. Returns number of items converted and assigned, or EOF if end-of-file or error occurs before any conversion. Each of the arguments following format must be a pointer. Format string may contain:- blanks and tabs, which are ignored
- ordinary characters, which are expected to match next non-white-space of input
- conversion specifications, consisting of:
- %
- (optional) assignment suppression character "*"
- (optional) maximum field width
- (optional) target width indicator:
- h: argument is pointer to short rather than int
- l: argument is pointer to long rather than int, or double rather than float
- L: argument is pointer to long double rather than float
- conversion character:
- d: decimal integer; int* parameter required
- i: integer; int* parameter required; decimal, octal or hex
- o: octal integer; int* parameter required
- u: unsigned decimal integer; unsigned int* parameter required
- x: hexadecimal integer; int* parameter required
- c:characters; char* parameter required; white-space is not skipped, and NUL-termination is not performed
- s: string of non-white-space; char* parameter required; string is NUL-terminated
- e,f,g: floating-point number; float* parameter required
- p: pointer value; void* parameter required
- n: chars read so far; int* parameter required
- [...]: longest non-empty string from specified set; char* parameter required; string is NUL-terminated
- [^...]: longest non-empty string not from specified set; char* parameter required; string is NUL-terminated
- %: literal %; no assignment
|
As you can see, the scanf() function needs to have your format which you'd like the user to input, something like:
Code:
int base, height;
printf("Please enter base in form of B:H: ");
fflush(stdout);
if(2 > scanf("%d:%d", &base, &height))
{
printf("Error, you didn't provide BxH\n");
return -1;
}
/* then do whatever calculation son base and height */
At the very least this would work, but for any other type of input, like strings or chars, you might want to use some other readin handling, since *scanf() functions are prone to introducing errors, because they don't provide any size checking of what they read.
|
|
|
06-06-2005, 12:05 AM
|
#3 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
|
When I first started messing around with your code, I saw that you actualy wanted two things out of the same code portion, from your description it looks like a nested loop where it writes every lumber size possible.
Here is a version where it loops through the lumber sizes:
Code:
#include <stdio.h>
int main()
{
int base, height, crosssection;
float moment, sectionmod;
for(base = 2; base <= 10; base +=2)
{
for(height = 2; height <= 12; height +=2)
{
crosssection=base*height;
moment=crosssection/12.0;
sectionmod=crosssection/6.0;
printf("\n");
printf("\tLumber size (%dx%d):\n", base, height);
printf("\t\t %d Cross-sectional area\n", crosssection);
printf("\t\t %.3f Momentof inertia \n", moment);
printf("\t\t %.3f Section modulous\n", sectionmod);
}
}
return 0;
}
But from your code, it looks like there shuld be some user interaction...
Here is a version where theres user interaction:
Code:
#include <stdio.h>
int main()
{
int base=0, height=0, crosssection=0;
float moment=0.0, sectionmod=0.0;
while((base <= 0 && height <=0) || (base > 10 && height > 12))
{
printf("Please enter base in form of: B:H\t");
fflush(stdout);
if(2 > fscanf(stdin, "%d:%d", &base, &height))
{
printf("Error, you didn't provide B:H\n");
continue;
}
if(base <= 0 || base >10)
{
printf("Error, base must be between 1 and 10.\n");
continue;
}
if(height <= 0 || height > 12)
{
printf("Error, height must be between 1 and 12.\n");
continue;
}
}
crosssection=base*height;
moment=crosssection/12.0;
sectionmod=crosssection/6.0;
printf("\tLumber size (%dx%d):\n", base, height);
printf("\t\t %d Cross-sectional area\n", crosssection);
printf("\t\t %.3f Momentof inertia \n", moment);
printf("\t\t %.3f Section modulous\n", sectionmod);
return 0;
}
I don't knwo which one you wanted, or if it was a combination of them both... But for starters you can look at my two examples and see if any of them fits you.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 09:18 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|