2015-11-25 43 views
-2

所以這可能是我的第一個基本程序。它接受用戶關於三角形的輸入,然後對它們進行分類並輸出給出的數據所暗示的三角形類型。我一直在猜測,如果事情只是用C++來玩,但我遇到了問題。我沒有從我的程序中得到預期的結果(雖然以前不完美),而不是輸入邊長,我輸入第一邊的長度,它會自動跳過其他兩邊的長度並繼續編碼。我的代碼中有錯誤,可能是語法錯誤,或者我找不到if語句。編程新手

#include <iostream> 

using std::cout; 
using std::cin; 
int sideA, sideB, sideC, angleA, angleB, angleC, longest, middle, shortest, sumbc, sumac, sumab; 
int main() 
{ 
cout << "Enter an angle for angle A\n"; 
cin >> angleA; 
cout << "Enter an angle for angle B\n"; 
cin >> angleB; 
cout << "Enter an angle for angle C\n"; 
cin >> angleC; 
sumbc = angleB+angleC; 
sumac = angleA+angleC; 
sumab = angleA+angleB; 
if(angleA + angleB + angleC == 180){ // This confirms that the angles add to 180 to form a triangle 
     cout << "This is a triangle!(Based on Angles)\n"; 

if(angleA == 90){ // These are for right angle triangles 
     cout << "This is a real right triangle!(angleA is 90 degrees..)\n"; 
     cout << "Please enter the side lengths now\n"; 
     cout << "Side 1:\n"; 
     cin >> sideA; 
     cout << "Side 2:\n"; 
     cin >> sideB; 
     cout << "Side 3:\n"; 
     cin >> sideC; 
     if(sideA > sideB && sideA > sideC){ 
      longest = sideA; 
      cout << "Side 1 is the hypotenuse!\n"; 
     } 
     else if(sideB > sideA && sideB > sideC) { 
      cout << "Side 2 is the hypotenuse\n"; 
      longest = sideB; 
     } 
     else if(sideC > sideA && sideC > sideB){ 
      cout << "The hypotenuse is Side 3\n"; 
      longest = sideC; 
     } 
     if(longest == sideC && sideA == sideB) { //Determines what kind of triangle it is if the longest side is sideC 
       cout << "This triangle is Right and Isosceles\n "; 

     } 
     else if(longest == sideB && sideC == sideA){ 
       cout << "This triangle is Right and Isosceles\n "; 

     } 
     else if(longest == sideA && sideB == sideC){ 
      cout << "This triangle is Right and Isosceles\n "; 
     } 
     else { 
      cout << "This triangle is right and Scalene!\n"; 
     } 


    } 
if(angleB == 90){ // These are for right angle triangles 

     cout << "This is a real right triangle!(angleB is 90 degrees..)\n"; 
     cout << "Please enter the side lengths now\n"; 
     cout << "Side 1:\n"; 
     cin >> sideA; 
     cout << "Side 2:\n"; 
     cin >> sideB; 
     cout << "Side 3:\n"; 
     cin >> sideC; 
     if(sideA > sideB && sideA > sideC){ //Finds longest side 
      longest = sideA; 
      cout << "Side 1 is the hypotenuse!\n"; 
     } 
     else if(sideB > sideA && sideB > sideC) { 
      cout << "Side 2 is the hypotenuse\n"; 
      longest = sideB; 
     } 
     else if(sideC > sideA && sideC > sideB){ 
      cout << "The hypotenuse side is Side 3"; 
      longest = sideC; 
     } 
     if(longest == sideC && sideA == sideB) { //Determines what kind of triangle it is if the longest side is sideC 
       cout << "This triangle is Right and Isosceles\n "; 

     } 
     else if(longest == sideB && sideC == sideA){ 
       cout << "This triangle is Right and Isosceles\n "; 

     } 
     else if(longest == sideA && sideB == sideC){ 
      cout << "This triangle is Right and Isosceles\n "; 
     } 
     else { 
      cout << "This triangle is right and scalene!\n"; 
     } 


    } 
if(angleC == 90){ // These are for right angle triangles 


     cout << "This is a real right triangle!(angleC is 90 degrees..)\n"; 
     cout << "Please enter the side lengths now\n"; 
     cout << "Side 1:\n"; 
     cin >> sideA; 
     cout << "Side 2:\n"; 
     cin >> sideB; 
     cout << "Side 3:\n"; 
     cin >> sideC; 
     if(sideA > sideB && sideA > sideC){ 
      longest = sideA; 
      cout << "Side 1 is the hypotenuse!\n"; 
     } 
     else if(sideB > sideA && sideB > sideC) { 
      cout << "Side 2 is the hypotenuse\n"; 
      longest = sideB; 
     } 
     else if(sideC > sideA && sideC > sideB){ 
      cout << "The hypotenuse is Side 3"; 
      longest = sideC; 
     } 
     if(longest == sideC && sideA == sideB) { //Determines what kind of triangle it is if the longest side is sideC 
       cout << "This triangle is Right and Isosceles\n "; 

     } 
     else if(longest == sideB && sideC == sideA){ 
       cout << "This triangle is Right and Isosceles\n "; 

     } 
     else if(longest == sideA && sideB == sideC){ 
      cout << "This triangle is Right and Isosceles\n "; 
     } 
     else { 
      cout << "This triangle is right and scalene!\n"; 
     } 


    } 
if(angleA == angleB && angleB == angleC){ // This is for equiangular/equilateral triangles 

      cout << "This is an equiangular/equilateral triangle.\n "; 
      cout << "Please enter the side lengths now\n"; 
      cout << "Side 1:\n"; 
      cin >> sideA; 
      int periequal = sideA*3; 
      int areaequal = sideA*sideA*.5; 
      cout << "The area is : " << areaequal << std::endl << "The perimeter is: " << periequal << std::endl; 
      cout << "This triangle is Isosceles, Equiangular, Equilateral, and Acute "; 


      } 

    if(angleA < 90 && sumbc != 120 && angleB < 90 && angleC < 90){ //deals with acute triangles 
     cout << "This is an acute triangle!\n"; 
     cout << "Please enter the side lengths now\n"; 
     cout << "Side 1:\n"; 
     cin >> sideA; 
     cout << "Side 2:\n"; 
     cin >> sideB; 
     cout << "Side 3:\n"; 
     cin >> sideC; 
     int AcuteIsoPeri = sideA + sideB + sideC; 
      if (sideA == sideB){ 
        cout << "This is an Acute, Isosceles Triangle. \n" << "With a perimeter of: " << AcuteIsoPeri; 

      } 
      else if(sideA == sideC){ 
       cout << "This is an Acute, Isosceles Triangle. \n" << "With a perimeter of: " << AcuteIsoPeri; 
      } 
      else if(sideB == sideC){ 
       cout << "This is an Acute, Isosceles Triangle. \n" << "With a perimeter of: " << AcuteIsoPeri; 
      } 
      else{ 
       cout << "This is an Acute, Scalene Triangle. \n" << "With a perimeter of " << AcuteIsoPeri; 
      } 

    } //end of angleA if statement 
    if(angleA > 90){ //Deals with obtuse 
    cout << "This angle is Obtuse, angle A is greater than 90.."; 
    cout << "Please enter the side lengths now\n"; 
     cout << "Side 1:\n"; 
     cin >> sideA; 
     cout << "Side 2:\n"; 
     cin >> sideB; 
     cout << "Side 3:\n"; 
     cin >> sideC; 
      if (sideA == sideB){ 
        cout << "This is an Obtuse, Isosceles Triangle. \n"; 

      } 
      else if(sideA == sideC){ 
       cout << "This is an Obtuse, Isosceles Triangle. \n"; 
      } 
      else if(sideB == sideC){ 
       cout << "This is an Obtuse, Isosceles Triangle. \n"; 
      } 
      else{ 
       cout << "This is an Obtuse, Scalene Triangle. \n"; 
      } 
} 
     else if(angleB > 90){ 
    cout << "This angle is Obtuse, angle B is greater than 90.."; 
    cout << "Please enter the side lengths now\n"; 
     cout << "Side 1:\n"; 
     cin >> sideA; 
     cout << "Side 2:\n"; 
     cin >> sideB; 
     cout << "Side 3:\n"; 
     cin >> sideC; 
         if (sideA == sideB){ 
        cout << "This is an Obtuse, Isosceles Triangle. \n"; 

      } 
      else if(sideA == sideC){ 
       cout << "This is an Obtuse, Isosceles Triangle. \n"; 
      } 
      else if(sideB == sideC){ 
       cout << "This is an Obtuse, Isosceles Triangle. \n"; 
      } 
      else{ 
       cout << "This is an Obtuse, Scalene Triangle. \n"; 
      } 
} 
     else if(angleC > 90){ 
     cout << "This angle is Obtuse, angle C is greater than 90.."; 
     cout << "Please enter the side lengths now\n"; 
     cout << "Side 1:\n"; 
     cin >> sideA; 
     cout << "Side 2:\n"; 
     cin >> sideB; 
     cout << "Side 3:\n"; 
     cin >> sideC; 
        if (sideA == sideB){ 
        cout << "This is an Obtuse, Isosceles Triangle. \n"; 

      } 
     else if(sideA == sideC){ 
       cout << "This is an Obtuse, Isosceles Triangle. \n"; 
      } 
     else if(sideB == sideC){ 
       cout << "This is an Obtuse, Isosceles Triangle. \n"; 
      } 
     else{ 
       cout << "This is an Obtuse, Scalene Triangle. \n"; 
      } 
} 
     } 




//scalene, obtuse, acute 
return 0; 
} 

