Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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++ C++;使用循环在框中打印短语_C++_Loops - Fatal编程技术网

C++ C++;使用循环在框中打印短语

C++ C++;使用循环在框中打印短语,c++,loops,C++,Loops,我无法将单词放在方框的中心。代码应该接受任何单词或短语,并将其显示在类似于代码示例顶部的框中。我已经设法在框中打印输入,但无法想出如何逐个堆叠单词并将其居中。任何帮助都将不胜感激。谢谢大家! /*Write a program that reads a sequence of words and then prints them in a box, with each word centered, like this: +----------+ | Hello | | C++

我无法将单词放在方框的中心。代码应该接受任何单词或短语,并将其显示在类似于代码示例顶部的框中。我已经设法在框中打印输入,但无法想出如何逐个堆叠单词并将其居中。任何帮助都将不胜感激。谢谢大家!

/*Write a program that reads a sequence of words and then prints them in a box, with each word centered, like this:

+----------+
|  Hello   |
|   C++    |
|programmer|
+----------+
*/

#include <iostream>
#include <string>
using namespace std;

int main() {

  string s;
  int max_word_len = 1;
  cout << "Please enter a word or sentence: " << endl;
  getline(cin, s);

  for (int i = 0; i < max_word_len; i++) 
  {
    int word_len = s.length();
    string box = "+";
    for (int i = 0; i < word_len; i++)
    {
        box += "-";
    }
    box += "+";
    cout << box << endl; 
    cout << "|" << s << "|" << endl;
    cout << box <<endl;
  }

  return 0;
}
/*编写一个程序,读取一系列单词,然后将它们打印在一个框中,每个单词居中,如下所示:
+----------+
|你好|
C++
|编程器|
+----------+
*/
#包括
#包括
使用名称空间std;
int main(){
字符串s;
int max_word_len=1;

这是我的实现,希望对你有所帮助

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

int main() {
    std::string str;
    std::vector<std::string> v;
    getline(std::cin, str);
    std::istringstream ss(str);
    size_t max_length = 0;
    do {
        std::string temp;
        ss >> temp;
        if (temp.length() != 0)
            v.push_back(temp);
        max_length = std::max(max_length, temp.length());
    } while(ss);
    std::cout << "+";
    for (size_t i = 0; i < max_length; i++)
        std::cout << "-";
    std::cout << "+" << std::endl;
    for (auto &i : v) {
        std::cout << "|";
        auto sp = max_length - i.length();
        for (size_t j = 0; j < sp/2; j++)
            std::cout << " ";
        std::cout << i;
        for (size_t j = 0; j < sp - sp/2; j++)
            std::cout << " ";
        std::cout << "|" << std::endl;
    }
    std::cout << "+";
    for (size_t i = 0; i < max_length; i++)
        std::cout << "-";
    std::cout << "+" << std::endl;
    return 0;
}
输出

hello c++ programmer
+----------+
|  hello   |
|   c++    |
|programmer|
+----------+

如果一个单词太长,不适合一行怎么办?那你想怎么办?@bruno我正在键入答案,然后我看到了你的评论,现在我想解决家庭作业问题。我的意思是,你说的很有意义。就代码而言,也不需要循环。(-)更清楚的是使用while而不是do-while,因为输入字符串可以是空的。要在单词之前生成空格,然后在结尾之前生成空格,可以使用std::setw删除循环。要生成两个-序列,还可以使用std::string(,“-”)删除循环。只需我的2美分