hello there, it says that i have 2 errors in my code. What have i done wrong? (doesnt show where); something that calculates the area and the perimeter.

#include <iostream.h>
typedef unsigned short int pint;
int main()
{
	pint width;
	pint length;
	pint area=(width*length);
	pint perim=( (width+width) + ( length+length) ); 
	
	pint answer;
	
	cout<<"please Enter a value for width:";
	cin>> width;
	cout<<"\n thank you, please enter a value for length:\n";
	cin>> length;
	
	cout<<"\n-----<Menu>-----\n";
	cout<<"\n Press 1 to calculate area\n";
	cout<<"\n Press 2 to calculate the perimeter\n";
	{
		cin>> answer;
		if (answer == 1)
		{cout<<"\nThe area is:" << area << "m2";}
		
		if (answer == 2)
		{cout<<"\nThe perimeter is:" << perim << "meters\n";}
                {
				else
					if (answer > 2)
					{cout"\nInvalid command!\n";}
				}
	}
		{cout<<"program now exiting...\n";}
	return 0;
}

help required

Recommended Answers

All 10 Replies

Well what are the errors, that's like taking your car into a shop and saying, "Something's wrong". Aside from the seemingly random placement of braces.

if (answer == 2)
		{cout<<"\nThe perimeter is:" << perim << "meters\n";}
                {
				else
					if (answer > 2)
					{cout"\nInvalid command!\n";}
				}

Unexpected open and close brace.

Invisal, that would be all well and good if that was the only occurance.

Firstly, you are trying to calculate before you have any values in your variables. Steps go as follows:
Define
Initialize (set values)
Calculation
Output

NOT

Define
Calculate
Initialize
Output

Also, take a gander at your nearest reading material on when and where you should open code blocks with braces. cout is not one of those times. However, if and else if/else are. However(that's redundant, I don't care), if there is only one operation after the clause (if/else if/else) then braces are unnecessary.

if(1 = 1) cout << "1 = 1"; //doesn't need braces
if(1 = 1)
{
   cout << "This needs braces";
   cout<< " 1 = 1"; //DOES require braces
}

ShawnCplus: I am really sorry because I was just checking for the syntax errors and I haven't noticed the logical errors. Thanks you for telling me :D

pint width;
	pint length;
	pint area=(width*length);
	pint perim=( (width+width) + ( length+length) );

revenge2: You were using the un-intiatized variables to calculate. What is value of width in this line? What is value of length?. One more thing, (width+width)+(length+length) can be write like this 2*(width+length).

@revenge2

i guess you have got the solution to your problem...

PS: it's better to use switch in menu-input programs...

cin>>choice
switch(choice)
{
case 1: //code for calculate area
            //output and break;
case 2: //code for calculate perimeter
           //output and break;
default: //check for invalid input
            //ask for re-entering and break;
}

Thanks for the help, im a complete newb im still a little bit away from using switches. Thanks for the help

specially shawncplus, clear explanation, i was just placing random braces hehehe.

one more question if i may?....

what is the meaning of placing \n before and after?
(i want to have like the menu, thats why)
-thanks

\n creates a new line whenever that is used so

cout<<"Hello\nWorld!";
would output
Hello
World!

yes i understand this..but suppose it was

cout<<"hello world!";
cout<<"\nHow are you?";

how does the 2nd line differ from
cout<<"\nhow are you\n"
&
cout<<"how are you\n"

Well for the one in code tags it has a newline at the start, the second has a newline at the start and another after it, and the third only sends it at the end.

simply remember....wherever u place \n...the string coming after this will be printed from new line in the command winodw while running...

u will also see \t soon... it place a space of one tab in the string coming after it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.