为什么我尝试定义函数体时会出现此编译器错误?

时间:2015-06-07 10:31:54

标签: c++ user-defined-functions

//Prints a box of X's with user unput of width and height. Uses a User-defined function

#include <iostream>
#include <cmath>

using namespace std;
void box(int height, int width, int h = 1, int w = 1);



int main() {
    int width, height;

    cout << "Please enter width (0-25): \n";
    cin >> width;

    while (!(cin >> width) || width < 0 || width > 25) {
        cout << "Invalid entry. Please re-enter width: \n";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    cout << "Please enter height (0-25): \n";
    cin >> height;

    while (!(cin >> height) || height < 0 || height > 25) {
        cout << "Invalid entry. Please re-enter height: \n";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    cin.ignore(numeric_limits<streamsize>::max(), '\n');

}

void box(int height, int width, int h, int w)
{

    for (int h = 1; h <= height; h++)
    {
        for (int w = 1; w <= width; w++)
        cout << "X";
        cout << endl;
    }
}

我已经进行了编辑,程序编译没有错误,但它不打印框。我知道我需要在main中定义函数,但是当我写:

框(宽度,高度);

它显示错误。此外,当我在命令提示符窗口中时,输出如下:

请输入宽度(0-25): 12

12 请输入身高(0-25): 12&lt; ---为什么在第一次拍摄时不接受这个号码?

ž 输入无效。请重新输入身高: 12 按任意键继续......

3 个答案:

答案 0 :(得分:1)

问题在于:

void box(int height, int width, int h, int w) // there shouldn't be a semicolon
{
    int height, int width, int h, int w;

    for (h = 1; h <= height; h++);
    {
        for (w = 1; w <= width; w++);
        cout << "X";
        cout << endl;
    }
}

答案 1 :(得分:1)

这一行

void box(int height, int width, int h, int w);

&#34;声明&#34;一个名为box的函数,它接受四个参数,然后该行末尾的分号结束当前语句。

因此,当您尝试定义框时,您需要省略尾随分号

void box(int height, int width, int h, int w)
{
   ...

答案 2 :(得分:0)

抓住:!)

#include <iostream>
#include <iomanip>

void box( size_t height, size_t width, char c = 'X' )
{
    while ( height-- )
    {
        std::cout << std::setfill( c ) << std::setw( width ) << c << std::endl;
    }
}   

int main() 
{
    while ( true )
    {
        const size_t N = 25;

        std::cout << "Please enter height and width "
                     "less than or equal to " << N << " (0-exit): ";

        size_t width = 0, height = 0;
        std::cin >> height >> width;

        if ( height == 0 || width == 0 ) break;

        if ( N < height ) height = N;
        if ( N < width ) width = N;

        std::cout << std::endl;

        box( height, width );

        std::cout << std::endl;
    }
} 

如果要输入例如

10 16

然后程序输出

Please enter height and width less than or equal to 25 (0-exit): 10 16

XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXX

Please enter height and width less than or equal to 25 (0-exit): 

至于你的代码,那么函数定义中至少有一个拼写错误

void box(int height, int width, int h, int w);
                                             ^^
{

并且没有意义重新声明函数体中的参数

void box(int height, int width, int h, int w);
{
    int height, int width, int h, int w;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

同样在循环中,您必须删除类型说明符和分号

for (int h = 1; h <= int height; h++);
     ^^^^            ^^^^            ^^

功能太复杂了。:)

编辑:至于您更新的代码,请删除以下语句

cout << "Please enter width (0-25): \n";
cin >> width; // <== remove

cout << "Please enter height (0-25): \n";
cin >> height; // <== remove

并在函数中重写循环,如

for ( ; h <= height; h++)
{
    for ( int w1 = w ; w1 <= width; w1++)
    cout << "X";
    cout << endl;
}

你也忘了在main中调用函数本身。:)