Xcode8 - 程序未正确执行
Xcode8 - Program is not executing correctly
在使用Xcode8时说它正在运行但没有输出。
输出:
10
请输入矩形的长度:
10
请输入矩形的宽度:
矩形的长度为 10.00
矩形的宽度为 10.00
矩形的面积为 100.00
圆的半径为 10.00
圆的面积是 314.16
程序以退出代码结束:0
想要的输出:
请输入矩形的长度:10
请输入矩形的宽度:10
矩形的长度为 10.00
矩形的宽度为 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;
}
当您将
<<
与cout
一起使用时,它不会立即推送到屏幕。如您所显示的,发送输出的常用方法是 endl
.如果不希望换行,请使用 flush
。
cout << "Please enter the rectangle's length: " << flush;
编辑:Xcode 8.3似乎在同一行上有关于cin
和cout
的新规则。
相关文章:
- 程序在执行程序的其余部分之前退出
- libprotobuf 检查在 Mac OS 上执行程序时失败
- 是否可以依赖函数范围的静态变量来执行程序关闭期间调用的方法?
- 在另一台 PC 上执行程序时加载 SDL2 共享库时出错
- 从 GUI 文件资源管理器执行程序,并且不生成终端
- 测试操作系统时执行程序集"sti"时虚拟框崩溃
- 重新执行C++程序会加快速度
- 如何用共享库替换可执行C++程序的功能?
- 编译并执行C#程序中的C++代码
- 如果我在 Linux 上更改C++动态共享库,而我的可执行程序在其上使用,会发生什么
- 在C++中,系统如何将这些对象中的每一个与执行程序的窗口相关联?
- 命令行参数执行C 程序
- 按 e 执行程序C++
- 在返回另一个函数的函数中执行程序期间出错
- 如何使用"trusty"映像在本地执行C++程序的Travis CI?
- 当我们说在执行程序时将操作系统的控制权传递给main()函数时,我们的意思是什么
- execve(..) 不执行程序,尽管传入了 PATH 变量
- 在机器异常之后重新执行程序
- 调试和发布库及其可执行程序
- 当访问者进入网站时执行C++程序