所以這可能是我的第一個基本程序。它接受用戶關於三角形的輸入,然後對它們進行分類並輸出給出的數據所暗示的三角形類型。我一直在猜測,如果事情只是用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
你有一個邏輯錯誤。程序編譯並鏈接正常。在語法錯誤的情況下,程序不會編譯 – Nandu
@Nandu哦好吧,謝謝你,我會更新這個問題。 – Noah
歡迎來到Stack Overflow。我無法重現您的錯誤。當你編寫代碼時,從小而簡單的工作開始,這是一個好主意,然後再逐步增加一點複雜性,在每一步都進行測試。這樣你就不會在一個巨大的程序中尋找一個可能在任何地方的錯誤。如果你真的那麼做了,試着將代碼簡化成一個[最小完整的例子](http://stackoverflow.com/help/mcve),這是產生錯誤的最簡單的例子。這會讓狩獵更容易,對你和我們來說都是如此。 – Beta