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

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.