Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-05-2005, 04:40 PM   #1 (permalink)
rogue
Registered User
 
Join Date: Apr 2005
Posts: 9
rogue is on a distinguished road
Smile 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,&sect ionmod");
}
}
Thank you for any help you can give me.
~Valerie
rogue is offline   Reply With Quote
Old 06-05-2005, 11:49 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-06-2005, 01:05 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
DB Design Question Part II sde Program Design and Methods 10 09-02-2008 12:37 PM
hey redhead quick question about c/c++ rogue Standard C, C++ 2 05-23-2005 02:22 AM
A question about Visual C++.Net 357mag Standard C, C++ 1 03-30-2005 06:01 AM
Question marks and apostrophes Belisarius HTML, XML, Javascript, AJAX 4 05-28-2004 05:11 PM
Compiler Question cheawick Standard C, C++ 3 04-30-2004 04:06 AM


All times are GMT -8. The time now is 05:42 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting