即時通訊嘗試在C++中編寫一個程序,該程序可以通過一個txt文件工作,如果這個文件中的數字有重複,不要打印它們,只打印一次出現的數字。C++從文件中刪除重複的數字
這是我得到的代碼。但會發生什麼是它打印出來的文件,然後打印出第二行,而不是尋找dublicates ...
任何人都可以告訴我哪裏會出錯。相當新的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;
}
你說這編譯? – 2013-04-24 02:37:21
您正在評論無需解釋的行。我不知道你的老師是否堅持你這樣做,但是一旦課程結束,請停止。代碼應該自我解釋 - 只有在不可能爲您的問題編寫自解釋代碼時才應該使用註釋,或者效率是一個問題,並且您使用了一種需要解釋的不直觀的算法。 'if(myfile.is_open())//如果文件是開放的'真的是多餘的 - 你只是重複代碼已經說過的。 – SelectricSimian 2013-04-24 02:37:24
是啊,它符合,它會打印文件中的數字,然後用第二行數字打印第二行,但不會刪除重複。任何人都知道我可以修復這個問題? – 2013-04-24 02:41:18