Почему моя программа отдельно не читает оператор else в моем цикле while в конце. Что бы я ни делал, это всегда включает в себя что-то еще

#if-statement #while-loop

Вопрос:

Вот мой код:

 #include <iostream>
using namespace std;
```c  
int main()
{
    do
    {
        int sideA, sideB, sideC;
        cout << "Please enter side A: ";
        cin >> sideA;
        while (sideA < 0) {
            cout << "Value entered must be positive!" << endl;
            cout << "Please enter side A: ";
            cin >> sideA;
        }
        cout << "Please enter side B: ";
        cin >> sideB;
        while (sideB < 0) {
            cout << "Value entered must be positive!" << endl;
            cout << "Please enter side B: ";
            cin >> sideB;
        }
        cout << "Please enter side C: ";
        cin >> sideC;
        while (sideC < 0) {
            cout << "Value entered must be positive!" << endl;
            cout << "Please enter side C: ";
            cin >> sideC;
        }
        int aSquare = sideA * sideA;
        int bSquare = sideB * sideB;
        int cSquare = sideC * sideC;
        if (sideA == sideB amp;amp; sideA == sideC amp;amp; sideB == sideC)
            cout << "This is an EQUILATERAL triangle!(all sides are equal)" << endl;
        else if (sideA == sideB amp;amp; sideA != sideC amp;amp; sideB != sideC)
            cout << "This is an ISOSCELES triangle, where only two sides are equal" << endl;
        else if (sideA != sideB amp;amp; sideA == sideC amp;amp; sideB != sideC)
            cout << "This is an ISOSCELES triangle, where only two sides are equal" << endl;
        else if (sideA != sideB amp;amp; sideA != sideC amp;amp; sideB == sideC)
            cout << "This is an ISOSCELES triangle, where only two sides are equal" << endl;
        else if (cSquare==aSquare bSquare)
            cout << "This is a RIGHT triangle!" << endl;
        else if (aSquare == cSquare   bSquare)
            cout << "This is a RIGHT triangle!" << endl;
        else if (bSquare == aSquare   cSquare)
            cout << "This is a RIGHT triangle!" << endl;
        else
            cout << "This is a OTHER kind of triangle, not ISOSCELES, not RIGHT, not EQUILATERAL" << endl;
        int repeat;
        cout << "Would you like to repeat( 1-YES, 2-NO)";
        cin >> repeat;
        if (repeat == 1)
            continue;
        if (repeat == 2)
            break;
    }
    while (true);
}
 

В нем не должно быть утверждения: «Это ДРУГОЙ вид треугольника, не РАВНОБЕДРЕННЫЙ, не ПРАВИЛЬНЫЙ, не РАВНОСТОРОННИЙ». он также выполняет равносторонние и правильные условия правильно, но даже не переходит в другое, если для равнобедренных.

Я много чего перепробовал, но безрезультатно.

Пример вывода:

 Please enter side A: 1
Please enter side B: -10
Value entered must be positive!
Please enter side B: 1
Please enter side C: 1
This is an EQUILATERAL triangle!(all sides are equal)
This is a OTHER kind of triangle, not ISOSCELES, not RIGHT, not EQUILATERAL
Would you like to repeat( 1-YES, 2-NO)
1
Please enter side A: 1
Please enter side B: 1
Please enter side C: 2
This is a OTHER kind of triangle, not ISOSCELES, not RIGHT, not EQUILATERAL
Would you like to repeat( 1-YES, 2-NO)
 

Комментарии:

1. Чтобы отформатировать код здесь, вставьте его в редактор, выберите часть кода и щелкните значок кода на панели инструментов редактора.

Ответ №1:

Ваша программа работает:

 #include<iostream>
using namespace std;
int main() { 
    while (true) { 
        int sideA, sideB, sideC; 
        cout << "Please enter side A: "; 
        cin >> sideA; 
        while (sideA < 0) { 
            cout << "Value entered must be positive!" << endl; 
            cout << "Please enter side A: "; 
            cin >> sideA; 
        } 
        cout << "Please enter side B: "; 
        cin >> sideB; 
        while (sideB < 0) {     
            cout << "Value entered must be positive!" << endl; 
            cout << "Please enter side B: "; 
            cin >> sideB; 
        } 
        cout << "Please enter side C: "; 
        cin >> sideC; 
        while (sideC < 0) { 
            cout << "Value entered must be positive!" << endl; 
            cout << "Please enter side C: "; 
            cin >> sideC; 
        } 
        int aSquare = sideA * sideA; 
        int bSquare = sideB * sideB; 
        int cSquare = sideC * sideC; 
        if (sideA == sideB amp;amp; sideA == sideC amp;amp; sideB == sideC) 
            cout << "This is an EQUILATERAL triangle!(all sides are equal)" << endl; 
        else if (sideA == sideB amp;amp; sideA == sideC amp;amp; sideB != sideC) 
            cout << "This is an ISOSCELES triangle, where only two sides are equal" << endl; 
        else if (sideA == sideB amp;amp; sideA != sideC amp;amp; sideB == sideC) 
            cout << "This is an ISOSCELES triangle, where only two sides are equal" << endl; 
        else if (sideA != sideB amp;amp; sideA == sideC amp;amp; sideB == sideC) 
            cout << "This is an ISOSCELES triangle, where only two sides are equal" << endl;
        else if (cSquare==aSquare bSquare)
            cout << "This is a RIGHT triangle!" << endl;
        else if (aSquare == cSquare   bSquare)
            cout << "This is a RIGHT triangle!" << endl;
        else if (bSquare == aSquare   cSquare)
            cout << "This is a RIGHT triangle!" << endl;
        else
                cout << "This is a OTHER kind of triangle, not ISOSCELES, not RIGHT, not EQUILATERAL" << endl;
        int repeat;
            cout << "Would you like to repeat( 1-YES, 2-NO)";
            cin >> repeat;
        if (repeat == 1)
            continue;
        if (repeat == 2)
            break;
    }
}
 
 Please enter side A: 1
Please enter side B: 1
Please enter side C: 1
This is an EQUILATERAL triangle!(all sides are equal)
Would you like to repeat( 1-YES, 2-NO)2
 

Комментарии:

1. Спасибо! я не уверен, почему это не сработало в первую очередь. странно.