私の英語はあまり上手ではありませんが、あなたが私を理解してくれることを願っています。C++ では文字列をコードに変換できないことはわかっていますが、問題を解決する別の方法が見当たりません。
私は構造の配列を持っています。データベースと言えます。タスクは、ユーザーが複合リクエストを作成することです。コンソールで、ユーザーは構造の 2 つのパラメーターを選択し、結合された要求を行います。このようなもの
cout<<"\nВыберите первый параметр для поиска:" // Choose first parameter
<<"\n1. processor"
<<"\n2. videocard"
<<"\n3. display"
<<"\n4. RAM"
<<"\n5. size"<<endl;
int first;
cin>>first;
cout<<"\nВыберите второй параметр для поиска:" // Choose second parameter
<<"\n1. processor"
<<"\n2. videocard"
<<"\n3. display"
<<"\n4. RAM"
<<"\n5. size"<<endl;
int second;
cin>>second;
cout<<"enter searchkey for first value: "
string search1;
cin>>search1;
cout<<"enter searchkey for second value: "
string search2;
cin>>search2;
string parameters[ 5 ] = { "processor", "videocard", "display", "RAM", "size" };
for ( i = 0; i < size; i++ ) // And that's where it all goes wrong.
if ( arrayOfStructs.parameters[ first ] == search1 && arrayOfStructs.parameters[ second ] == search2 )
cout<<"found a match"<<endl;
コードが機能しない理由はわかっています。私に似たソリューションが存在すると確信しています。私の「解決策」は列挙型のように見えますが、この場合の列挙型は適切ではありません。解決策を知っている場合は、以下に書き留めてください。
私のプログラムの完全なコード
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct computer
{
string processor;
string videocard;
string display;
string RAM;
string size;
string getFieldValue( int );
};
string computer::getFieldValue( int fieldNumber )
{
stringstream str;
switch (fieldNumber)
{
case 1: str<<this->processor;
case 2: str<<this->videocard;
case 3: str<<this->display;
case 4: str<<this->RAM;
case 5: str<<this->size;
}
return str.str();
}
void coutAndWrite( computer aStruct, ofstream &aFile );
int main()
{
setlocale( 0, "" );
computer sklad[ 30 ];
computer temp;
int i = 0, j;
ifstream fromFile("structury.txt");
while ( !fromFile.eof() )
{
fromFile>>sklad[ i ].processor
>>sklad[ i ].videocard
>>sklad[ i ].display
>>sklad[ i ].RAM
>>sklad[ i ].size;
i++;
}
fromFile.close();
ofstream rezultaty("rezultaty.txt");
for ( i = 0; i < 30; i++ )
for ( j = 0; j < 29; j++ )
if ( sklad[ j + 1 ].processor[ 0 ] < sklad[ j ].processor[ 0 ] )
{
temp = sklad[ j + 1 ];
sklad[ j + 1 ] = sklad[ j ];
sklad[ j ] = temp;
}
while ( 1 )
{
cout<<"\nВыберите тип запроса:"
<<"\n1. Простой"
<<"\n2. Составной"
<<"\n0. Выход из программы\n";
int prostoiIliSostavnoi;
cin>>prostoiIliSostavnoi;
if ( prostoiIliSostavnoi == 0 )
break;
if ( prostoiIliSostavnoi == 1 )
{
cout<<"\nВыберите параметр для поиска:"
<<"\n1. processor"
<<"\n2. videocard"
<<"\n3. display"
<<"\n4. RAM"
<<"\n5. size"<<endl;
int parametr;
cin>>parametr;
cout<<"Введите ключ поиска: ";
string poisk;
cin>>poisk;
cout<<"Результаты поиска: ";
for ( i = 0; i < 30; i++ )
if ( sklad[ i ].getFieldValue( parametr ) == poisk )
coutAndWrite( sklad[ i ], rezultaty );
}
}
system("pause");
}
void coutAndWrite( computer aStruct, ofstream &aFile )
{
cout<<"\nprocessor: "<<aStruct.processor
<<"\nvideocard: "<<aStruct.videocard
<<"\ndisplay: "<<aStruct.display
<<"\nRAM: "<<aStruct.RAM
<<"\nsize: "<<aStruct.size<<endl<<endl;
aFile<<setw( 15 )<<aStruct.processor
<<setw( 15 )<<aStruct.videocard
<<setw( 20 )<<aStruct.display
<<setw( 10 )<<aStruct.RAM
<<setw( 10 )<<aStruct.size<<endl;
}