Question: i don't want my code to repeat my output twice : #include #include #include #include #include using namespace std; class Bits { private: string sequence;

i don't want my code to repeat my output twice : #include
#include
#include
#include
#include
using namespace std;
class Bits {
private:
string sequence;
string packet;
public:
Bits(const string& seq, const string& pkt) : sequence(seq), packet(pkt){}
string getSequence() const { return sequence; }
string getPacket() const { return packet; }
void toString() const {
cout << "Sequence: "<< sequence <<" Packet: "<< packet << endl;
}
};
vector readBitsFromFile(const string& filename, const string& user_sequence){
vector bitsObjects;
unordered_map uniqueSequences;
ifstream inputFile(filename);
if (!inputFile.is_open()){
cerr << "Error: Unable to open file "<< filename << endl;
return bitsObjects;
}
string word;
while (inputFile >> word){
if (word.find(user_sequence)!= string::npos && uniqueSequences.find(word)== uniqueSequences.end()){
string packet ="P"+ to_string(word.length())+","+ word;
uniqueSequences[word]= packet; // Store word and corresponding packet in map
}
}
inputFile.close();
// Convert map entries to Bits objects
for (const auto& entry : uniqueSequences){
bitsObjects.emplace_back(user_sequence, entry.second);
}
return bitsObjects;
}
int main(){
string user_sequence;
cout << "Enter a sequence of 0's and 1's: ";
cin >> user_sequence;
for (char bit : user_sequence){
if (bit !='0' && bit !='1'){
cerr << "Error: Invalid input. Please enter only 0's and 1's."<< endl;
return 1;
}
}
vector bitsObjects = readBitsFromFile("bits.txt", user_sequence);
for (const auto& bitsObject : bitsObjects){
bitsObject.toString();
std::cout.flush();
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!