Hi,

I just to know what code should I use to terminate the program when the user input the character n.
Here is the code I have so far

Dani AI

Generated

Good repair by — that removes the infinite loop. To make the program robust for real use, add input validation, verify the output file opens, guard the Heron formula against invalid sides, and accept upper- or lowercase answers. Prefer double for better precision and handle non-numeric input so the program doesn't spin or produce NaN results. The short example below shows a safe pattern you can drop into your main loop without changing the core flow you already have.

#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <cctype>

int main() {
    std::ofstream out("hw1Output.txt");
    if (!out) { std::cerr << "Cannot open output file\n"; return 1; }

    while (true) {
        double a,b,c;
        std::cout << "Enter three side lengths: ";
        if (!(std::cin >> a >> b >> c)) {
            std::cerr << "Numeric input required. Exiting.\n";
            break;
        }

        if (a <= 0 || b <= 0 || c <= 0 || !(a + b > c && a + c > b && b + c > a)) {
            std::cout << "Invalid triangle sides. Try again.\n";
        } else {
            double s = (a + b + c) / 2.0;
            double rad = s*(s-a)*(s-b)*(s-c);
            double area = (rad <= 0.0) ? 0.0 : std::sqrt(rad);
            std::cout << "Area = " << area << '\n';
            out << a << ',' << b << ',' << c << " -> " << area << '\n';
        }

        std::string reply;
        std::cout << "Calculate another (y/n)? ";
        std::cin >> reply;
        if (!reply.empty() && std::tolower(static_cast<unsigned char>(reply[0])) == 'n') break;
    }
    return 0;
}

Notes and cautions: check std::cin after numeric reads and clear/ignore leftover input on error. Validate the triangle with a + b > c (and permutations) before calling sqrt; a negative radicand means invalid input and yields NaN. Cast to unsigned char before std::tolower to avoid undefined behavior on signed char. Using an std::string for the y/n reply lets you accept "N" or "n" and ignore stray whitespace. These tweaks complement 's fix and protect the program from bad input for .

Recommended Answers

All 4 Replies

Gaack, I'm sorry. I accidentally killed your code when I changed this article from a code snippet to a discussion thread. Would you mind posting it again as a reply?

#include <iostream>
#include <cmath>
#include <fstream>           //for file I/O

using namespace std;

int main()
{
    ofstream outFile;               //file with the results

    // Declaration of variables
    float a, b, c; 
    float s;
    float Area;
    char answer;

    outFile.open("hw1Output.txt");  //Open the output file

    while (true)
    {
    // Prompt and read the lenghts of the sides of a triangle
    cout << " Please enter the lengths of the three sides of a triangle " << endl;
    cin >> a >> b >> c;

    // Computation of the semiperimeter and area of a triangle

    s = (a + b + c)/2;
    Area = sqrt(s * (s-a) * (s-b) * (s-c));

    // Display the input values and the resulting area

    cout << " The area of the triangle with sides : " << a << "," << " " << b << "," << " " << "and" << " " << c << " " << "is" << " " << Area << endl;
    cout << endl << endl;
    outFile << " The area of the triangle with sides : " << a << "," << " " << b << "," << " " << "and" << " " << c << " " << "is" << " " << Area << endl;

    cout<<"Do you want to calculate the area of another triangle (y/n)? "; 
    cin>>answer; 
    }

    if (answer = 'n')
    {

        cout<<"Have a nice day"<<endl;
    }


    outFile.close();                //close hw1Output.txt
    return 0;

}

As further apology, here's one possible solution:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    float a, b, c;
    float s;
    float Area;
    char answer = 'y';

    while (answer != 'n') {
        cout << " Please enter the lengths of the three sides of a triangle " << endl;
        cin >> a >> b >> c;

        s = (a + b + c)/2;
        Area = sqrt(s * (s-a) * (s-b) * (s-c));

        cout << " The area of the triangle with sides : " << a << "," << " " << b << "," << " " << "and" << " " << c << " " << "is" << " " << Area << endl;
        cout << endl << endl;

        cout<<"Do you want to calculate the area of another triangle (y/n)? ";
        cin>>answer;
    }

    cout<<"Have a nice day"<<endl;

    return 0;
}

In your original code the loop was basically infinite, so you need some way to break out of it. The value of answer is one option which I used above. Just make sure that answer has an appropriate starting value and you're golden.

Also note that the = operator is for assignment. It may have been a typo, but confusing = and == is a very common and often disastrous bug.

Cheers!

deceptikon , thank you very much for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.