Xcode8-程序执行不正确 [英] Xcode8 - Program is not executing correctly

查看:117
本文介绍了Xcode8-程序执行不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然使用Xcode8表示它正在运行,但是没有输出.

While using Xcode8 says that it is running but there is no output.

输出:

10

请输入矩形的长度:

10

请输入矩形的宽度:

矩形的长度为10.00

The length for the rectangle is 10.00

矩形的宽度是10.00

The width for the rectangel is 10.00

矩形的面积为100.00

The area for a rectangle is 100.00

圆的半径为10.00

The radius for a circle is 10.00

一个圆圈的面积是314.16

The area for a circle is 314.16

程序以退出代码0结束

想要的输出:

请输入矩形的长度:10

Please enter the rectangle's length: 10

请输入矩形的宽度:10

Please enter the rectangle's width: 10

矩形的长度为10.00

The length for the rectangle is 10.00

矩形的宽度是10.00

The width for the rectangel is 10.00

矩形的面积为100.00

The area for a rectangle is 100.00

圆的半径为10.00

The radius for a circle is 10.00

一个圆的面积是314.16

The area for a circle is 314.16

程序以退出代码0结束

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

// Function prototypes
double getLength(); // Function that receives the length
double getWidth(); // Function that receives the width
double getArea(double); //Function calculates area for circle
double getArea(double,double); // Function calculates area for rectangle
void displayData(double,double,double,double, double);
// displayData function is used to display values

int main() {

    // Declares variables
    double length, width, circleArea, rectangleArea;

    length = getLength(); // Stores length
    width = getWidth(); // Stores width
    circleArea = getArea(width); // Stores area of circle
    rectangleArea = getArea(width, length); // Stores are of rectangle

    // Displays the length, width, area of circle, and rectangle area
    displayData(length, width, rectangleArea, width, circleArea);

    return 0;
}

/* This function asks the user to enter the rectangle's length and
 then returns that values as a double */

double getLength() {
    double length;
    cout << "Please enter the rectangle's length: ";
    cin >> length; // User input is stored in variable length
    cout << "\n";
    /* While loop doesn't let user input a
    negative value or a value of zero for 
    the length of the rectangle. */

    while (length <= 0) {

        if (length < 0) { //if statement used when a negative value is entered for length
            cout << "INVALID INPUT!" << endl; // Error message
            cout << "Please enter a positive value for the Length of the Rectangle: ";
            // Asks the user to enter the length again

            cin >> length; // Stores value of length

            if (length > 0) {
                cout << "\n";
            }
        }
        if (length == 0) { // If statement used when a value of zero is entered for length
            cout << "INVALID INPUT!" << endl;//error message
            cout << "Please enter a positive value for the Length of the Rectangle: ";
            // Asks the user to enter the length again

            cin >> length;// Stores the value of length

            if (length > 0) {
                cout << "\n";
            }
        }
    }

    return length;
}

/* This function asks the user to enter the rectangle's width and
 then returns that values as a double */

double getWidth() {
    double width;
    cout << "Please enter the rectangle's width: ";
    cin >> width; // User input is stored in variable width
    cout << "\n";

    /* While loop doesn't let user input a
     negative value or a value of zero for the
     width of the rectangle. */

    while (width <= 0) {
        if (width < 0) { // If statement used when a negative value is entered for width

            cout << "INVALID INPUT!" << endl; // Error message
            cout << "Please enter a positive value for the Width of the Rectangle: ";
            // Asks the user to enter the width again
            cin >> width; //Stores the value of width in the variable, width
            if (width > 0) {
                cout << "\n";
            }
        }
        if (width == 0) { // If statement used when a value of zero is entered for width
            cout << "INVALID INPUT!" << endl; // Error message
            cout << "Please enter a positive value for the Width of the Rectangle: ";
            // Asks the user to enter the width again
            cin >> width; // Stores the value of width in the variable, width
            if (width > 0)
            {
                cout << "\n";
            }
        }
    }


    return width;
}

double getArea(double radius) {
    const double PI = 3.14159;
    double circleArea;

    circleArea = PI * pow(radius, 2); // Formula for the area of a circle
    return circleArea;

}

/* This function accepts the rectangle's length and width as arguments and returns
 the rectangle's area. The area is calculated by multiplying the length by the
 width. */

double getArea(double length, double width) {
    double rectangleArea;

    rectangleArea = length * width; // Formula for the area of a rectangle
    return rectangleArea;
}

/* This function accepts the rectangle's length, width, and area as arguments
 and displays them in an appropriate message on the screen */

void displayData(double l, double w, double ra, double r, double ca) {

    cout << setprecision(2) << fixed;
    cout << "The length for the rectangle is " << l << endl;
    cout << "The width for the rectangle is " << w << endl;
    cout << "The area for a rectangle is " << ra << endl << endl;
    cout << "The radius for the circle is " << r << endl;
    cout << "The area for the circle is " << ca << endl;

}

推荐答案

<<cout一起使用时,它不会立即推送到屏幕.如您所示,发送输出的常见方法是endl.如果您不希望返回行,请使用flush.

When you use << with cout, it doesn't immediately push to the screen. A common way to send the output, as you've shown, is endl. If you don't want a line return, use flush.

cout << "Please enter the rectangle's length: " << flush;

Xcode 8.3似乎在同一行上有关于cincout的新规则.

Xcode 8.3 seems to have new rules about cin and cout on the same line.

这篇关于Xcode8-程序执行不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