如何修复while语句以显示正确的cout?

时间:2016-02-20 21:10:12

标签: c++

如何才能将<div class="container"> <div class="main-display"> Main box: this box should be at the center of the container. </div> <ul class="extra-info"> <li>These items should naturally follow the main box and not care about vertical centering.</li> <li>Item 2</li> <li>Item 3</li> </ul> </div>声明正文中的cout打印出来?原因是当我尝试在while的行中写下这句话时,它会弄乱我的cout << password;

例如,如果我写password,它会在末尾显示带有数字的句子。

如果有人能给我一些帮助,我将不胜感激。

以下是我的计划:

cout << "this is your new password " << password;

2 个答案:

答案 0 :(得分:0)

您可以使用&#34; endl&#34;要在新行中显示您的密码,那么它不会搞砸:

.container {
  display: flex; /* Magic begins */
  flex-direction: column;
}
.before, .after {
  flex: 1; /* If possible, center .main vertically */
  min-height: 0; /* Really, don't care about overflow, just center .main vertically */
}
.main {
  align-self: center; /* Center .main horizontally */
}

答案 1 :(得分:0)

您无法在cout循环中获取while以后打印。如果它在while循环中,它将在每个循环中执行(直到被if左右包围)。

所以你有两个选择:

  1. 在您输入while循环之前打印句子:

    cout << "Your new password...";
    while(password!='\n'){
        ...
        cout << password;
    }
    
  2. 将您的“密码”(不是单词而非单个字符)存储在这样的字符串中:

    std::string str = "";
    while(password!='\n'){
        ...
        str += password;
    }
    cout << "Your new password..." << str;