Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的任务是在框架内打印字符串,例如正方形 #包括 使用名称空间std; int main() { int i=1,len; char ch[26][26],ch2; cout_C++_String_Loops_Nested_Nested Loops - Fatal编程技术网

C++ 我的任务是在框架内打印字符串,例如正方形 #包括 使用名称空间std; int main() { int i=1,len; char ch[26][26],ch2; cout

C++ 我的任务是在框架内打印字符串,例如正方形 #包括 使用名称空间std; int main() { int i=1,len; char ch[26][26],ch2; cout,c++,string,loops,nested,nested-loops,C++,String,Loops,Nested,Nested Loops,由于您的代码中没有提供太多细节,我从一开始就使用了一个新代码,这就是我想到的: #include<iostream> using namespace std; int main() { int i=1,len; char ch[26][26],ch2; cout<<"enter string: "<<endl; for(i=0;;i++) { cin>>ch[i]; le

由于您的代码中没有提供太多细节,我从一开始就使用了一个新代码,这就是我想到的:

#include<iostream>
using namespace std;
int main()
{
    int i=1,len;
    char ch[26][26],ch2;
    cout<<"enter string: "<<endl;   
    for(i=0;;i++)
    {
        cin>>ch[i];
        len++;
        if(getchar()=='\n')
        break;
    }
    int n,j;
    cout<<"enter size: "<<endl;
    cin>>n;
    int k;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=n;j++)
        {
            if(i==0||i==n||j==0||j==n)
            {
                cout<<"*";
            }
            else
                cout<<" ";
            if(i==((n/2)-2)&&j==((n/2)-2))
            {
                for(k=0;k<len;k++)
                {
                    cout<<ch[k]<<endl;
                    cout<<"*";
                }
            }
        }   
        cout<<"\n";
    }
} 
最好避免使用
名称空间std;
。只需导入您真正需要的内容

using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::endl;
不要在输入提示后断行!(作为Linux用户,我个人讨厌它)

这里不需要变量
i
,只需运行一个无限循环(如果可以避免无限循环,请再次尝试重新安排,
,而(getchar()!='\n')
更容易解释

    for(;;) {
正如pstrjds在评论中建议的那样,如果可以,请使用
getline()

        string s;
        cin >> s;
        strings.push_back(s);
就像我说的,试着用
while
条件重新格式化

        if(getchar() == '\n')
            break;
广场的上边

    // first horizontal row of stars
    for(j = 0; j < n; ++j)
        cout << '*';
    cout << endl;
    // last horizontal line of '*' (we close the square)
    for(j = 0; j < n; ++j)
        cout << '*';
    cout << endl;
我们在打印完字符串后,用空行完成正方形

    for(i = empty_lines_around_text + strings.size() + 1; i < n; ++i) {
        cout << '*';
        for(j = 1; j < n - 1; ++j) {
            cout << ' ';
        }
        cout << '*' << endl;
    }

现在,这个代码并不完美,有很多重构和优化的需要,但是它最大化了C++特性的使用。 是一个包含整个代码的粘贴库

当使用字符串
Hello friends
和size
12
运行时,输出:

return 0;
}

主要问题在于:

************
*          *
*          *
*          *
*          *
*   hello  *
*  friends *
*          *
*          *
*          *
*          *
************

用于(k=0;如果您查看正在打印字符串的代码,请注意您开始在正方形中间打印字符串,但是您没有考虑应该打印的字符串的长度。您需要计算中间与字符串相关的位置。你能编辑你的问题来解释问题的确切位置吗?以及预期的结果是什么?添加示例!是的,你能添加一些吗?这将大大有助于解决你的问题请不要喜欢堆栈溢出谢谢大家你们都是救世主
    for(i = 1; i < empty_lines_around_text; ++i) {
        cout << '*';
        for(j = 1; j < n - 1; ++j) {
            cout << ' ';
        }
        cout << '*' << endl;
    }
    //here we do the actual printing of the strings
    for(i = 0; i < strings.size(); ++i) {
        string s = strings[i];

        // once again, assuming the size of each string is < n
        unsigned int empty_chars_around_string((n - s.size()) / 2);
        cout << '*';
        for(j = 0; j < empty_chars_around_string; ++j)
            cout << ' ';
        cout << s;
        for(j = empty_chars_around_string + s.size() + 1; j < n - 1; ++j)
            cout << ' ';
        cout << '*' << endl;
    }
    for(i = empty_lines_around_text + strings.size() + 1; i < n; ++i) {
        cout << '*';
        for(j = 1; j < n - 1; ++j) {
            cout << ' ';
        }
        cout << '*' << endl;
    }
    // last horizontal line of '*' (we close the square)
    for(j = 0; j < n; ++j)
        cout << '*';
    cout << endl;
return 0;
}
************
*          *
*          *
*          *
*          *
*   hello  *
*  friends *
*          *
*          *
*          *
*          *
************
for(k=0;k<len;k++)
{
    cout<<ch[k]<<endl;
    cout<<"*";
}
for( k = 0; k<len; k++ )
{
    cout << ch[k];
    j += strlen( ch[k] );
}