General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 22nd, 2010, 05:53 PM
ianrice ianrice is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2010
Posts: 2 ianrice User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 58 sec
Reputation Power: 0
For Loop Help

I need to create a program that calculates the beam size needed for a certain load based on 3 given equations using if test and a for loop.

#include <iostream>
#include <cmath>
#include<fstream> //include library so you can read/write to files
using namespace std;


double buck(double length,double width);
double comp(double width);
double slender(double length, double width);

int main()
{
double load,length,loadb,width,test=1,loadc,result;

ofstream outfile; //declare var to write to file
outfile.open("hw4c.txt");//open output file to write to


cout<<"Enter the expected load in pounds: ";
cin>>load;
cout<<"Enter the length of the column in inches: ";
cin>>length;

for(width=2;test=1;width+=2)// Something seems to be wrong here
{ loadb=buck(length,width);
loadc=comp(width);
result=slender(length,width);}
cout<<loadb<<"\n";cout<<loadc<<"\n";cout<<result<<"\n";
if(load<=loadb && load<=loadc && result==1)
{test=0;
cout<<"For a load of "<<load<<" and a length of "<<length<<" inches, recommended sqaure beam has a side of "<<width<<" inches\n";}
else
test=1;
cout<<test<<"\n";


system("pause");
return 0;
}


double buck(double len,double wid)
{double area(0);
area=wid*wid;
return (51000*area)/(len*len/area);}



double comp(double width)
{double loadc(0);
return width*width*445;}




double slender(double length, double width)
{ double result(0);
if (length/width <=50)
return 1;
else
return 0;}

Reply With Quote
  #2  
Old March 25th, 2010, 08:25 AM
mecanicu mecanicu is offline
anonymous
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 244 mecanicu User rank is Private First Class (20 - 50 Reputation Level)mecanicu User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 12 h 21 m 10 sec
Reputation Power: 13
For ...

This is a more readable code:
Code:
int main()
{
	double load,length,loadb,width,test=1,loadc,result;

	ofstream outfile; //declare var to write to file
	outfile.open("hw4c.txt");//open output file to write to


	cout<<"Enter the expected load in pounds: ";
	cin>>load;
	cout<<"Enter the length of the column in inches: ";
	cin>>length;

	for(width=2;test=1;width+=2)// Something seems to be wrong here
	{ 
		loadb=buck(length,width);
		loadc=comp(width);
		result=slender(length,width);
	}
	cout<<loadb<<"\n";cout<<loadc<<"\n";cout<<result<<"\n";
	if(load<=loadb && load<=loadc && result==1)
	{
		test=0;
		cout<<"For a load of "<<load<<" and a length of "<<length<<" inches, recommended sqaure beam has a side of "<<width<<" inches\n";
	}
	else
		test=1;
	cout<<test<<"\n";


	system("pause");
	return 0;
}

As for the "for" statement you can read something here
In your statement
for(width=2;test=1;width+=2)
"test=1" is not a condition (it's an assignment), you need a comparison like
"test==1" but in your "for" loop the "test" variable has the value 1 all the time so this condition always evaluates to true
I suspect that you want to stop the "for" loop when width is greater than a given value

hope it helps

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > For Loop Help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap