[英]How to compare string char to struct array?
My goal is to turn a user imputed string to Tap code/Knock code.我的目标是将用户估算的字符串转换为 Tap code/Knock code。 https://en.wikipedia.org/wiki/Tap_code https://en.wikipedia.org/wiki/Tap_code
The problem i'm having is that i don't know how to compare the char i get from the string to the struct array.我遇到的问题是我不知道如何将从字符串中获得的字符与结构数组进行比较。
I want to read a letter of a string then compare it with the structure array.我想读取一个字符串的字母,然后将其与结构数组进行比较。 So if i input the letter "a" the code will compare it with the array and output the corresponding row and column assigned to the letter, in this case 1 1.因此,如果我输入字母“a”,代码会将其与数组和 output 分配给该字母的相应行和列进行比较,在这种情况下为 1 1。
What can be done to compare these two things?可以做些什么来比较这两件事?
Struct is mandatory here.结构在这里是强制性的。
(New to programming) (编程新手)
#include <iostream>
#include <string>
using namespace std;
struct letter
{
int row;
int column;
};
int main()
{
int x1 = 0;
string input;
letter a; a.row = 1; a.column = 1;
letter b; b.row = 1; b.column = 2;
letter c; c.row = 1; c.column = 3;
letter d; d.row = 1; d.column = 4;
letter e; e.row = 1; e.column = 5;
letter f; f.row = 2; f.column = 1;
letter g; g.row = 2; g.column = 2;
letter h; h.row = 2; h.column = 3;
letter i; i.row = 2; i.column = 4;
letter j; j.row = 2; j.column = 5;
letter k; k.row = 1; k.column = 3;
letter l; l.row = 3; l.column = 1;
letter m; m.row = 3; m.column = 2;
letter n; n.row = 3; n.column = 3;
letter o; o.row = 3; o.column = 4;
letter p; p.row = 3; p.column = 5;
letter q; q.row = 4; q.column = 1;
letter r; r.row = 4; r.column = 2;
letter s; s.row = 4; s.column = 3;
letter t; t.row = 4; t.column = 4;
letter u; u.row = 4; u.column = 5;
letter v; v.row = 5; v.column = 1;
letter w; w.row = 5; w.column = 2;
letter x; x.row = 5; x.column = 3;
letter y; y.row = 5; y.column = 4;
letter z; z.row = 5; z.column = 5;
struct letter arr[26] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z };
cout << "Input string using only small letters.\n";
getline(cin, input);
int length = input.size();
for (int x = 0; x < length; x++)
{
do
{
x1++;
}
while (input[x] != arr[x1]); ////////////////////////////This
}
}
1>------ Build started: Project: labwokrk4, Configuration: Debug Win32 ------
1>labwokrk4.cpp
1>C:\Users\lietotajs\Desktop\corse work\labwokrk4\labwokrk4.cpp(62,29): error C2678: binary '!=': no operator found which takes a left-hand operand of type 'char' (or there is no acceptable conversion)
1>C:\Program Files (x86)\visual basic temp\VC\Tools\MSVC\14.23.28105\include\system_error(253,24): message : could be 'bool std::operator !=(const std::error_condition &,const std::error_condition &) noexcept'
1>C:\Program Files (x86)\visual basic temp\VC\Tools\MSVC\14.23.28105\include\system_error(248,24): message : or 'bool std::operator !=(const std::error_condition &,const std::error_code &) noexcept'
1>C:\Program Files (x86)\visual basic temp\VC\Tools\MSVC\14.23.28105\include\system_error(243,24): message : or 'bool std::operator !=(const std::error_code &,const std::error_condition &) noexcept'
1>C:\Program Files (x86)\visual basic temp\VC\Tools\MSVC\14.23.28105\include\system_error(238,24): message : or 'bool std::operator !=(const std::error_code &,const std::error_code &) noexcept'
1>C:\Program Files (x86)\visual basic temp\VC\Tools\MSVC\14.23.28105\include\exception(302,24): message : or 'bool std::operator !=(const std::exception_ptr &,std::nullptr_t) noexcept'
1>C:\Program Files (x86)\visual basic temp\VC\Tools\MSVC\14.23.28105\include\exception(298,24): message : or 'bool std::operator !=(std::nullptr_t,const std::exception_ptr &) noexcept'
1>C:\Program Files (x86)\visual basic temp\VC\Tools\MSVC\14.23.28105\include\exception(294,24): message : or 'bool std::operator !=(const std::exception_ptr &,const std::exception_ptr &) noexcept'
1>C:\Users\lietotajs\Desktop\corse work\labwokrk4\labwokrk4.cpp(62,29): message : while trying to match the argument list '(char, letter)'
1>Done building project "labwokrk4.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
There's actually no need to loop through the array to find the letter that matches.实际上不需要遍历数组来查找匹配的字母。 You can use character arithmetic to transform a letter from the input into an index of your Tap Code array.您可以使用字符算法将输入中的字母转换为 Tap Code 数组的索引。
That may look like:这可能看起来像:
for (int x = 0; x < length; x++)
{
if(std::isalpha(static_cast<unsigned char>(input[x]))) { // If it's not a letter, don't bother
continue;
}
int indx = std::tolower(static_cast<unsigned char>(input[x])) - 'a'; // Transform into index
letter tap = arr[indx];
// Do Tap Code stuff...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.