Project 3 [Solved]

Posts 1–8 of 8 · Page 1 of 1
Project 3 [Solved]
so i sort of got the hang of c++ but i still havent been able to finish the project... heres what i have so far but keeps giving me errors..anything that u guys can teach would be greatly apreciated

//Yair Jimenez

#include <iostream>
using namespace std;

int main
{
int double length=0,width=0,volume=0,maxvolume=0,height=0,max height=0;cut;

cout << "\nEnter Length in cm: "<<endl;
char again = 'y';
while (again=='y'||again=='Y')
while (!(cin >> length)||length <=0)
{
cout << "\nEnter length"<<endl;
while (!(cin>>length)||length <=0)
{
cout << "\nError,invalid entry.Re-Enter length in cm (must be positive integer): "<<endl;
cin.clear();
fflush(stdin);
}
cout <<"\nEnter width: "<<endl;
while(!(cin >> width)||width <=0)
{
cout <<"\nError, invalid entry .Re-Enter Length in cm (must be positive integer): "<<endl;
cin.clear();
fflush(stdin);
}

for (heigh=0;height<(length/2);height+=0.001)
{
Volume = (length-(2*height)) * (width-(2 * height)) * (height);
if (maxvolume < volume
{maxvolume=volume
cut=height
}
{
cout <<"\n with length of"<<length<<"cm"
<<"and width of"<<width
<<"cm: "<<endl;
cout <<"\nResult of maximum volume is: "
<<maxvolume<<",and the cut is: "<<cut<<endl;
fflush(stdin);
cout<<endl<<" press y to start over, other press enter to exit";
cin>>again
}
}
}
}
Quote Originally Posted by yair View Post
so i sort of got the hang of c++ but i still havent been able to finish the project... heres what i have so far but keeps giving me errors..anything that u guys can teach would be greatly apreciated

//Yair Jimenez

#include <iostream>
using namespace std;

int main
{
int double length=0,width=0,volume=0,maxvolume=0,height=0,max height=0;cut;

cout << "\nEnter Length in cm: "<<endl;
char again = 'y';
while (again=='y'||again=='Y')
while (!(cin >> length)||length <=0)
{
cout << "\nEnter length"<<endl;
while (!(cin>>length)||length <=0)
{
cout << "\nError,invalid entry.Re-Enter length in cm (must be positive integer): "<<endl;
cin.clear();
fflush(stdin);
}
cout <<"\nEnter width: "<<endl;
while(!(cin >> width)||width <=0)
{
cout <<"\nError, invalid entry .Re-Enter Length in cm (must be positive integer): "<<endl;
cin.clear();
fflush(stdin);
}

for (heigh=0;height<(length/2);height+=0.001)
{
Volume = (length-(2*height)) * (width-(2 * height)) * (height);
if (maxvolume < volume
{maxvolume=volume
cut=height
}
{
cout <<"\n with length of"<<length<<"cm"
<<"and width of"<<width
<<"cm: "<<endl;
cout <<"\nResult of maximum volume is: "
<<maxvolume<<",and the cut is: "<<cut<<endl;
fflush(stdin);
cout<<endl<<" press y to start over, other press enter to exit";
cin>>again
}
}
}
}
Code:
#include <iostream>
using namespace std;

int main()
{
	int length=0,width=0,cut;
	double volume=0,maxvolume=0,height=0,maxheight=0;	
	char again = 'y';
	while (again=='y'||again=='Y')
	{
		cout << "\nEnter Length in cm: "<<endl;
		cin >> length;
		while (length <=0)
		{
			cout << "\nError,invalid entry.Re-Enter length in cm (must be positive integer): "<<endl;
			cin.clear();
			fflush(stdin);
			cout << "\nEnter length"<<endl;
			cin>>length;	
        	}

		cout <<"\nEnter width: "<<endl;
		cin >> width;
		while(width <=0)
		{
			cout <<"\nError, invalid entry .Re-Enter Length in cm (must be positive integer): "<<endl;
			cin.clear();
			fflush(stdin);
			cout <<"\nEnter width: "<<endl;
			cin >> width;
		}
			
		for (height=0;height<(length/2);height+=0.001)
		{
			volume = (length-(2*height)) * (width-(2 * height)) * (height);
			if (maxvolume < volume)
				maxvolume=volume;
			cut=height;
		}
		
		cout <<"\n with length of "<<length<<"cm"<<" and width of "<<width<<"cm"<<endl;
		cout <<"\nResult of maximum volume is: "<<maxvolume<<",and the cut is: "<<cut<<endl;
		fflush(stdin);
		cout<<endl<<" press y and enter to start over, other press enter to exit"<<endl;
		cin>>again;
	}
	return 0;
}
when i enter 10 and 20 i should get a cut of 2.11 and max volume of 192.45...do u know what im doing wrong?
You're using integers for width and height, change those to doubles. integers don't allow decimals
Quote Originally Posted by Hell_Demon View Post
You're using integers for width and height, change those to doubles. integers don't allow decimals
Correct
Quote Originally Posted by Hell_Demon View Post
You're using integers for width and height, change those to doubles. integers don't allow decimals
It was actually that he was storing cut in an integer, as well as doing some weirdo thing for the loop rofl.

Code still looks ugly as dick.
/Marking solved. Jason helped him fix the code.

Here's the fixed code:
[highlight=vb]//Yair Jimenez

#include <iostream>
using namespace std;

int main()
{
int length=0, width=0; //givin value to different variables
char again = 'y';
while (again=='y'||again=='Y') // allows the user to run the program multiple times
{
cout << "\nEnter the Length: "<<endl; //tells the user to enter the length
cin >> length;
while (length <=0)
{

cout << "\nError,invalid entry.Re-Enter length (must be positive integer): "<<endl; //Displays error message if the width is not a postive integer
cin.clear(); fflush(stdin);
cout << "\nEnter length"<<endl;
cin>>length; //holds the integer entered for length
}

cout <<"\nEnter width: "<<endl;
cin >> width; //holds the integer entered for width

while(width <=0)
{
cout <<"\nError, invalid entry .Re-Enter Length (must be positive integer): "<<endl; //Displays error message if the width is not a postive integer
cin.clear();fflush(stdin);
cout <<"\nEnter width: "<<endl;
cin >> width;
}

double cut = 0, volume = 0, curvolume = 0;
double upperBound = length / 2.0;
if ( width / 2.0 < upperBound ) upperBound = width / 2.0;

for(cut = 0.0; cut < upperBound; cut += 0.01)
{
curvolume = (length - ( cut * 2 )) * (width - ( cut * 2 )) * cut;
if ( volume > curvolume )
break;
volume = curvolume;
}
cut -= 0.01;

cout <<"\n For the entered sides of length "<<length<<" and width of "<<width<<endl; //displays information
cout <<"\nResult of maximum volume is: "<<volume<<",and the cut is: "<<cut<<endl;
fflush(stdin);
cout<<endl<<" Do you want to run the program again <y/n>?"<<endl; //ask user if they would like to run the program again
cin>>again;

}
return 0;
}[/highlight]
I just tried that code, works great at finding the volume
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us