please help, i'm really stuck

This is a discussion on please help, i'm really stuck within the C++ Programming forums, part of the General Programming Boards category; I know this is a mess... I got parts of it to work (the square part) but when I add ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    please help, i'm really stuck

    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.

  2. #2
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Some pieces of advice:
    1. Indent your code properly!
    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;
    }
    [edit]My indentation is bad because the original code used tabs instead of spaces.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    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.

  4. #4
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I didn't get it... about your space thing, are you talking about the screen display, the forum code display or your code itself?

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    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

  6. #6
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 08:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 05:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:40 PM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 08:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 09:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21