鱼C论坛

 找回密码
 立即注册
查看: 1578|回复: 2

[已解决]这个代码为什么输出不了东西

[复制链接]
发表于 2023-11-24 17:34:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <iostream>
#include <string>
#include <set>
#include <sstream>
using namespace std;
int main()
{
        string s,temp;
        set<string> str;
        while (cin >> s)
        {
                for (int i = 0; i < s.length(); i++)
                {
                        if (isalpha(s[i]))
                        {
                                s[i] = tolower(s[i]);
                        }
                        else
                        {
                                s[i] = ' ';
                        }
                }
                stringstream iss(s);
                while (iss >> temp)
                {
                        str.insert(temp);
                }
        }
        for (set<string>::iterator it = str.begin(); it != str.end(); it++)
        {
                cout << *it << endl;
        }
        return 0;
}
最佳答案
2023-11-24 17:35:19
这段代码的问题在于它会陷入无限循环。在 while (cin >> s) 循环中,你正在尝试从标准输入流中读取字符串,并将其存储到变量 s 中。然而,如果没有明确的终止条件来结束输入过程,程序将一直等待输入,因此无法输出结果。

为了解决这个问题,你可以在输入完成后添加一个终止条件,例如输入一个特定的字符串来表示输入结束,然后在循环中进行判断。以下是修改后的代码示例:
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. #include <sstream>
  5. using namespace std;
  6. int main()
  7. {
  8.     string s, temp;
  9.     set<string> str;
  10.     while (cin >> s)
  11.     {
  12.         if (s == "end")  // 输入 "end" 结束输入
  13.             break;
  14.         
  15.         for (int i = 0; i < s.length(); i++)
  16.         {
  17.             if (isalpha(s[i]))
  18.             {
  19.                 s[i] = tolower(s[i]);
  20.             }
  21.             else
  22.             {
  23.                 s[i] = ' ';
  24.             }
  25.         }
  26.         stringstream iss(s);
  27.         while (iss >> temp)
  28.         {
  29.             str.insert(temp);
  30.         }
  31.     }
  32.     for (set<string>::iterator it = str.begin(); it != str.end(); it++)
  33.     {
  34.         cout << *it << endl;
  35.     }
  36.     return 0;
  37. }
复制代码

这样,当你在输入过程中输入 "end" 后,程序将结束输入并输出结果。

希望这次能够帮助到你!如果还有其他问题,请随时提问。

回复来自于CHAT-GPT。回复仅供参考,如有人工回答,最佳答案请优先选择人工
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-24 17:35:19 | 显示全部楼层    本楼为最佳答案   
这段代码的问题在于它会陷入无限循环。在 while (cin >> s) 循环中,你正在尝试从标准输入流中读取字符串,并将其存储到变量 s 中。然而,如果没有明确的终止条件来结束输入过程,程序将一直等待输入,因此无法输出结果。

为了解决这个问题,你可以在输入完成后添加一个终止条件,例如输入一个特定的字符串来表示输入结束,然后在循环中进行判断。以下是修改后的代码示例:
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. #include <sstream>
  5. using namespace std;
  6. int main()
  7. {
  8.     string s, temp;
  9.     set<string> str;
  10.     while (cin >> s)
  11.     {
  12.         if (s == "end")  // 输入 "end" 结束输入
  13.             break;
  14.         
  15.         for (int i = 0; i < s.length(); i++)
  16.         {
  17.             if (isalpha(s[i]))
  18.             {
  19.                 s[i] = tolower(s[i]);
  20.             }
  21.             else
  22.             {
  23.                 s[i] = ' ';
  24.             }
  25.         }
  26.         stringstream iss(s);
  27.         while (iss >> temp)
  28.         {
  29.             str.insert(temp);
  30.         }
  31.     }
  32.     for (set<string>::iterator it = str.begin(); it != str.end(); it++)
  33.     {
  34.         cout << *it << endl;
  35.     }
  36.     return 0;
  37. }
复制代码

这样,当你在输入过程中输入 "end" 后,程序将结束输入并输出结果。

希望这次能够帮助到你!如果还有其他问题,请随时提问。

回复来自于CHAT-GPT。回复仅供参考,如有人工回答,最佳答案请优先选择人工
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-24 17:35:47 | 显示全部楼层
这个代码无法输出任何东西的问题在于两点:

1. 缺少命名空间
代码中使用了`set`和`set::iterator`,但没有指明使用的是哪个命名空间。你需要添加`using namespace std;`来使用`std::set`和`std::set::iterator`。

2. 函数调用错误
在for循环中,你使用了`isalpha(s)`来检查字符串`s`中是否只包含字母。但实际上,`isalpha()`函数需要传入一个字符而不是字符串。你应该改为`s[i ]`来检查每个字符。另外,正确的转换为小写字母的函数是`tolower()`而不是`tolower(s)`。

修改后的代码如下:
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cctype>
  5. #include <set>
  6. using namespace std;
  7. int main()
  8. {
  9.     string s, temp;
  10.     set<string> str;
  11.     while (cin >> s)
  12.     {
  13.         for (int i = 0; i < s.length(); i++)
  14.         {
  15.             if (isalpha(s[i]))
  16.             {
  17.                 s[i] = tolower(s[i]);
  18.             }
  19.             else
  20.             {
  21.                 s[i] = ' ';
  22.             }
  23.         }
  24.         stringstream iss(s);
  25.         while (iss >> temp)
  26.         {
  27.             str.insert(temp);
  28.         }
  29.     }
  30.     for (set<string>::iterator it = str.begin(); it != str.end(); it++)
  31.     {
  32.         cout << *it << " ";
  33.     }
  34.     return 0;
  35. }
复制代码

这个代码会将输入的字符串按照单词进行分割,忽略非字母字符,并将所有的单词转换为小写字母后存储在一个`set`中,最后输出这个`set`中的单词。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 15:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表