CompSci is hard

This is a discussion on CompSci is hard within the C++ Programming forums, part of the General Programming Boards category; I am having some trouble with this program, and none of my friends are able to help me. My teacher ...

  1. #1
    IRLeeb
    Join Date
    Feb 2005
    Location
    Amston, CT
    Posts
    7

    Question CompSci is hard

    I am having some trouble with this program, and none of my friends are able to help me. My teacher is out, so we have been stuck with a substitute.

    Code:
    /*
    Len Bansavich III
    
      Period 6
      
    	Triangle Program
    	
    	  This program computes the type of triangle (both by angle and side length) when given the length of three sides.
    */
    
    #include <iostream.h>
    #include <windows.h>
    #include <iomanip.h>
    #include <math.h>
    
    void input(double &side1, double &side2, double &side3);
    void sort(double &side1, double &side2, double &side3, double temp);
    void angle(double &side1, double &side2, double &side3, char angleclass);
    void type(double &side1, double &side2, double &side3, char typeclass);
    void output(double &side1, double &side2, double &side3, char angleclass, char typeclass);
    
    int main()
    {
    	double side1; // side 1 variable
    	double side2; // side 2 variable
    	double side3; // side 3 variable
    	double temp; // temporary variable for sort
    	char angleclass[6]; // type of triangle array
    	char typeclass[11]; // type of triangle array
    	
    	input(side1, side2, side3);
    	
    	sort(side1, side2, side3, temp);
    	
    	angle(side1, side2, side3, angleclass[6]);
    	
    	type(side1, side2, side3, typeclass[11]);
    	
    	output(side1, side2, side3, angleclass[6], typeclass[11]);
    	
    	return 0;
    }
    
    // input function's definition
    
    void input(double &side1, double &side2, double &side3)
    {
    	do
    	{
    		cout << "Please enter the lengths of three sides of a triangle" << endl; 
    		cout << "(All need to be positive, sum of shortest must be greater than longest)" << endl;
    		cout << "Please enter the length of the first side." << endl;
    		cin >> side1;
    		cout << "Please enter the length of the second side." << endl;
    		cin >> side2;
    		cout << "Please enter the length of the third side." << endl;
    		cin >> side3;
    	}
    	while(side1 > 0 && side2 > 0 && side3 > 0);
    }
    
    // sort function's definition
    
    void sort(double &side1, double &side2, double &side3, double &temp)
    {	
    	if(side1 > side3 && side3 > side2)
    	{
    		temp = side2;
    		side2 = side3;
    		side3 = temp;
    	}
    	if(side2 > side1 && side1 > side3)
    	{
    		temp = side1;
    		side1 = side2;
    		side2 = temp;
    	}
    	if(side2 > side3 && side3 > side1)
    	{
    		temp = side1;
    		side1 = side2;
    		side2 = side3;
    		side3 = temp;
    	}
    	if(side3 > side1 && side1 > side2)
    	{
    		temp = side1;
    		side1 = side3;
    		side3 = side2;
    		side2 = temp;
    	}
    	if(side3 > side2 && side2 > side1) 
    	{
    		temp = side1;
    		side1 = side3;
    		side3 = temp;
    	}
    }
    
    // angle function's definition
    
    void angle(double &side1, double &side2, double &side3, char angleclass[])
    {
    	if((side1 * side1) + (side2 * side2) > (side3 * side3))
    	{
    		angleclass[0] = 'A';
    		angleclass[1] = 'c';
    		angleclass[2] = 'u';
    		angleclass[3] = 't';
    		angleclass[4] = 'e';
    		angleclass[5] = '\0';
    	}
    	if((side3 * side3) = (side1 * side1) + (side2 * side2))
    	{
    		angleclass[0] = 'R';
    		angleclass[1] = 'i';
    		angleclass[2] = 'g';
    		angleclass[3] = 'h';
    		angleclass[4] = 't';
    		angleclass[5] = '\0';
    	}
    	if((side1 * side1) + (side2 * side2) < (side3 * side3))
    	{
    		angleclass[0] = 'O';
    		angleclass[1] = 'b';
    		angleclass[2] = 't';
    		angleclass[3] = 'u';
    		angleclass[4] = 's';
    		angleclass[5] = 'e';
    		angleclass[6] = '\0';
    	}
    	
    }
    
    // type function's definition
    
    void type(double &side1, double &side2, double &side3, char typeclass[])
    {
    	if(side1 = side2 = side3)
    	{
    		typeclass[0] = 'E';
    		typeclass[1] = 'q';
    		typeclass[2] = 'u';
    		typeclass[3] = 'i';
    		typeclass[4] = 'l';
    		typeclass[5] = 'a';
    		typeclass[6] = 't';
    		typeclass[7] = 'e';
    		typeclass[8] = 'r';
    		typeclass[9] = 'a';
    		typeclass[10] = 'l';
    		typeclass[11] = '\0';
    	}	
    	if(side1 = side2 != side3 || side1 = side3 != side2 || side2 = side3 != side1)
    	{
    		typeclass[0] = 'I';
    		typeclass[1] = 's';
    		typeclass[2] = 'o';
    		typeclass[3] = 's';
    		typeclass[4] = 'c';
    		typeclass[5] = 'e';
    		typeclass[6] = 'l';
    		typeclass[7] = 'e';
    		typeclass[8] = 's';
    		typeclass[9] = '\0';
    	}
    	if(side1 != side2 != side3 || side2 != side3 != side1 || side3 != side1 != side2)
    	{
    		typeclass[0] = 'S';
    		typeclass[1] = 'c';
    		typeclass[2] = 'a';
    		typeclass[3] = 'l';
    		typeclass[4] = 'e';
    		typeclass[5] = 'n';
    		typeclass[6] = 'e';
    		typeclass[7] = '\0';
    	}
    }
    
    // output function's definition
    
    void output(double &side1, double &side2, double &side3, char angleclass[], char typeclass[])
    {
    	if(side1 > (side2 + side3))
    	{
    		cout << "The sides entered are not that of a triangle";
    	}	
    	else
    	{
    		cout << "Triangle Type = " << typeclass << endl;
    		cout << "Triangle Type 2 = " << angleclass << endl;
    	}
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    #include <iostream.h>
    #include <windows.h>
    #include <iomanip.h>
    #include <math.h>
    Use the newer headers if possible:
    Code:
    #include <iostream>
    #include <windows.h>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    Code:
    void sort(double &side1, double &side2, double &side3, double temp);
    
    int main()
    {
        ...
    
        double temp;
    
        ...
    
        sort(side1 ,side2 ,side3 ,temp );
    Since temp is passed by value here there is no chance the called function (sort) will be able to modify the value in the main function. This, combined with the fact you aren't initializing temp to anything prior to calling sort, makes me wonder why you are even bothering to pass this value into the function at all. Make temp a local variable to the sort function.

    Code:
    void angle(double &side1, double &side2, double &side3, char angleclass);
    void type(double &side1, double &side2, double &side3, char typeclass);
    void output(double &side1, double &side2, double &side3, char angleclass, char typeclass);
    
    
    int main()
    {
        ...
    
        char angleclass[6]; // type of triangle array
        char typeclass[11]; // type of triangle array
    
        ...
    
        angle(side1, side2, side3, angleclass[6]);
    
        type(side1, side2, side3, typeclass[11]);
    
        output(side1, side2, side3, angleclass[6], typeclass[11]);
    
    ...
    
    void angle(double &side1, double &side2, double &side3, char angleclass[])
    {
        ...
    
    
    void type(double &side1, double &side2, double &side3, char typeclass[])
    {
        ...
    
    void output(double &side1, double &side2, double &side3, char angleclass[], char typeclass[])
    {
        ...
    First your function definitions don't match your prototypes. Your prototypes should be expecting a char* and not just a simple char. When passing the values into the function, you would just use the plain unadorned variable name without anything like [6] or [11]. As you have it, you are attempting to pass characters at invalid indicies, i.e. typeclass[11] is one beyond the end of the array which stops at typeclass[10].

    Code:
    angleclass[0] = 'A';
    angleclass[1] = 'c';
    angleclass[2] = 'u';
    angleclass[3] = 't';
    angleclass[4] = 'e';
    angleclass[5] = '\0';
    Just use strcpy (you'd also want to include <cstring> for that) instead for all of those similar blocks of code:
    Code:
    strcpy(angleclass,"Acute");
    Last edited by hk_mp5kpdw; 06-06-2005 at 11:24 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    IRLeeb
    Join Date
    Feb 2005
    Location
    Amston, CT
    Posts
    7
    Okay, here is my program so far, but it still doesn't work.
    Code:
    /*
    Len Bansavich III
    
      Period 6
      
    	Triangle Program
    	
    	  This program computes the type of triangle (both by angle and side length) when given the length of three sides.
    */
    
    #include <iostream.h>
    #include <windows.h>
    #include <iomanip.h>
    #include <math.h>
    
    void input(double &side1, double &side2, double &side3);
    void sort(double &side1, double &side2, double &side3);
    void angle(double &side1, double &side2, double &side3, char* angleclass);
    void type(double &side1, double &side2, double &side3, char* typeclass);
    void output(double &side1, double &side2, double &side3, char* angleclass, char* typeclass);
    
    int main()
    {
    	double side1; // side 1 variable
    	double side2; // side 2 variable
    	double side3; // side 3 variable
    	char* angleclass; // type of triangle array
    	char* typeclass; // type of triangle array
    	
    	input(side1, side2, side3);
    	
    	sort(side1, side2, side3);
    	
    	angle(side1, side2, side3, angleclass);
    	
    	type(side1, side2, side3, typeclass);
    	
    	output(side1, side2, side3, angleclass, typeclass);
    	
    	return 0;
    }
    
    // input function's definition
    
    void input(double &side1, double &side2, double &side3)
    {
    	do
    	{
    		cout << "Please enter the lengths of three sides of a triangle" << endl; 
    		cout << "(All need to be positive, sum of shortest must be greater than longest)" << endl;
    		cout << "Please enter the length of the first side." << endl;
    		cin >> side1;
    		cout << "Please enter the length of the second side." << endl;
    		cin >> side2;
    		cout << "Please enter the length of the third side." << endl;
    		cin >> side3;
    	}
    	while(side1 <= 0 && side2 <= 0 && side3 <= 0);
    }
    
    // sort function's definition
    
    void sort(double &side1, double &side2, double &side3)
    {	
    	double temp; // temporary sort variable
    	
    	if(side1 > side3 && side3 > side2)
    	{
    		temp = side2;
    		side2 = side3;
    		side3 = temp;
    	}
    	if(side2 > side1 && side1 > side3)
    	{
    		temp = side1;
    		side1 = side2;
    		side2 = temp;
    	}
    	if(side2 > side3 && side3 > side1)
    	{
    		temp = side1;
    		side1 = side2;
    		side2 = side3;
    		side3 = temp;
    	}
    	if(side3 > side1 && side1 > side2)
    	{
    		temp = side1;
    		side1 = side3;
    		side3 = side2;
    		side2 = temp;
    	}
    	if(side3 > side2 && side2 > side1) 
    	{
    		temp = side1;
    		side1 = side3;
    		side3 = temp;
    	}
    }
    
    // angle function's definition
    
    void angle(double &side1, double &side2, double &side3, char* angleclass)
    {
    	double dside1 = side1 * side1;
    	double dside2 = side2 * side2;
    	double dside3 = side3 * side3; 
    	
    	if(dside1 + dside2 > dside3)
    	{
    		strcpy(angleclass,"Acute");
    	}
    	if(dside3 = dside1 + dside2)
    	{
    		strcpy(angleclass,"Right");
    	}
    	if(dside1 + dside2 < dside3)
    	{
    		strcpy(angleclass,"Obtuse");
    	}
    	
    }
    
    // type function's definition
    
    void type(double &side1, double &side2, double &side3, char* typeclass)
    {
    	
    	if(side1 == side2 && side2 == side3)
    	{
    		strcpy(typeclass,"Equilateral");
    	}	
    	if(side1 == side2 != side3 || side1 == side3 != side2 || side2 == side3 != side1)
    	{
    		strcpy(typeclass,"Isosceles");
    	}
    	if(side1 != side2 != side3 || side2 != side3 != side1 || side3 != side1 != side2)
    	{
    		strcpy(typeclass,"Scalene");
    	}
    }
    
    // output function's definition
    
    void output(double &side1, double &side2, double &side3, char* angleclass, char* typeclass)
    {
    	if(side1 > (side2 + side3))
    	{
    		cout << "The sides entered are not that of a triangle";
    	}	
    	else
    	{
    		cout << "Triangle Type = " << typeclass << endl;
    		cout << "Triangle Type 2 = " << angleclass << endl;
    	}
    }
    I have no errors, but 8 warnings.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
    if(side1 == side2 != side3 || side1 == side3 != side2 || side2 == side3 != side1)
    You comparison chaining is not doing what you think it is.

    side1 == side2 is going to be true or false and then you'll have:
    true != side3 or false != side3

    so I will give you this one and you can change the other. so using your check for equilateral, I'll just check for not equilateral

    Code:
    if(!(side1 == side2 && side2 == side3)&& ((side1 == side2)||(side1 == side3)||(side2 == side3)))
    You also have some issues where you are strcpy'ing strings to char* that have to memory allocated to them.

    When you create you char* you need to allocate some memory like
    Code:
    char* angleclass = new char[20];// or whatever size you need
    and make sure you delete[] angleclass; at the end
    Last edited by Darryl; 06-06-2005 at 03:24 PM.

  5. #5
    IRLeeb
    Join Date
    Feb 2005
    Location
    Amston, CT
    Posts
    7
    Thank you both so very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting data from a laptop hard drive
    By DavidP in forum Tech Board
    Replies: 6
    Last Post: 06-13-2009, 07:02 AM
  2. Detect SCSI Hard Drive Serial Number
    By mercury529 in forum Windows Programming
    Replies: 3
    Last Post: 10-17-2006, 06:23 PM
  3. Replies: 2
    Last Post: 07-06-2005, 07:11 PM
  4. Trinary Hard Drive
    By nickname_changed in forum Tech Board
    Replies: 14
    Last Post: 05-13-2005, 10:01 AM
  5. hard drive problems continually increasing
    By DavidP in forum Tech Board
    Replies: 5
    Last Post: 11-21-2002, 09:48 PM

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