[转贴]如何在C ++中绘制矩形 [英] [Repost] how to draw a rectangle in C++

查看:70
本文介绍了[转贴]如何在C ++中绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次尝试了所有这些代码,但仍然得到相同的图像

我得到这样的矩形

hi again i have tried all these codes but still i get the same image

i get my rectangle like this

#               #
#               #
#               #
#               #



但我希望成为



but i would like it to be

################
#              #
#              #
################


我该怎么办?

这就是我尝试过的


how can i do it ?

this is what i have tried

#include <string>

#include <stdio>

using namespace std;
int main()
{
    static const int WIDTH=18;
    static const int HEIGHT=8;

    string border(WIDTH-2,'#');
    string spaces(WIDTH-2,' ');
    cout << '#' << border << '#' << std::endl;
    for(unsigned int i=1; i<height-1;>    {
        cout << '#' << spaces << '#' << endl;
    }
        cout << '#' << border << '#' << endl;

    system("pause");
    return 0;
}



在这里已经回答 how-to-draw-a-rectangle -in-Cplusplus [ ^ ]



Already answered here how-to-draw-a-rectangle-in-Cplusplus[^]

推荐答案

此代码将完全无法编译:
1)没有这样的stdio标头,它必须是stdio.h

2)查看您的for循环-未声明变量i1

这是可以完成工作的代码的不同版本:
This code will not compile at all:
1) There is no such stdio header, it must be stdio.h

2) See your for loop - the variable i1 is undeclared!

Here is a bit different version of your code which will do the job:
#include <iostream> // For cout and cin
#include <string> 

using namespace std;

int main()
{
    static const int WIDTH=18;
    static const int HEIGHT=8;
    string border(WIDTH-2,'#');
    string spaces(WIDTH-2,' ');

    cout << '#' << border << '#' << std::endl;
   
    // And here is the fix for the loop
    for(unsigned int i=1; i < HEIGHT; ++i)
    {
        cout << '#' << spaces << '#' << endl;
    }
    cout << '#' << border << '#' << endl;
    
    // Personally I prefer using cin.get() instead of system("pause")
    cin.get();

    return 0;
}



我希望这有帮助. :)

问候



I hope this helps. :)

Regards


这篇关于[转贴]如何在C ++中绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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