【发布时间】:2014-06-02 07:17:48
【问题描述】:
第一行包含一个整数n(1 ≤ n ≤ 100)。以下 n 行中的每一行都包含一个单词。所有单词均由小写拉丁字母组成,长度为 1 到 100 个字符。 (来源:http://codeforces.com/problemset/problem/71/A)
如果给定 n,你将如何从用户那里获得输入?我尝试使用 while 循环,但它不起作用:
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int i;
while (i<=n) {
cin>>i ;
i++;
}
}
【问题讨论】:
-
cin>>i ; i++;Aawww! 这看起来很奇怪:-/ ... -
您想如何存储该输入?
-
如果您只想阅读它,这将起作用
#include <iostream> using namespace std; int main() { int n; cin>>n; int i=0; string xd; while (i<=n) { cin>>xd ; i++; } }它会一次读取一个单词,并将它们存储在字符串“xd”中。您可以选择将其附加到其他内容,将其放入 [i] 元素的字符串数组中,或者您想要的任何其他内容。 -
@random21 cmets 中的代码是如此 baaaad!你为什么不写一个答案?
-
因为这么简单的程序不值得被认为是答案。
标签: c++ input while-loop