2013-04-24 54 views
0

即時通訊嘗試在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; 
} 
+0

你說這編譯? – 2013-04-24 02:37:21

+2

您正在評論無需解釋的行。我不知道你的老師是否堅持你這樣做,但是一旦課程結束,請停止。代碼應該自我解釋 - 只有在不可能爲您的問題編寫自解釋代碼時才應該使用註釋,或者效率是一個問題,並且您使用了一種需要解釋的不直觀的算法。 'if(myfile.is_open())//如果文件是開放的'真的是多餘的 - 你只是重複代碼已經說過的。 – SelectricSimian 2013-04-24 02:37:24

+0

是啊,它符合,它會打印文件中的數字,然後用第二行數字打印第二行,但不會刪除重複。任何人都知道我可以修復這個問題? – 2013-04-24 02:41:18

回答

1

至少有一個錯誤:array被聲明爲一個整數數組,你正在閱讀的字符串line並分配stringint正下方:

getline (myfile,line); //^^line is string, array[i] is int 
array[loop] = line; 

您可以嘗試閱讀向量中的那些行然後調用std :: unique以使向量唯一併將其打印出來。您的文件行不一定是整數行,因此將它們存儲在整數數組中可能無效。

你可以試試:

#include <vector> 
#include <string> 
#include <algorithm> 
#include <iterator> 

int main() 
{ 
    std::vector<std::string> data; 
    ifstream myfile ("problem3.txt"); 
    string line; 
    //if not required to use array 
    while (getline(myfile, line)) 
    { 
     data.push_back(line); 
    } 

    std::vector<std::string> data(dataArray, dataArray + 100); 

    myfile.close(); 
    std::sort(data.begin(), data.end()); 
    data.erase(std::unique(data.begin(), data.end()), data.end()); 
    //now print vector out: 
    std::copy(data.begin(), data.end(), ostream_iterator<string>(cout, " \n")); 
    return 0; 
} 
+0

本身調用'std :: unique'幾乎是不正確的。你需要用它的返回值做一些事情,否則你會得到一系列不確定的值,你不知道它從哪裏開始。一般來說,將它和末尾迭代器一起傳遞給容器的'erase'成員函數是所需的功能。與你使用'std :: remove'或'std :: remove_if'做類似。 – 2013-04-24 03:02:49

+0

@BenjaminLindley謝謝,是的,我想我錯過了什麼,謝謝!將在幾分鐘內更新它。 – taocp 2013-04-24 03:05:17

+0

@BenjaminLindley更新,請你看看是否你的意思是?非常感謝! – taocp 2013-04-24 03:09:13

0

而不是使用嵌套的for循環中,我會建議使用一個數組來保持一定的整數多少次顯示出來計數。然後,您可以循環一次,只打印計數爲1的整數。

嵌套for循環的一個問題是由於您只檢查重複項,只要j < i,您可能會打印一個整數即使它在數組中稍後有重複。您只需檢查整個數組以確保沒有重複項。

如果你仍然想嘗試,你可能想要做這樣的事情:

for (int i = 0; i < loop; i++) // start at 0! 
{ 
    bool matching = false; 
    for (int j=0; j<loop; j++) { // check the whole array for duplicates! 
     if (i != j && array[i] == array[j]) { 
      matching = true; 
      break; // instead of complicated condition in for loop 
     } 
    } 
    if (!matching) cout << array[i] << " "; 
}