C++ ostream操作员<&书信电报;工作不正常

C++ ostream操作员<&书信电报;工作不正常,c++,arrays,printing,hashtable,ostream,C++,Arrays,Printing,Hashtable,Ostream,奥斯特雷姆问题 My ostream运算符函数Customer::getHash中存在逻辑错误。这可能无法解决您的问题,但无论如何都应该解决 int Customer::getHash(int hash) { string key = getLastname(); cout<<"key: "<<key<<endl; // getFirstname(); // getID(); int i = 0; // int

奥斯特雷姆问题
My ostream运算符函数
Customer::getHash
中存在逻辑错误。这可能无法解决您的问题,但无论如何都应该解决

int Customer::getHash(int hash)
{
    string key = getLastname();
    cout<<"key: "<<key<<endl;
    // getFirstname();
    //  getID();
    int i = 0;
    // int j = 0;
    // int k = 0;
    for (i = 0; i < key.length(); i++)
    {
        i += (int)key[i]; // Problem.
                          // At this time, i may be greater than key.length().
    }
   // getFirstname();
   // getID();
   return  i = i % hash;

}
作为一种副作用

 while (inputFile >> newCustomer)
没有定义

取消对该行的注释

//return in;
在函数中。这将修复另一个错误。希望这是最后一个

更新2

您在
循环中读取了太多信息

 // This line reads all the information of one customer
 while (inputFile >> newCustomer)
 {
    //inputFile >> newCustomer;
    string lastname;

    // PROBLEM
    // Now you are reading data corresponding to the next customer.

    getline (inputFile, lastname, ' ');
    while (inputFile.peek() == ' ')
       inputFile.get();

    string firstname;
    getline (inputFile, firstname, ' ');
    while (inputFile.peek() == ' ')
       inputFile.get();

    string id;
    getline (inputFile, id);

    buildCustomerList(cHeadPtr, cTailPtr, lastname, firstname, id);
    customer.insert(newCustomer);
    //cout<<lastname<<endl;
    //cout<<firstname<<endl;
    //cout<<id<<endl;
 }

嗯,我想(int)key[i]可以给你垃圾数据,因为大小不匹配……我的大小是512,进入getHash的大小是512。你有什么建议?谢谢,我明白我的错误了。谢谢我的for循环是否正常工作?在我看来,这里有一些非常简单的东西,但我听不懂!!Thanks@kris,对我来说,
循环看起来不错。@R Sahu,我发布了我的完整程序。我在想,当我阅读文件并设置数据时,可能出现了问题?如果能提供任何帮助,我将不胜感激,我几乎花了整整3天的时间来解决这个问题,我想我几乎可以做到done@R萨胡,谢谢你的帮助。这并没有解决我的问题,但这是问题的一部分。谢谢我猜我的设置和插入功能被破坏了,因为只有一条记录被打印出来,而不是512
//return in;
 // This line reads all the information of one customer
 while (inputFile >> newCustomer)
 {
    //inputFile >> newCustomer;
    string lastname;

    // PROBLEM
    // Now you are reading data corresponding to the next customer.

    getline (inputFile, lastname, ' ');
    while (inputFile.peek() == ' ')
       inputFile.get();

    string firstname;
    getline (inputFile, firstname, ' ');
    while (inputFile.peek() == ' ')
       inputFile.get();

    string id;
    getline (inputFile, id);

    buildCustomerList(cHeadPtr, cTailPtr, lastname, firstname, id);
    customer.insert(newCustomer);
    //cout<<lastname<<endl;
    //cout<<firstname<<endl;
    //cout<<id<<endl;
 }
 while (inputFile >> newCustomer)
 {
    string lastname = newCustomer.getLastname();
    string firstname = newCustomer.getFirstname();
    string id = newCustomer.getID();

    buildCustomerList(cHeadPtr, cTailPtr, lastname, firstname, id);
    customer.insert(newCustomer);
 }