C 不将重复数字打印到输出
C++ Not printing duplicate numbers to the output
我必须在一个.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;
}
相关文章:
- 数组类 阵列的打印输出
- 打印输出在主,而不是在函数中
- 执行ls命令并打印输出
- 在代码仍在 C++ QT 中运行时显示打印输出
- 函数打印输出C 的错误
- 如果关闭娱乐后()所有内存都会被交易,则如何打印输出
- 如何水平排列程序的打印输出循环而不重复
- 程序被卡住了,不打印输出
- 使用递归打印输出
- 指针类函数不打印输出
- 没有得到 ASCII 编号 = 7 的字符串打印输出
- 以未格式化的输出 c++ 打印输出
- 将值输入到结构数组 + 打印输出
- 程序运行,但程序中的第二个功能不打印?输出中没有错误
- 抑制Python打印输出
- 输出正确,但每次打印输出后程序崩溃
- 无限循环heisenbug:如果我添加打印输出,它就会退出
- 为类打印输出(打印调试信息)的最佳实践是什么?
- 正在检查CppUnit中的打印输出
- 如何在C++中解决此打印输出