私はこのメッセージボードを販売したいと思っています。最終的にすべてを読み込む方法を理解しましたが、アイテムを比較する方法についてのアドバイスが必要です。ファイルを読み込んだときに、配列をチェックして、必要な項目がある(true)かどうかを確認し、すでに配列に入っている項目の名前を数えて販売したいとします。一致が見つかった場合は、その項目を追加せず、配列から必要な項目を削除してシフトします。マッチの追加と削除を同時に行っている間に配列内のアイテムを比較する
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <list>
using namespace std;
struct messageBoard {
string item;
bool forSale;
int price;
};
int main(){
messageBoard board;
const int arrayLength = 100;
std::list<messageBoard> arr;
int index = 0;
string filename;
ifstream words;
cout<<"Please enter the filename:";
cin>>filename;
words.open(filename);
if (words.fail()) {
cout << "file not found or something" << endl;
}else {
string word;;
while (getline(words, word)) {
int wordIndex = 0;
stringstream ss;
ss << word;
while (getline(ss, word, ',')){
if (wordIndex==0){
board.item = word;
}
else if (wordIndex==1&&word==" for sale"){
board.forSale = false;
}
else if (wordIndex==1&&word==" wanted"){
board.forSale = true;
}
else if (wordIndex==2){
board.price = atoi(word.c_str());
}
wordIndex++;
}
index ++;
arr.push_back(board);
}
}
words.close();
for(std::list<messageBoard>::iterator it = arr.begin(); it != arr.end(); it++) {
std::cout << "item: " << (it)->item << " bool: " << (it)->forSale <<"Price: "<<(it)->price << std::endl;
}
}
アドバイスをいただき、ありがとうございました。今後の進歩のためのガイドラインとしてご利用いただけると幸いです。 –
私はあなたが提案したようにリストを実装しました。あなたが話したブール関数にはどのようなパラメータが必要でしょうか。ボードとアールを比較するか? @ジェームズ・K・ローデン –