• 09-30-2003
sweetly
I know this is a mess... I got parts of it to work (the square part) but when I add in triangle...I get lots of errors

Code:

```#include <iostream> #include <string> #include <cstdlib> using namespace std; void instructions();        //Instructions to the user. void calculations();        //Calculates the area of a triangle or square. int main () { // Instructions for the user.         instructions(); // Calculates the area.         calculations(); return 0; } // Instructions.         void instructions() {                cout << "This program calculates the area of "                 << "either a square or a triangle.  You will" << endl;         cout << "first be asked to enter a letter "                 << "(s for square, or t for triangle) to identify" << endl;         cout << "which kind of figure you want the area computed for.  "                 << "Be sure to type a " << endl;         cout << "lower-case letter, and not a capital letter.  "                 << "Then you will be asked for " << endl;         cout << "dimensions of the figure.  Always hit the <Enter> "                 << "key after typing whatever " << endl;         cout << "has been asked for." << endl << endl; }  // End instructions.         // Calculations.         void calculations()         {         int squ1, tri1;         char let1;         float areas;          float areat; cout << "Type the letter s to compute the area of a square, "                 << "or type t for a triangle: ";         cin >> let1;         if (let1=='s'){                 cout << "Enter the length of a side for the square: ";                 cin >> squ1;                 if (squ1 > 0)                 {//Calculates the area of a square                 areas = (squ1 * squ1);                 cout << "The area of a square with a side of " << squ1                         << " is " << areas << endl;}                         else (squ1 <= 0)                         {cout << "It is impossible to have a square with a side                                   << "whose length is " << squ1 << endl;}         else                 if (let1=='t'){                 cout << "Enter the length of the base for the triangle: ";                 cin >> tri1;                 //Calculates the area of a triangle                 areat = (.5 * tri1 * tri1);                 cout << "The area of a triangle with a base of " << tri1                         << " and a height of " << tri1 << " is " << areat << endl;                 }                 else {                         cout << "This program can only recognize the "                                 << "lower-case letters s and t.  Sorry." << endl;                         }         return; }        // End calculations.```
• 09-30-2003
lyx
2. Stop placing your brackets like that, choose a style ans stick to it, don't use several different ones at the same time.
3. Skip some lines to make your code clearer.
4. Align your statements to a certain column.
5. Put spaces between an operator and its operands.

