C 不将重复数字打印到输出

C++ Not printing duplicate numbers to the output

本文关键字:打印 输出 数字      更新时间:2023-10-16

我必须在一个.txt文件中阅读,该文件包含用于确定谁将赢得假装选举的投票。这是文件的一点点,因此您可以了解。

1YYYYYYYYYYThe New Guy
2YNYNYNYNYNHarry Potter
2YNNYYNNYYNHarry Potter
2NNNNNNNNNNThe New Guy
3NYNYNYNYNYThe New Guy
3YYYYYYYYYYHarry Potter
3YYYYYYYYNYHarry Potter

第一个数字是" ID"号码,如果它们是该数字的任何重复项,我不应该将其读取到输出中。显示。这些数字都按顺序排列。因此,它可以如图所示,但不能进行1222333。这是我到目前为止的代码,循环仅在达到第一个重复时就停止,任何帮助将不胜感激。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <limits>
#include <cstdio>
#include <string>
int main()
{
int ID; //pirate ID number
int IDTest;
char ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, ch9, ch10; //vote on amendments
std::string vote; //vote for captain
std::ifstream fileReader; //open file for reading
fileReader.open("BallotsHogwart.txt");
if (fileReader.fail())
{
   std::cout << "The ballots failed to open with code: " << fileReader.failbit;
}
else
{
    do
    {
        IDTest = ID;
        fileReader >> ID;
        if (IDTest != ID)
        {
            std::cout << "Id:" << ID;
            fileReader >> ch1;
            fileReader >> ch2;
            fileReader >> ch3;
            fileReader >> ch4;
            fileReader >> ch5;
            fileReader >> ch6;
            fileReader >> ch7;
            fileReader >> ch8;
            fileReader >> ch9;
            fileReader >> ch10;
            std::cout << " char 1 - 10: " << ch1 << ch2 <<  ch3 << ch4 << ch5 << ch6 << ch7 << ch8 << ch9 << ch10;
            getline(fileReader,vote);
            std::cout << " the votee: " << vote << std::endl;
        }
    } while(!fileReader.eof());
}
fileReader.close();
return 0;

}

当使用getline((

重复ID时,您需要忽略该行

代码将是这样:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <limits>
#include <cstdio>
#include <string>
int main() {
    int ID = 0; //pirate ID number
    int IDTest;
    char ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, ch9, ch10; //vote on amendments
    std::string vote; //vote for captain
    std::string ignore; //ignore
    std::ifstream fileReader; //open file for reading
    fileReader.open("asdas.txt");
    if (fileReader.fail())
    {
        std::cout << "The ballots failed to open with code: " << fileReader.failbit;
    }
    else
    {
        do
        {
            IDTest = ID;
            fileReader >> ID;
            if (IDTest != ID)
            {
                std::cout << "Id:" << ID;
                fileReader >> ch1;
                fileReader >> ch2;
                fileReader >> ch3;
                fileReader >> ch4;
                fileReader >> ch5;
                fileReader >> ch6;
                fileReader >> ch7;
                fileReader >> ch8;
                fileReader >> ch9;
                fileReader >> ch10;
                std::cout << " char 1 - 10: " << ch1 << ch2 << ch3 << ch4 << ch5 << ch6 << ch7 << ch8 << ch9 << ch10;
                getline(fileReader, vote);
                std::cout << " the votee: " << vote << std::endl;
            }
            else {
                getline(fileReader, ignore);
            }
        } while (!fileReader.eof());
    }
    fileReader.close();
    std::cin.get();
    return 0;
}

我将使用 map

std::ifstream fileReader; //open file for reading
fileReader.open("BallotsHogwart.txt");
if (fileReader.fail())
{
    std::cout << "The ballots failed to open with code: " << fileReader.failbit;
}
else
{
    string line;
    std::map<string, string> myvotes;
    do 
    {
        getline(filereader, line);
        // pull out ID
        std::map<string,string>::iterator value = myvotes.find(ID); 
        if (value == myvotes.end())
           continue;
        // do the rest of your processing on line.
        // put "Harry Potter" in map.
        myvotes.insert(ID, candidate);
    } while(!fileReader.eof());
    fileReader.close();
    return 0;
}