所以我可能不知道我在做什麼,我是一個可怕的程序員什麼的哈哈,但這裏是我所得到的,當我把這個信息: 信息:角度是40,114,26 ..它認爲這是對的,這是正確的,然後它要求的邊長也是我打算做的。然後我把第一邊的長度(6.4)並且程序跳到最後一次我按回車。我可能犯了一個我無法找到的語法錯誤,或者如果語句錯誤,我使用了這種方式。如果你能幫助這個混亂,將不勝感激! (開始研究C++的晚上,並希望將其應用到真實的東西是這個怪物是如何產生的。)Examle of Proper Output ... Example of Wrong Output

+1

你有一個邏輯錯誤。程序編譯並鏈接正常。在語法錯誤的情況下,程序不會編譯 – Nandu

+0

@Nandu哦好吧,謝謝你,我會更新這個問題。 – Noah

+0

歡迎來到Stack Overflow。我無法重現您的錯誤。當你編寫代碼時,從小而簡單的工作開始,這是一個好主意,然後再逐步增加一點複雜性,在每一步都進行測試。這樣你就不會在一個巨大的程序中尋找一個可能在任何地方的錯誤。如果你真的那麼做了,試着將代碼簡化成一個[最小完整的例子](http://stackoverflow.com/help/mcve),這是產生錯誤的最簡單的例子。這會讓狩獵更容易,對你和我們來說都是如此。 – Beta

回答

0
int sideA, sideB, sideC, angleA, angleB, angleC, longest, middle, shortest, sumbc, sumac, sumab; 

您的變量全部被typeint。這些只有整數。你可能想的類型是floatdouble,這是浮點類型,可容納實數:

double sideA, sideB, sideC, angleA, angleB, angleC, longest, middle, shortest, sumbc, sumac, sumab; 

你開始使用這些類型之後,你將不得不與這些線路的問題:

if(angleA + angleB + angleC == 180) 
if(angleA == 90) 

問題是,即使您輸入的值可能會在紙張上添加到180,在計算機中,50.0 + 50.0 + 80.0可能等於179.999992或類似的值。請參閱What every programmer should know about floating point math

您確定這三條if語句中的一條總是會評估爲true

if(sideA > sideB && sideA > sideC){ 
     longest = sideA; 
     cout << "Side 1 is the hypotenuse!\n"; 
    } 
    else if(sideB > sideA && sideB > sideC) { 
     cout << "Side 2 is the hypotenuse\n"; 
     longest = sideB; 
    } 
    else if(sideC > sideA && sideC > sideB){ 
     cout << "The hypotenuse is Side 3\n"; 
     longest = sideC; 
    } 

如果沒有,longest不會有過分配一個值,你會得到意想不到的效果。

你的代碼還有其他問題,但我停止看這一點。祝你好運。

+0

最長僅適用於直角三角形。那麼它是否需要一個值,如果它只適用於三角形是直角三角形?而且由於我的工作很快,而且我的知識非常有限,所以我確信這個計劃的很少一部分是正確的或有效的。 – Noah

0

短的問題是,你想放放漂浮/加倍到一個整數。當你混合類型時,cin不喜歡它。如果你輸入字母'a',也會發生同樣的情況。嘗試使用浮動而不是整數。

編輯:澄清我的意思是讓你的側面和角度變成浮動。只要我沒有輸入帶小數點的數字,該程序就可以爲我工作。

+0

我還沒有了解「浮點」數據類型,並解決了它似乎的問題。浮點數是什麼,我應該總是使用低位數十進制數? – Noah

+0

取決於你需要什麼。浮動和雙打可以但不總是有小數位。這取決於號碼的大小。我強烈建議現在查看,因爲浮動四捨五入會導致你的錯誤,這將讓你搜索幾天。 – soulsabr