在同一个缓冲区上存储多个输入?

时间:2018-12-23 04:16:18

标签: c++11

说您需要存储一个人的名字和姓氏。我知道我可以这样做:

       <!DOCTYPE html>
          <html>
            <head>
            <link rel="stylesheet" type="text/css" href="style.css">

            </head>
             <body>
               <div id="container">
                 <div id="Btns">
                   <button id="recreate" 
                     onclick="resizeGrid(this.value)">remake 
                           grid</button>
                 </div>
                        <br />
              </div>

             <script src="script.js"></script>
          </body>
        </html>




       window.onload = function(){
          var createGrid = prompt("How many rows do you want?");
            for(i = 0; i <= createGrid; i++){
              var row = document.createElement('div');
                row.className = "row";
                row.id = "row" + i;

             for(k= 0; k <= createGrid; k++ ){
               var box = document.createElement('div');
                 box.className = "box";
                 row.appendChild(box);
            }
            container.appendChild(row);
        }
            return container;
    }


    #container {

      width: 50%;
      height: 50%;
      margin-top:50px;
      margin-left: auto;
      margin-right: auto;
      background: "white";
      overflow:hidden;
      box-sizing: border-box;
   }


   .row{
      border:1px solid red;
      height:1em;
      width:25.25%;
      overflow:hidden;
      box-sizing: border-box;

   }




   .box{
      display: inline-block;
      width: 8.25%;
      height: 1em;
      border: 1px solid red;
      border-bottom: 0px;
      border-left: 0px;
      float:right;
      overflow:hidden;
      box-sizing: border-box;
      margin: auto;
   }

我想消除两个额外的变量和最后的串联。

我希望能够在将缓冲区存储到变量之前继续在缓冲区上输入。

有没有办法做到这一点?

3 个答案:

答案 0 :(得分:2)

您可以实现input function的C ++版本并使用它。例如:

#include <iostream>
#include <exception>
#include <string>

std::string input() {
    std::string line;
    if(getline(std::cin, line))
        return line;
    throw std::runtime_error(std::cin.eof() ? "end of input" : "input error");
}

std::string input(char const* prompt) {
    (std::cout << prompt).flush();
    return input();
}

int main() {
    auto firstname = input("Enter first name? ");
    auto lastname = input("Enter last name? ");
    auto fullname = firstname + ' ' + lastname;
    std::cout << "Hello " << fullname << '\n';
}

答案 1 :(得分:0)

嘿,我认为您可以轻松摆脱其中一个变量。

#include <iostream>
#include <string>

int main()
{
std::string lastname;
std::string fullname;

std::cout << "Users firstname ? "; std::cin >> fullname;
std::cout << "Users last name ? "; std::cin >> lastname;

fullname += ' ' + lastname;

std::cout << "Full name: " << fullname << std::endl;

return 0;

}

迭代器所希望的第二种方法是更多您想要的东西。

#include <iostream>
#include <string>
#include <iterator>

int main()
{
    std::cout << "Users firstname ? ";
    std::istream_iterator<std::string> it(std::cin);

    std::string fullName;

    std::cout << "Users lastname ? ";
    fullName += *it;
    ++it;
    fullName += ' ' + *it;

    std::cout << "Full Name: " << fullName << "\n";

    return 0;
}

我个人认为第一种方法更容易阅读和遵循。

答案 2 :(得分:0)

有一个有趣的post on reddit,其中包含您展示的示例。

当前,C ++尚无实现此目标的标准方法,但是该文章的作者提供了header-only library(这意味着您可以将代码复制粘贴到文件中或项目中的文件中,也可以只包含依赖项),例如:

int age = read<int>("Please enter your age: ");
cout << "You are " << age << " years old.\n";

如果您对应用外部依赖项不感兴趣,则必须坚持使用标准解决方案,即问题中显示的内容以及 Eddie C。的{{ 3}}。

相关问题