鱼C论坛

 找回密码
 立即注册
查看: 1059|回复: 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 中。然而,如果没有明确的终止条件来结束输入过程,程序将一直等待输入,因此无法输出结果。

为了解决这个问题,你可以在输入完成后添加一个终止条件,例如输入一个特定的字符串来表示输入结束,然后在循环中进行判断。以下是修改后的代码示例:
#include <iostream>#include <string>#include <set>#include <sstream>using namespace std;int main(){    string s, temp;    set<string> str;    while (cin >> s)    {        if (s == "end")  // 输入 "end" 结束输入            break;                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;}
这样,当你在输入过程中输入 "end" 后,程序将结束输入并输出结果。

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

回复来自于CHAT-GPT。回复仅供参考,如有人工回答,最佳答案请优先选择人工
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

为了解决这个问题,你可以在输入完成后添加一个终止条件,例如输入一个特定的字符串来表示输入结束,然后在循环中进行判断。以下是修改后的代码示例:
#include <iostream>#include <string>#include <set>#include <sstream>using namespace std;int main(){    string s, temp;    set<string> str;    while (cin >> s)    {        if (s == "end")  // 输入 "end" 结束输入            break;                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;}
这样,当你在输入过程中输入 "end" 后,程序将结束输入并输出结果。

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

回复来自于CHAT-GPT。回复仅供参考,如有人工回答,最佳答案请优先选择人工
想知道小甲鱼最近在做啥?请访问 -> 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)`。

修改后的代码如下:
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <set>
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 << " ";
    }
    return 0;
}
这个代码会将输入的字符串按照单词进行分割,忽略非字母字符,并将所有的单词转换为小写字母后存储在一个`set`中,最后输出这个`set`中的单词。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-6 23:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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