为项目制作人工智能程序。无法让它识别用户输入
Making an AI program for a project. Can't get it to recognise the user input
所以我必须创建一个 AI 程序来与用户交互并根据用户输入做出响应。我不是很有经验,这已经花了几个小时 lmao,我在网上看过,但我想我实际上 post 我的代码并尝试获取一些 help/advice.
基本上,AI 可以帮助数学,我让程序自我介绍并询问它需要什么帮助,但是当我输入加法、减法等时,它只用数字来响应,而它应该用 "Great, I'll help you with Addition!/(whatever user input)"
第一个 运行 程序的屏幕截图:http://prntscr.com/elw7b4
输入用户需要帮助的内容后的屏幕截图:http://prntscr.com/elw7ky
(很明显,目前它有点到处都是,我先计算了计算器,因此它给出了额外的结果。
计算器在输入以下代码之前是可以工作的:(如你所见http://prntscr.com/elwavs 只有两个链接因为没有更多超过 10 次)
void Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of Assistance today?" << endl;
float inpsum;
cin >> inpsum;
cout << "Great!, I will help you with " << (inpsum) << endl;
}
但是输入上面的代码破坏了计算器。
完整代码如下:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
//user inputs what he needs help with/program output
void Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of Assistance today?" << endl;
cin >> inpsum;
cout << "Great!, I will help you with " << (inpsum) << endl;
}
//addition function
void Add() {
float add1, add2;
cout << "Please enter two values you want added together" << endl;
cin >> add1;
cin >> add2;
cout << "The answer is: " << (add1 + add2) << endl;
}
//subtraction function
void Subt() {
float subt1, subt2;
cout << "Please enter two values you want subtracted" << endl;
cin >> subt1;
cin >> subt2;
cout << "The answer is: " << (subt1 - subt2) << endl;
}
//division function
void Div()
{
float div1, div2;
cout << "Please enter two values you want divided" << endl;
cin >> div1;
cin >> div2;
cout << "The answer is: " << (div1 / div2) << endl;
}
//multiplication function
void Mult() {
float mult1, mult2;
cout << "Please enter two values you want multiplacted" << endl;
cin >> mult1;
cin >> mult2;
cout << "The answer is: " << (mult1 * mult2) << endl;
}
int main()
{
Inpsum(); //user inputs what they want help with
Add();
Subt();
Div();
Mult();
return 0 ;
}
基本上 - 我已经设置了计算器,它可以正常工作。但是在尝试在用户和程序之间实现输入和输出时,我出错了并且破坏了一切。而不是程序说 "Great I'll help you with Addition",它说 "Great, I'll help you with -134567432"
我不是要任何人为我做这件事,而是为我指明正确的方向,这样我才能真正知道将来该做什么。
使用枚举可能会有所帮助,因为您只有少数几个选择。你可以这样做:
enum class OPERATION : char {
Addition = 'A',
Subtraction = 'S',
Division = 'D',
Multiplication = 'M'
};
然后改为 cin 到一个字符串并具有以下内容:
std::string input;
std::cin >> input;
switch(static_cast<OPERATION>(input[0])) {
case OPERATION::Addition:
Add();
break;
case OPERATION::Subtraction:
Subt();
break;
case OPERATION::Division:
Div();
break;
case OPERATION::Multiplication:
Mult();
break;
default:
std::cerr << "Invalid input" << std::endl;
exit(1);
}
定义枚举将允许您将与其值相匹配的值转换为它。这使您可以安全地使用您希望在程序运行时看到的已定义输入进行切换。
请注意,您使用 float inpsum;
定义了 inpsum
,但您要存储的是字符串或单词。它们不兼容。您可以进一步了解 C++ 中的数据类型和字符串。
所以我必须创建一个 AI 程序来与用户交互并根据用户输入做出响应。我不是很有经验,这已经花了几个小时 lmao,我在网上看过,但我想我实际上 post 我的代码并尝试获取一些 help/advice.
基本上,AI 可以帮助数学,我让程序自我介绍并询问它需要什么帮助,但是当我输入加法、减法等时,它只用数字来响应,而它应该用 "Great, I'll help you with Addition!/(whatever user input)"
第一个 运行 程序的屏幕截图:http://prntscr.com/elw7b4 输入用户需要帮助的内容后的屏幕截图:http://prntscr.com/elw7ky (很明显,目前它有点到处都是,我先计算了计算器,因此它给出了额外的结果。
计算器在输入以下代码之前是可以工作的:(如你所见http://prntscr.com/elwavs 只有两个链接因为没有更多超过 10 次)
void Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of Assistance today?" << endl;
float inpsum;
cin >> inpsum;
cout << "Great!, I will help you with " << (inpsum) << endl;
}
但是输入上面的代码破坏了计算器。
完整代码如下:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
//user inputs what he needs help with/program output
void Inpsum()
{
cout << "Hello, my name is Eva! I am able to help you with basic Maths! How may I be of Assistance today?" << endl;
cin >> inpsum;
cout << "Great!, I will help you with " << (inpsum) << endl;
}
//addition function
void Add() {
float add1, add2;
cout << "Please enter two values you want added together" << endl;
cin >> add1;
cin >> add2;
cout << "The answer is: " << (add1 + add2) << endl;
}
//subtraction function
void Subt() {
float subt1, subt2;
cout << "Please enter two values you want subtracted" << endl;
cin >> subt1;
cin >> subt2;
cout << "The answer is: " << (subt1 - subt2) << endl;
}
//division function
void Div()
{
float div1, div2;
cout << "Please enter two values you want divided" << endl;
cin >> div1;
cin >> div2;
cout << "The answer is: " << (div1 / div2) << endl;
}
//multiplication function
void Mult() {
float mult1, mult2;
cout << "Please enter two values you want multiplacted" << endl;
cin >> mult1;
cin >> mult2;
cout << "The answer is: " << (mult1 * mult2) << endl;
}
int main()
{
Inpsum(); //user inputs what they want help with
Add();
Subt();
Div();
Mult();
return 0 ;
}
基本上 - 我已经设置了计算器,它可以正常工作。但是在尝试在用户和程序之间实现输入和输出时,我出错了并且破坏了一切。而不是程序说 "Great I'll help you with Addition",它说 "Great, I'll help you with -134567432"
我不是要任何人为我做这件事,而是为我指明正确的方向,这样我才能真正知道将来该做什么。
使用枚举可能会有所帮助,因为您只有少数几个选择。你可以这样做:
enum class OPERATION : char {
Addition = 'A',
Subtraction = 'S',
Division = 'D',
Multiplication = 'M'
};
然后改为 cin 到一个字符串并具有以下内容:
std::string input;
std::cin >> input;
switch(static_cast<OPERATION>(input[0])) {
case OPERATION::Addition:
Add();
break;
case OPERATION::Subtraction:
Subt();
break;
case OPERATION::Division:
Div();
break;
case OPERATION::Multiplication:
Mult();
break;
default:
std::cerr << "Invalid input" << std::endl;
exit(1);
}
定义枚举将允许您将与其值相匹配的值转换为它。这使您可以安全地使用您希望在程序运行时看到的已定义输入进行切换。
请注意,您使用 float inpsum;
定义了 inpsum
,但您要存储的是字符串或单词。它们不兼容。您可以进一步了解 C++ 中的数据类型和字符串。