2017-06-17 75 views
1

我有两个函数,一个用于文件读取,另一个用于数字的小排序。使用函数read()我正在读取文件的每一行并将每行放入数组中。文件看起来像:使用指针传递数组值

1 
2 
3 

随着功能sort()我想和值只打印数字大于1

什么是错的:我得到了印两个数组,但我的排序阵列仍打印所有值,而不是只有大于1

我的代码:

#include <iostream> 
    #include <fstream> 
    #include <string> 
    #include <cstdlib> 

    using namespace std; 

    class UZD 
    { 
    private: 
    int numLines; 
    int *value; 
    public: 
     UZD(); 
     int * read(); 
     int sort(); 
    }; 
    // ========================================================= 
    UZD::UZD() 
    { 
    } 
    // ========================================================= 
    int * UZD::read(){ 
    ifstream myfile("stu.txt"); 
    int value[20]; 
    string line[20]; 
    int i=0; 
    while(!myfile.eof() && i < 20) { 
     getline(myfile,line[i]); 
     ++i; 
      } 
    numLines = i; 
    for (i=0; i < numLines; ++i) { 
     value[i] = atoi(line[i].c_str()); 
     cout << i << ". " << value[i]<<'\n'; 
      } 
    return value; 
    } 
    // ========================================================= 
    int UZD::sort(){ 
    int *p; 
    p = read(); 
    int i; 
    if(p[i] > 1) { 
     cout << p <<'\n'; 
     } 
    } 
    // ========================================================= 
    int main() { 
    UZD wow; 
    wow.read(); 
    wow.sort(); 
    } 
+0

