2015-10-30 132 views
-2

當我運行我的程序時,我不斷收到此錯誤。該程序仍然會執行並運行並在終端中顯示正確的nymbers,但我需要它輸出另一個文件並將其放在那裏。請幫助新的C++。執行時出現C++數組錯誤

的bash-3.2 $克++ -Wall 1.cpp
1.cpp:在函數 '的std :: string IP_Calculation(的std :: string *,性病::字符串,整數)':1.cpp: 71:1:警告:控制到達非void函數的端[-Wreturn型]


string IP_Calculation(string IP[], string Company_name, int total_IPS) 
{ 
    string temp = ""; 
    char buf[80]; 
    char buf2[80]; 
    if (total_IPS != 0) 
    { 
     int UniqueIP_count = total_IPS; 

     for (int i = 0; i < total_IPS; i++) 
     { 
      for (int j = i + 1; j < total_IPS; j++) 
      { 

       if (strcmp(IP[i].c_str(), IP[j].c_str()) == 0) 
       { 
        if (strcmp(IP[i].c_str(), "") == 0) 
        { 
         continue; 
        } 

        IP[j] = ""; 
        UniqueIP_count--; 

       } 

      } 

     } 

     temp = print_array(IP); 

     cout << Company_name << " | Number of Visitor: " << total_IPS 
       << "| Unique   Visitors: " << UniqueIP_count << endl; 
     //cout<<Company_name<<" | Number of Visitor: "<<buf <<"| Unique Visitors:  "<<UniqueIP_count<<endl; 
     cout << temp; 
     sprintf(buf, "%d", total_IPS); 
     sprintf(buf2, "%d", UniqueIP_count); 
     // return temp=Company_name+" | Number of Visitor: "+(total_IPS) +"|  Unique Visitors: "+to_string(UniqueIP_count)+"\n"+temp+"\n"; 
     return temp = Company_name + " | Number of Visitor: " + buf 
       + "| Unique Visitors: " + buf2 + "\n" + temp + "\n"; 
    } 
} 
+0

其他代碼我不知道它爲什麼不會出現 –

+0

cout << Company_name <<「|訪客數量:」<< total_IPS <<「|唯一訪客:」<< UniqueIP_count << endl; // cout << Company_name <<「|訪客數量:」<< buf <<「|唯一訪客:」<< UniqueIP_count << endl; cout << temp; sprintf(buf,「%d」,total_IPS); sprintf(buf2,「%d」,UniqueIP_count); // return temp = Company_name +「|訪客數量:」+(total_IPS)+「|唯一訪客:」+ to_string(UniqueIP_count)+「\ n」+ temp +「\ n」; \t return temp = Company_name +「|訪客數量:」+ buf +「|唯一訪客:」+ buf2 +「\ n」+ temp +「\ n」; \t} } –

+0

編輯,可讀格式是強烈的首選。 – LogicStuff

回答

-2

我不知道如果我明白了。你想將輸出寫入文件?像這樣的東西應該工作:

int writeFile() 
{ 
    ofstream myfile; 
    myfile.open ("example.txt"); 
    myfile << "Writing this to a file.\n"; 
    myfile.close(); 
    return 0; 
} 
+0

這與他得到的警告無關。 – Barmar

+0

@Barmar對不起,我不回答,因爲有人這樣做,所以我只幫助其他問題。或者我認爲是另一個問題。 – leoxs

1

該警告是因爲函數聲明爲返回一個string,但是編譯器已經確定,這是可能的,它可能無法做到這一點。您有一個return語句,該語句在您的if語句中返回一個字符串。但是當Total_IPs0時,您將不會執行該代碼塊,並且您將永遠不會執行該return語句。由於您沒有else塊,因此您只需退出該功能即可,不需要根據需要返回字符串。您需要將其更改爲:

if (Total_IPs != 0) { 
    ... 
} else { 
    return ""; 
} 

以便您在條件失敗時返回某些內容。