Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何允许用户输入大小写? 我是新手C++程序员。我已经编写了一个基本的情感检查器,它根据从数组中获取的回复做出反应。我想知道如何使回复在输入时同时使用大写和小写?例如,当用户键入“happy”或“happy”时,这两个选项都会起作用。我已经读过switch语句和toupper/tolower,但是我不知道如何为我的代码实现它们。这是: // Array of emotions string positive[] = {"happy", "pleased", "joyful", "excited", "content", "cheerful", "satisfied", "positive"}; string negative[] = {"unhappy", "sad", "depressed", "gloomy", "down", "glum", "despair", "negative"}; string reply; cout << "Please state your current emotions." << endl; cin >> reply; for (int i = 0; i < 10; i++) if (reply == positive[i]) { cout << "I am glad to hear that!" << endl; } else if (reply == negative[i]) { cout << "I am sorry to hear that." << endl; } //情绪数组 字符串正[]={“快乐”、“高兴”、“高兴”、“兴奋”、“满意”、“积极”}; 字符串负[]={“不快乐”、“悲伤”、“沮丧”、“忧郁”、“低落”、“忧郁”、“绝望”、“消极”}; 字符串回复; 答不上来; 对于(int i=0;i_C++ - Fatal编程技术网

如何允许用户输入大小写? 我是新手C++程序员。我已经编写了一个基本的情感检查器,它根据从数组中获取的回复做出反应。我想知道如何使回复在输入时同时使用大写和小写?例如,当用户键入“happy”或“happy”时,这两个选项都会起作用。我已经读过switch语句和toupper/tolower,但是我不知道如何为我的代码实现它们。这是: // Array of emotions string positive[] = {"happy", "pleased", "joyful", "excited", "content", "cheerful", "satisfied", "positive"}; string negative[] = {"unhappy", "sad", "depressed", "gloomy", "down", "glum", "despair", "negative"}; string reply; cout << "Please state your current emotions." << endl; cin >> reply; for (int i = 0; i < 10; i++) if (reply == positive[i]) { cout << "I am glad to hear that!" << endl; } else if (reply == negative[i]) { cout << "I am sorry to hear that." << endl; } //情绪数组 字符串正[]={“快乐”、“高兴”、“高兴”、“兴奋”、“满意”、“积极”}; 字符串负[]={“不快乐”、“悲伤”、“沮丧”、“忧郁”、“低落”、“忧郁”、“绝望”、“消极”}; 字符串回复; 答不上来; 对于(int i=0;i

如何允许用户输入大小写? 我是新手C++程序员。我已经编写了一个基本的情感检查器,它根据从数组中获取的回复做出反应。我想知道如何使回复在输入时同时使用大写和小写?例如,当用户键入“happy”或“happy”时,这两个选项都会起作用。我已经读过switch语句和toupper/tolower,但是我不知道如何为我的代码实现它们。这是: // Array of emotions string positive[] = {"happy", "pleased", "joyful", "excited", "content", "cheerful", "satisfied", "positive"}; string negative[] = {"unhappy", "sad", "depressed", "gloomy", "down", "glum", "despair", "negative"}; string reply; cout << "Please state your current emotions." << endl; cin >> reply; for (int i = 0; i < 10; i++) if (reply == positive[i]) { cout << "I am glad to hear that!" << endl; } else if (reply == negative[i]) { cout << "I am sorry to hear that." << endl; } //情绪数组 字符串正[]={“快乐”、“高兴”、“高兴”、“兴奋”、“满意”、“积极”}; 字符串负[]={“不快乐”、“悲伤”、“沮丧”、“忧郁”、“低落”、“忧郁”、“绝望”、“消极”}; 字符串回复; 答不上来; 对于(int i=0;i,c++,C++,读入字符串后,您可能希望添加一个步骤来处理该字符串reply 一种解决方案是迭代reply的长度,并对字符串中的每个字符调用tolower 从中修改的示例 然后,在比较字符串时,不必担心大小写。首先,编写一个名为equals的函数,通过将字符串的所有字符转换为小写来比较两个世界是否相同。例如: bool equals(string status, string userInput) { //lowercaseStatus = convert userInput to lowercase

读入字符串后,您可能希望添加一个步骤来处理该字符串
reply

一种解决方案是迭代
reply
的长度,并对字符串中的每个字符调用
tolower

从中修改的示例


然后,在比较字符串时,不必担心大小写。

首先,编写一个名为equals的函数,通过将字符串的所有字符转换为小写来比较两个世界是否相同。例如:

bool equals(string status, string userInput)
{
    //lowercaseStatus = convert userInput to lowercase
    //lowercaseUserInput = convert status to lowercase
    return lowercaseStutus == lowercaseUserInput;
}
然后使用for循环中的函数:

for (int i = 0; i < 10; i++)

if (equals(positive[i],reply))
{
        cout << "I am glad to hear that!" << endl;
}

else if (equals(negative[i],reply))
{
    cout << "I am sorry to hear that." << endl;
}
for(int i=0;i<10;i++)
如果(等于(肯定[i],答复))
{

无法在进入循环之前将所有输入转换为小写。请查看文档
for (int i = 0; i < 10; i++)

if (equals(positive[i],reply))
{
        cout << "I am glad to hear that!" << endl;
}

else if (equals(negative[i],reply))
{
    cout << "I am sorry to hear that." << endl;
}