您好,堆栈溢出用户!我对程序有疑问,也遇到了问题。我实际上已经完成了该程序,但是对于我的尺寸(火箭的宽度和长度),我似乎没有单独的void函数。我目前在我的void函数中创建了我的火箭,void绘制框,但是我似乎无法弄清楚如何为我的尺寸使用单独的void函数。有任何提示或我做错的事情可以解决此问题吗?感谢您的任何帮助,再次感谢!
#include <iostream>
using namespace std;
void dimensions (int, int);
void drawBox (int , int , int);
int main ()
{
int numXs, numSpaces, numRows, width, height;
//dimensions (width, height);
drawBox (numXs, numSpaces, numRows);
return 0;
}
void dimensions (int width, int height)
{
cout << "Enter width: ";
cin >> width;
cout << "Enter height: ";
cin >> height;
cout << endl;
}
void drawBox (int numXs, int numSpaces, int numRows)
{
int count, rowCount, spaceCount, width, height;
cout << "Enter width: ";
cin >> width;
cout << "Enter height: ";
cin >> height;
cout << endl;
for (count = 0; count < width; count++)
{
cout << "X";
}
cout << endl;
for (rowCount = 0; rowCount < height; rowCount++)
{
cout << "X";
if ( width % 2 == 0)
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << " ";
}
cout << "X" << endl;
}
else
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << "X";
}
cout << "X" << endl;
}
}
cout << "X";
for (spaceCount = 0; spaceCount < numSpaces; spaceCount++)
{
cout << " ";
}
for (count = 0; count < width - 1; count++)
{
cout << "X";
}
cout << endl;
//second box is being created below
for (count = 0; count < width; count++)
{
cout << "X";
}
cout << endl;
for (rowCount = 0; rowCount < height; rowCount++)
{
cout << "X";
if ( width % 2 == 0)
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << " ";
}
cout << "X" << endl;
}
else
{
for (spaceCount = 0; spaceCount < width - 2; spaceCount++)
{
cout << "X";
}
cout << "X" << endl;
}
}
cout << "X";
for (spaceCount = 0; spaceCount < numSpaces; spaceCount++)
{
cout << " ";
}
for (count = 0; count < width - 1; count++)
{
cout << "X";
}
cout << endl;
}
答案 0 :(得分:2)
通过引用传递变量:
void dimensions(int& height, int& width)
{
height = 5;
width = -6; // Because int is signed, this is possible.
}
int main()
{
int tall = -42;
int length = 22;
dimensions(tall, length);
std::cout << "tall: " << tall << "\n";
std::cout << "length: " << length << "\n";
return 0;
}
答案 1 :(得分:0)
您是否尝试过创建高度和宽度全局变量,以便可以在两个函数中使用它们?