Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

function - C++ Beginner Infinite Loop When Input Wrong Data Type and Help Evaluate My Code

I have an assignment to create a menu-based program to calculate area of several shapes using user-defined function. My code creates an infinite loop when a user input a char instead of a number. What should I do? Also, I would like if someone could evaluate my code, Is there anything I should change or modify? I'm still new to C++.

Input: 1. a 2. j

#include <iostream>
#include <cmath>
using namespace std;

float Circle(double r1);
float Rectangle(double length, double width);
float Triangle(float s1, float s2, float s3);
float Cylinder(double height, double r2);
float Square(double l1, double l2);

int main()
{  
    char o, d;
    do
    {//List  of what shapes the program can calculate
        cout << " Calculator for area of shapes " << endl;
        cout << endl;
        cout << " List of Area: " << endl;
        cout << " A. Circle " << endl;
        cout << " B. Rectangle " << endl;
        cout << " C. Triangle " << endl;
        cout << " D. Cylinder " << endl;
        cout << " E. Square " << endl;
        cout << " X. Exit " << endl;
        cout << endl;
        //Choose what shape to calculate
        cout << " Please enter the area you wish to calculate: ";
        cin >> o;
        cout << endl;
        //Program determine what calculation to use
        switch (o)
        {
        case 'a'://For Circle
        case 'A':

            double r1;
            while (true)
            {
                cout << " Radius of Circle: ";
                cin >> r1;
                if (r1 > 0)
                {
                    Circle(r1);
                    cout << endl;
                    break;
                }
                else
                {// If the input is less than 0 an error message will occur and user need to reinput the data
                    cout << " ERROR! NOT A CIRCLE. Please input value more than 0." << endl;
                }
            }
            break;

        case 'b'://For Rectangle
        case 'B':
            double length, width;
            while (true)
            {
                cout << " Length of Rectangle: ";
                cin >> length;
                cout << " Width of Rectangle: ";
                cin >> width;
                if (length > 0 && width > 0)
                {
                    Rectangle(length, width);
                    cout << endl;
                    break;
                }
                else
                {
                    cout << " ERROR! Not A Rectangle. Please input value more than 0 " << endl;
                }
            }

        case 'c'://For Triangle
        case 'C':
            float s1, s2, s3;
            while (true)
            {
                cout << " Length of side A: ";
                cin >> s1;
                cout << " Length of side B: ";
                cin >> s2;
                cout << " Length of side C: ";
                cin >> s3;
                if ((s1 + s2) > s3 && (s2 + s3) > s1 && (s3 + s1) > s2)
                {
                    Triangle(s1, s2, s3);
                    cout << endl;
                    break;
                }
                else
                {
                    cout << " ERROR!!! NOT A TRIANGLE. Please input value more than 0 " << endl;
                }
            }

        case 'd'://For Cylinder
        case 'D':
            double height, r2;
            cout << " Radius of Cylinder: ";
            cin >> r2;
            cout << "Height of Cylinder: ";
            cin >> height;
            while (true)
            {
                if (r2 > 0 && height > 0)
                {
                    Cylinder(height, r2);
                    break;
                }
                else
                {
                    cout << " ERROR!!! NOT A CYLINDER. Please input value more than 0 " << endl;
                }
            }

        case 'e'://For Square
        case 'E':
            double l1, l2;
            while (true)
            {
                cout << " Length of side 1: ";
                cin >> l1;
                cout << " Length of side 2: ";
                cin >> l2;
                if (l1 > 0 && l2 > 0)
                {
                    Square(l1, l2);
                    break;
                }
                else
                {
                    cout << " ERROR!!! NOT A SQUARE. " << endl;
                }
            }

        case 'x':
        case 'X'://Program end
            cout << " ---- PROGRAM END ---- ";
            exit(0);

        default: // Error message occur if user don't
            cout << " ERROR!! PLEASE INPUT RIGHT LETTER ";
            cout << endl;
        }
        // user can choose to use program again or not
        cout << " Do you wish to continue using the calculator?(Y/N): ";
        cin >> d;
        cout << endl;

    } while (d == 'y' || d == 'Y');
        if (d == 'n' || d == 'N')
        {
            cout << " ---- PROGRAM END ---- ";
        }

    return 0; 
}
float Circle(double r1)//Calculation for Circle
{
    double area;
    const double pi = 3.14; 
    area = pi * (r1 * r1); //Formula for area of Circle
    cout << " Area of Circle: " << area; 
    cout << endl;
    return 0;
}
float Rectangle(double length, double width)//Calculation for Rectangle
{
    double area;
    area = length * width;//Formula for area of Rectangle
    cout << " Area of Rectangle: " << area;
    cout << endl;
    return 0;
}
float Triangle(float s1, float s2, float s3)//Calculation for Triangle
{
    float area, s;
    s = (s1 + s2 + s3) / 2;
    area = sqrt(s * (s - s1) * (s - s2) * (s - s3));//Formula for area of Triangle
    cout << " Area of triangle: " << area << endl;
    cout << endl;
    return 0;
}
float Cylinder(double height, double r2)//Calculation for Cylinder
{
    double area; 
    const double pi = 3.14;
    area = 2 * pi * r2 * (r2 + height);//Formula for area of Cylinder
    cout << endl;
    return 0;
}
float Square(double l1, double l2)//Calculation for Square
{
    double area;
    area = l1 * l2;//Formula for area of Square
    cout << " Area of Square: " << area << endl;
    cout << endl;
    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With float r1; cin >> r1; and non-numeric input, there is nothing consumed from input. Repeating cin >> r1 results in an endless loop.

What to do?

  1. Check whether cin >> r1 was successful: if (cin >> r1) { /* process r1 */ }
  2. If not successful, do something to consume wrong input: else { cin.clear(); cin.ignore(); }

std::ios::clear()

Sets the stream error state flags by assigning them the value of state. By default, assigns std::ios_base::goodbit which has the effect of clearing all error state flags.

std::istream::ignore()

Extracts and discards characters from the input stream until and including delim.

Example:

#include <iostream>
#include <limits>

int main()
{
  double value = 0.0;
  for (;;) {
    std::cout << "Value: ";
    if (std::cin >> value) {
      break; // success -> bail out of loop
    } else {
      std::cerr << "Wrong input!
";
      std::cin.clear(); // reset state
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '
'); // consume wrong input
    }
  }
  std::cout << "Value: " << value << '
';
  return 0;
}

Output:

Value: Hello
Wrong input!
Value: 1.23
Value: 1.23

Live Demo on coliru


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...