2017-04-01 57 views
-1

雖然使用Xcode8說它正在運行,但沒有輸出。Xcode8 - 程序執行不正確

輸出:

請輸入該矩形的長度:

請輸入矩形的寬度:

爲矩形的長度是10.00

寬度爲rectangel是10.00

爲矩形的面積是100.00

爲一個圓的半徑爲10.00

爲一個圓的區域是314.16

方案與出口端代碼:0

求購輸出:

請輸入該矩形的長度:10

請輸入矩形的寬度:10

爲矩形的長度是10.00

用於rectangel寬度爲10.00

爲矩形的區域是100.00

爲一個圓的半徑爲10.00

爲一個圓的區域是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; 

} 
+0

有沒有這樣的事情'Xcode程序'。 Xcode只是一個集成的開發環境。 – dandan78

+0

對不起,我只是想指出我正在使用xcode。 –

+0

因爲我相信這是Xcode的一個問題。 –

回答

1

當您使用 <<cout,它不會立即推到屏幕上。如您所示,發送輸出的常用方式是 endl。如果您不想換行,請使用 flush

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

編輯:Xcode的8.3似乎有大約在同一行cincout新規則。

+0

除非你做一些奇怪的事情,'cin'與'cout'綁定,並且會自動啓動一次flush每個輸入。我在這裏沒有Xcode8,但在Visual Studio 2017中,代碼按預期工作。 –

+0

沖洗似乎沒有做任何事情,如果我使用endl輸入是不可見的(屏幕上未顯示)。 –

+0

@BoPersson說得很好。 Xcode似乎對何時顯示有自己的看法。你可以從終端運行。 –