英文:
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 <stdio.h>
#include <conio.h>
int main()
{
int Number_of_words, lower_limit = 1;
char word;
printf ("Number of Words: ");
scanf ("%d",&Number_of_words);
for (lower_limit = 1; lower_limit <= Number_of_words; lower_limit += 1)
{
printf("Word: ");
scanf("%str",&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.
答案1
得分: 1
scanf("%str",&word);
会导致未定义行为,因为word
指向一个字符,而不是字符数组。因此,任何都可能发生。
附注:"%str"
应该是 "%s"
。
这是如何在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("%str",&word);` causes [undefined behavior][1] since `word` is pointing to character, not a character array. Thus *anything* may happen.
Side note: `"%str"` should have been `"%s"`.
Here's how you can do it in C++:
```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';
}
}
答案2
得分: 1
你在你的代码中使用了错误的格式说明符来使用 scanf
,C++ 使用 <iostream>
中的 std::cout
和 std::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 <iostream>
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 <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;
}
}
If you want to actually store all the words for later use, you should use a std::vector<std::string>
to store them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论