如何输入与给定数量一样多的单词?

huangapple go评论70阅读模式
英文:

How to input a number of words as many as the given number?

问题

Here's the translated code portion:

#include <stdio.h>
#include <conio.h>

int main()
{
    int Number_of_words, lower_limit = 1;
    char word;

    printf("单词数量:");
    scanf("%d", &Number_of_words);

    for (lower_limit = 1; lower_limit <= Number_of_words; lower_limit += 1)
    {
        printf("单词:");
        scanf("%s", &word);
    }
}

I've translated the comments and user prompts into Chinese while leaving the code logic unchanged.

英文:
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;

int main()
{
	int Number_of_words, lower_limit = 1;
	char word;
	
	printf (&quot;Number of Words: &quot;);
	scanf (&quot;%d&quot;,&amp;Number_of_words);
	
	for (lower_limit = 1; lower_limit &lt;= Number_of_words; lower_limit += 1)
	{
		printf(&quot;Word: &quot;);
		scanf(&quot;%str&quot;,&amp;word);
		
	}
}

so, I want to make a code that will allow the user to input a number of words as many as the number that has been input by the user before. For example,when I run the code, if the input is "4" on "Number of Words", that will allow me to write 5 words. But instead of allowing me to write 4 words, it asks me to input more than 5 words.

(image)It keeps asking more than 4 words

答案1

得分: 1

scanf("%str",&word); 会导致未定义行为,因为word指向一个字符,而不是字符数组。因此,任何都可能发生。

附注:&quot;%str&quot; 应该是 &quot;%s&quot;

这是如何在C++中实现的:

#include <cstddef>
#include <iostream>
#include <string>

int main() {
  std::size_t sz;
  std::cout << "Number: ";
  std::cin >> sz;

  for (std::string s; sz-- && std::cin >> s; /*nothing*/) {
    std::cout << s << '\n';
  }
}

<details>
<summary>英文:</summary>

`scanf(&quot;%str&quot;,&amp;word);` causes [undefined behavior][1] since `word` is pointing to character, not a character array. Thus *anything* may happen. 

Side note: `&quot;%str&quot;` should have been `&quot;%s&quot;`.

Here&#39;s how you can do it in C++:

```C++
#include &lt;cstddef&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

int main() {
  std::size_t sz;
  std::cout &lt;&lt; &quot;Number: &quot;;
  std::cin &gt;&gt; sz;

  for (std::string s; sz-- &amp;&amp; std::cin &gt;&gt; s; /*nothing*/) {
    std::cout &lt;&lt; s &lt;&lt; &#39;\n&#39;;
  }
}

答案2

得分: 1

你在你的代码中使用了错误的格式说明符来使用 scanf,C++ 使用 &lt;iostream&gt; 中的 std::coutstd::cin 来进行格式化输入/输出。此外,char 只能保存单个字符,而不是字符串。对于字符串,应使用 std::string。此外,using namespace std; 被认为是不良实践,最好仅在需要时声明变量,并且通常约定将大写字母用于类型或常量的名称。

修正后的代码可能如下所示:

#include <string>
#include <iostream>

int main() {
    size_t number_of_words;   
    std::cout << "Number of Words: ";
    std::cin >> number_of_words;

    for (size_t i = 0; i < number_of_words; ++i) {
        std::string word;
        std::cout << "word: ";
        std::cin >> word;
    }
}

如果您想要实际存储所有单词以供以后使用,应使用 std::vector<std::string> 来存储它们。

英文:

You are using the wrong format specifier for scanf in your code, c++ has std::cout and std::cin from &lt;iostream&gt; for formatted in/output. Next, a char holds a single character not a string. For strings there is std::string. Moreover, using namespace std; is considered bad practice, it is better to declare variables only when you need them, and it is common convention to reserve capital starting letters for types or constants.

The corrected code could look like this:

#include &lt;string&gt;
#include &lt;iostream&gt;

int main() {
    size_t number_of_words;   
    std::cout &lt;&lt; &quot;Number of Words: &quot;;
    std::cin &gt;&gt; number_of_words;

    for (size_t i=0;i&lt;number_of_words;++i) {
        std::string word;
        std::cout &lt;&lt; &quot;word: &quot;;
        std::cin &gt;&gt; word;
    }
}

If you want to actually store all the words for later use, you should use a std::vector&lt;std::string&gt; to store them.

huangapple
  • 本文由 发表于 2020年1月6日 17:32:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定