使用正确的工具来解决这样的问题是你的调试器。在*堆栈溢出问题之前,您应该逐行执行您的代码。如需更多帮助,请阅读[如何调试小程序(由Eric Lippert撰写)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,你应该[编辑]你的问题,以包含一个[Minimal,Complete,and Verifiable](http://stackoverflow.com/help/mcve)例子来重现你的问题,以及你在调试器中所做的观察。 –

+0

@πάνταῥεῖ我整天都在调试,我不明白这是什么错误。 – Diggs

+1

您可能没有正确使用调试器。 –

回答

0

有在你的代码中的许多问题,其中最明显的是“返回值”在读()实现方法具d。值是一个本地数组,并且将会超出read()的范围。此外,设计似乎有缺陷。你正在调用read()两次,一次从main()和一次sort()。 我写了一个工作代码,使用向量。可能这就是你所期望的:

#include <iostream> 
#include <fstream> 
#include <string> 
#include <cstdlib> 
#include <vector> 

using namespace std; 

class UZD 
{ 
     private: 
       int numLines; 
       vector<int> value; 
     public: 
       UZD(); 
       vector<int> & read(); 
       void sort(); 
}; 
// ========================================================= 
UZD::UZD() 
{ 
} 
// ========================================================= 
vector<int> & UZD::read(){ 
     ifstream myfile("stu.txt"); 
     vector<string> line(20); 
     int i=0; 
     while(!myfile.eof() && i < 20) { 
       getline(myfile,line[i]); 
       ++i; 
     } 
     numLines = i; 
     cout << "After reading file: " << endl; 
     for (i=0; i < numLines; ++i) { 
       value.push_back(atoi(line[i].c_str())); 
       cout << i << ". " << value[i]<<'\n'; 
     } 
     return value; 
} 
// ========================================================= 
void UZD::sort(){ 
     cout << "Inside sort()" << endl; 
     for(int i=0; i<value.size(); ++i){ 
       if(value[i] > 1) 
         cout << value[i] << endl; 
     } 
} 
// ========================================================= 
int main() { 
     UZD wow; 
     wow.read(); 
     wow.sort(); 
     return 0; 
} 

为了清晰起见,我保留了变量名。如果你没有得到任何东西,请告诉我。

+0

非常感谢。我用另一个函数编写sort函数,并用你的例子立即写入文件。再次感谢你。 – Diggs

+0

@Diggs我很乐意提供帮助。我添加了一个答案,没有载体并更正了你自己的代码。检查一下。 –

0

你的程序中有很多问题;只是提到其中一些: 在sort,你使用变量i未初始化,这是未定义的行为(可能崩溃);您编写的while(!myfile.eof()...通常被认为是错误的(see this SO answer;不推荐使用atoi,因为如果该参数不代表数字,则不安全;您将返回一个指向局部变量的指针,范围;你声明成员变量,而是通过本地的(例如,值)...

请看下面的代码这也证明了int*使用和vector<int>;希望它可以帮助

class UZD 
{ 
private: 
    int numLines; 
    int *value = nullptr; 
public: 
    ~UZD() { 
     if (value) 
      delete value; 
    }; 
    void read(); 
    void print(); 
}; 

// ========================================================= 
void UZD::read(){ 
    ifstream myfile("stu.txt"); 
    value = new int[20]; 
    int val; 
    numLines = 0; 
    while(numLines < 20 && myfile >> val) { 
     value[numLines] = val; 
     numLines++; 
    } 
} 
// ========================================================= 
void UZD::print(){ 
    for (int i=0; i<numLines; i++) 
     cout << value[i] << endl; 
} 


class UZD_vector 
{ 
private: 
    vector<int> values; 
public: 
    void read(); 
    void print(); 
}; 

// ========================================================= 
void UZD_vector::read(){ 
    ifstream myfile("stu.txt"); 
    int val; 
    while(myfile >> val) { 
     values.push_back(val); 
    } 
} 
// ========================================================= 
void UZD_vector::print(){ 
    for (auto val : values) 
     cout << val << endl; 
} 

// ================================================== ======= int main(){

cout << "with int[]:" << endl; 
UZD wow; 
wow.read(); 
wow.print(); 

cout << "with vector:" << endl; 
UZD wow_vector; 
wow_vector.read(); 
wow_vector.print(); 

}

0

这里是你自己的代码纠正,以防万一你发现矢量太难学了(这不应该是真实的,虽然)

#include <iostream> 
#include <fstream> 
#include <string> 
#include <cstdlib> 

using namespace std; 

class UZD 
{ 
     private: 
       int numLines; 
       int *value; 
       int num; 
     public: 
       UZD(); 
       void read(); 
       void sort(); 
}; 
// ========================================================= 
UZD::UZD():num(20) 
{} 
// ========================================================= 
void UZD::read(){ 
     ifstream myfile("stu.txt"); 
     value = new int[num]; 
     string line[num]; 
     int i=0; 
     while(!myfile.eof() && i < num) { 
       getline(myfile,line[i]); 
       ++i; 
     } 
     numLines = i; 
     for (i=0; i < numLines; ++i) { 
       value[i] = atoi(line[i].c_str()); 
       cout << i << ". " << value[i]<<'\n'; 
     } 
} 
// ========================================================= 
void UZD::sort(){ 
     cout << "After sorting: " << endl; 
     for (int i = 0; i < num; ++i) { 
       if(value[i] > 1) 
         cout << value[i] << endl; 
     } 
} 
// ========================================================= 
int main() { 
     UZD wow; 
     wow.read(); 
     wow.sort(); 
     return 0; 
} 
+0

只是倾销代码,而没有解释OP代码中的错误在哪里,也没有解释代码如何改进原始代码没有帮助。 – bolov

+0

@bolov我还没有做“只是倾销代码”。这个答案是对我之前答案的一个延续,我清楚地指出了原始代码中的主要问题,并使用矢量实现了一个解决方案。然后我纠正了原来的代码,认为可能会限制使用矢量。你没有检查早期的答案,只是批评:-) –

+0

不,我没有检查其他答案。我不需要为了理解这个答案。答案应该是孤立的,你不能指望人们阅读所有的答案,以理解一个答案。在有很多答案的帖子中,你的两个答案可能会变得相距甚远。我再说一遍:正如一个问题本身应该有一些问题,而不需要检查一些外部链接来理解它,应该单独理解一个答案。 – bolov