So, here the code, a if was missing after an else and before the conditionnal statement. And, you forgot to place a closing bracket somewhere. (but, well, if you didn't use such a messy style...)
Code:

```#include <iostream> #include <string> #include <cstdlib> using namespace std; static void instructions();        //Instructions to the user. static void calculations();        //Calculates the area of a triangle or square. int main () {         instructions();         calculations();     return 0; } void instructions() {                cout << "This program calculates the area of "                 << "either a square or a triangle.  You will" << endl;         cout << "first be asked to enter a letter "                 << "(s for square, or t for triangle) to identify" << endl;         cout << "which kind of figure you want the area computed for.  "                 << "Be sure to type a " << endl;         cout << "lower-case letter, and not a capital letter.  "                 << "Then you will be asked for " << endl;         cout << "dimensions of the figure.  Always hit the <Enter> "                 << "key after typing whatever " << endl;         cout << "has been asked for." << endl << endl; } void calculations()        {         int  squ1, tri1;         char  let1;         float areas;         float areat;     cout << "Type the letter s to compute the area of a square, "                 << "or type t for a triangle: ";         cin  >> let1;         if (let1 == 's') {                 cout << "Enter the length of a side for the square: ";                 cin  >> squ1;                 if (squ1 > 0) {                     areas = (squ1 * squ1);                     cout << "The area of a square with a side of " << squ1                             << " is " << areas << endl;         } else if (squ1 <= 0)             cout << "It is impossible to have a square with a side "                                 << "whose length is " << squ1 << endl;     } else if (let1 == 't') {                 cout << "Enter the length of the base for the triangle: ";                 cin  >> tri1;                 areat = (.5 * tri1 * tri1);                 cout << "The area of a triangle with a base of " << tri1                         << " and a height of " << tri1 << " is " << areat << endl;         } else             cout << "This program can only recognize the "                         << "lower-case letters s and t.  Sorry." << endl;         return; }```
My indentation is bad because the original code used tabs instead of spaces.
• 09-30-2003
sweetly
Thanks!! Actually I did get it cleaned up and it works now. I just need some help adjusting the spaces. It comes out with spaces between each line, but I need to have the code display without spaces. (I hope that made sense)

Code:

```#include <iostream> #include <string> #include <cstdlib> using namespace std; void instructions();        //Instructions to the user. void calculations();        //Calculates the area of a triangle or square. int main () { // Instructions for the user.         instructions();         // Calculates the area.         calculations();         calculations();         calculations();         calculations();         calculations();         calculations();         calculations();         calculations();         return 0; } ----------------------------------------------------------- // Instructions.         void instructions() {                cout << "This program calculates the area of "                 << "either a square or a triangle.  You will" << endl;         cout << "first be asked to enter a letter "                 << "(s for square, or t for triangle) to identify" << endl;         cout << "which kind of figure you want the area computed for.  "                 << "Be sure to type a " << endl;         cout << "lower-case letter, and not a capital letter.  "                 << "Then you will be asked for " << endl;         cout << "dimensions of the figure.  Always hit the <Enter> "                 << "key after typing whatever " << endl;         cout << "has been asked for." << endl << endl; }  // End instructions. ------------------------------------------------------------------- // Calculations.         void calculations() {         int squ1, tri1, tri2, area;         char let1;                  cout << "Type the letter s to compute the area of a square, "                 << "or type t for a triangle: ";         cin >> let1;         cout << endl;                if (let1 == 's')         {                cout << "Enter the length of a side for the square: ";                         cin >> squ1;                         if (squ1 <=0)        //sanity check                         {                                 cout << "It is impossible to have a square with "                                         << "a side of "                                         << squ1                                         << endl << endl;                         }                         else                         {                                 cout << endl;                                 area = squ1 * squ1;        //calculate area                                 cout << "The area of a square with a side of "                                         << squ1                                         << " is "                                         << area                                         << endl << endl;                         }         }         else if (let1 == 't')         {                         cout << "Enter the length of the base for the triangle: ";                         cin >> tri1;                         cout << endl;                         if (tri1 <=0)        //sanity check                         {                                 cout << "It is impossible to have a triangle with a "                                         << "base whose length is "                                         << tri1                                         << endl << endl;                         }                         else                         {                                 cout << "Enter the height of the triangle: ";                                 cin >> tri2;                                 cout << endl <<endl;                                 if (tri2 <=0) //sanity check                                 {                                         cout << "It is imppossible to have a triangle with a "                                                 << "height of "                                                 << tri2                                                 << endl << endl;                                 }                                 else                                 {                                 area = tri1 * tri2 / 2;        //calculate area                                 cout << "The area of a triangle with a base of "                                         << tri1                                         << " and a height of "                                         << tri2                                         << " is "                                           << area                                         << endl << endl;                                 }                         }                 }         else         {                         cout << "This program can only recognize the "                                 << "lower-case letters s and t.  Sorry." << endl <<endl;         }                                 return; }        // End calculations.```
• 09-30-2003
lyx
I didn't get it... about your space thing, are you talking about the screen display, the forum code display or your code itself?
• 09-30-2003
sweetly
I don't know the right terminology, but here's how it displays when the program is executed:

******************************************

Type the letter s to compute the area of a square, or type t for a triangle: s

Enter the length of a side for the square: 3
The area of a square with a side of 3 is 9

Type the letter s to compute the area of a square, or type t for a triangle: t

Enter the length of the base for the triangle: 4

Enter the height of the triangle: 6

The area of a triangle with a base of 4 and a height of 6 is 12

Type the letter s to compute the area of a square, or type t for a triangle:

******************************
I'd like to keep the spaces only between each new "Type the letter s to compute...." and delete the ones in between so it looks like

space
Enter the length of the base:
Enter the height:
The area of a triangle is:
space
• 09-30-2003
lyx
Well, I think you're able to do it by yourself, really and without much efforts, so I'll let you do it; just remove the endl where you do'nt wnat a space.