0

I am new to this website and it told me that my post is mostly code. This is not important to my post, it is here just so I can fulfill the post parameters. Looking at the output, how do I change my code so that it doesn't keep looping incorrectly. I have tried everything and I just can't figure out how to make it work. Keep in mind that I am a beginner programmer. I don't know much syntax. How would I make this loop work using the most simplest syntax? I don't know why it loops once correctly, then displays everything before the for loop and then everything in the else block.

#include <iostream>
#include <string>
#include <istream>
#include <stdlib.h>

using namespace std;

int main()
{
    char repeat = 'n';

    string circle;
    string triangle;
    string rectangle;
    string shape;

    long double areaCircle;
    long double areaTriangle;
    long double areaRectangle;
    do
    {   
        cout << "Type the shape you want to find the area for.\n";
        cout << "Circle\n";
        cout << "Triangle\n";
        cout << "Rectangle\n";
        getline(cin,shape);

        if (shape == "circle")
        {
            long double r;
            cout << "Enter the radius of the circle.\n";
            cin >> r;
            areaCircle = (3.14)*(r*r);
            cout << "The area of the circle is " << areaCircle << endl;
            cout << "Enter 'Y' to repeat, 'N' to quit\n";
            cin >> repeat;          
        }
        else if (shape == "triangle")
        {
            long double h, b;
            cout << "Enter the base.\n";
            cin >> b;
            cout << "Enter the height.\n";
            cin >> h;
            areaTriangle = (b*h) / 2;
            cout << "The area of the triangle is " << areaTriangle << endl;
            cout << "Enter 'Y' to repeat, 'N' to quit\n";
            cin >> repeat;
        }
        else if (shape == "rectangle")
        {
            long double l, w;
            cout << "Enter the length.\n";
            cin >> l;
            cout << "Enter the width.\n";
            cin >> w;
            areaRectangle = l * w;
            cout << "The area of the rectangle is " << areaRectangle << endl;
            cout << "Enter 'Y' to repeat, 'N' to quit\n";
            cin >> repeat;
        }
        else if (shape !=circle || shape!= triangle || shape != rectangle)
        {
            cout << shape << "is not a valid shape.\n";
            cout << "Enter 'Y' to repeat, 'N' to quit\n";
            cin >> repeat;
        }
    } 
    while (repeat == 'y');
    system("pause");
    return 0;
}

Click here to see the output of the code

  • 1
    Hello @ThomasMcDonnell, Welcome to SO. What is exectly your question? – Alejandro Montilla Feb 13 '18 at 20:06
  • it doesn't loop the way i want it to. it does the first iteration fine but then repeats the first 4 cout statements twice. then it lets me input something. Why is it outputing the first 4 cout statements twice? –  Feb 13 '18 at 22:24
  • Duplicate of https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction . `cin >> repeat` leaves line feed character in the stream, and then `getline(cin,shape)` reads an empty line. – Igor Tandetnik Feb 14 '18 at 06:06
  • oh ok. I figured it out. thanks. I had to change the getline(cin,shape) to cin >> shape; –  Feb 15 '18 at 14:33

0 Answers0