首页 > 解决方案 > 如何将大写字母读成小写字母?

问题描述

聊天机器人将读取他们的输入并根据输入进行回复,但小写和大写的读取方式不同。我一直未能找到如何让这段代码将大写字母读为小写。我如何让它将输入识别为小写?

#include <iostream>
#include <cstring>
#include <locale>
#include <limits>
#include <cstdlib> 

using namespace std;

int main

(int argc, char **argv)
{

string input;

cout << "Hello there" << endl;
{

for(;;)
{

std::cin >> input;

if (input == "hi")
cout << "hey what's up?" << endl;
if (input == "hey")
cout << "hey, what's up?" << endl;
if (input == "hello")
cout << "Hey, what's up?" << endl;
if (input == "how are you?")
cout << "I am good, how are you?" << endl;
if (input == "fine")
cout << "that's good" << endl;
if (input == "hru")
cout << "i am good, how are you?" << endl;
if (input == "good")
cout << "thats good" << endl;

        }
    }
}

标签: c++chatbot

解决方案


通过在进行比较之前将所有内容转换为小写。要轻松做到这一点,请#include <cctype>使用 tolower() 函数。


推荐阅读