我试图用 c++ 制作一个可以通过 txt 文件运行的程序,如果这个文件中的数字有重复,不要打印它们,只打印出现一次的数字。
这是我得到的代码。但发生的事情是它打印出文件,然后再次打印出第二行而不是寻找重复项......
谁能告诉我我哪里出错了。对 C++ 来说相当新
// array.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
int array[100]; // creates array to hold numbers
short loop=0; //short for loop for input
string line; //this will contain the data read from the file
ifstream myfile ("problem3.txt"); //opening the file.
if (myfile.is_open()) //if the file is open
{
while (! myfile.eof() ) //while the end of file is NOT reached
{
getline (myfile,line); //get one line from the file
array[loop] = line;
cout << array[loop] << endl; //and output it
loop++;
}
for (int i = 1; i < loop; i++)
{
bool matching = false;
for (int j = 0; (j < i)&& (matching == false); j++)
{
if (array[i] == array[j])
matching = true;
}
if (!matching)
cout<< array[i] << " "
}
myfile.close(); //closing the file
}
else
cout << "Unable to open file"; //if the file is not open output
system("PAUSE");
return 0;
}