Advertisement

My first program is broken.

Started by April 17, 2001 04:44 PM
3 comments, last by Mr_Matrix 23 years, 9 months ago
This is my first day programing and I made a program to find the area of a square or rectangle but it says there are errors plz help. // Area- this program finds the area of a shape // you select then enter the demensions #include #include int main(int nNumberofArgs, char* pszArgs[]) { string shape; cout << "enter the shape you want to find"; cout << "the area of\n"; cin >> shape; if (shape == square) { // When they select square... int sqSide; int sqArea; cout << "For a square enter the length of one side\n"; cin >> sqSide; sqArea = sqSide * sqSide; cout << "The area for your square is:" cout << sqArea; } if (shape == rectangle) { //When they select rectangle... int recSide1; int recSide2; int recArea; cout << "Enter the lengh and height of the rectangle\n" cin >> recSide1; cin >> recSide2; recArea = recSide1 * recSide2; cout << "The area for your rectangle is:" cout << recArea; } else { cout << "Sorry I dont know that shape yet"; } return 0; }
please give us the error-messages next time you post, they would help us a lot

at first sight, it looks like you have to put square and rectangle between " ", because they are string-constants

you also forgot to put '';'' at the end of each line

and either put ''using namespace std;'' under the #includes or use ''std::string'' instead of ''string''
Advertisement
"square" and "rectangle" aren''t variables. You need to test the input against a value, not a variable. Secondly, use if/else if/else instead of if/if/else.

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/
Ok, first part.
You define a variable as string shape. I can not see your #includes, but are you including string? string is not a default c/c++ type.
If so, you are attempting to use the string from the stl. In order to use the stl, you need to specify the std namespace or you can prefix the std namespace scope to the string.

I assume that you are also including the iostream.h header file. Again, an assumption based on the fact that I can not see your include statement.

You also missed the semi-colon at 3 locations.

cout << "Enter the lengh and height of the rectangle\n"


cout << "The area for your rectangle is:"


cout << "The area for your square is:"

  #include <string>//if you want to use string, you need to add this line to your //sourceusing namespace std;//or, you can simply use the scope operator to the namespace, //ie. std::string.  


another problem that I see is that you are comparing your string variable to square, but square is not defined. Did you define square someplace? Or did you simply forget the "" around the word square.

Here is a working copy of your program.
  // Area- this program finds the area of a shape// you select then enter the demensions#include <string>#include <iostream>using namespace std;int main(int nNumberofArgs, char* pszArgs[]){		string shape;		cout << "enter the shape you want to find";	cout << "the area of\n";	cin >> shape;		if (shape == "square")	{		// When they select square...		int sqSide;		int sqArea;		cout << "For a square enter the length of one side\n";		cin >> sqSide;				sqArea = sqSide * sqSide;				cout << "The area for your square is:";		cout << sqArea << endl;	}		else if (shape == "rectangle")	{		//When they select rectangle...		int recSide1;		int recSide2;		int recArea;		cout << "Enter the lengh and height of the rectangle\n";		cin >> recSide1;		cin >> recSide2;				recArea = recSide1 * recSide2;				cout << "The area for your rectangle is:";		cout << recArea << endl;	}	else	{		cout << "Sorry I dont know that shape yet";	}	return 0;}  


In the future, it might be helpful to print out some of your compile errors you get.

Alek
Thanks for the advice it helped alot

This topic is closed to new replies.

Advertisement