简体   繁体   中英

Conversion of string to char in c++

I am trying to build a spell check validator using Hash Table. I have list of words in a text file. I want to imported them to program and entered them into Hash Table using seperate chaining. Now, I want to run the program and I have these two errors. Can anyone help me with this?

at line 30-- no matching function for call to 'std::__cxx11::basic_string::push_back(std::__cxx11::string&)'

at line 40-- no match for 'operator==' (operand types are '__gnu_cxx::__alloc_traits, char>::value_type' {aka 'char'} and 'std::__cxx11::string' {aka 'std::__cxx11::basic_string'})

I know it's simple mistake of converting str to char but I couldn't figure out how to do without changing the rest of the program.

I would like to have a simple solution which doesnot change the existing code. If it is not possible please tell me how to proceed.

#include<iostream>
#include <string>
#include <cstring>
#include <fstream>


std::string hashTable[27];
int hashTableSize = 27;
#define MAX_LEN 27
using namespace std;


int hashFunc(std::string s)
{
    // A simple hashing, no collision handled
    int sum=0,index=0;
    for(std::string::size_type i=0; i < s.length(); i++)
    {
        sum += s[i];
    }
    index = sum % MAX_LEN;
    return index;
}

void insert(std::string s)
{
    // Compute the index using Hash Function
    int index = hashFunc(s);
    // Insert the element in the linked list at the particular index
    hashTable[index].push_back(s);
    }

void search(string s)
{
    //Compute the index by using the hash function
    int index = hashFunc(s);
    //Search the linked list at that specific index
    for(int i = 0;i < hashTable[index].size();i++)
    {
        if(hashTable[index][i] == s)
        {
            cout << s << " is found!" << endl;
            return;
        }
    }
    cout << s << " is not found!" << endl;
}
int main(){


    //opening text file
    std::ifstream inFile;
    inFile.open("un.txt");

    // If text file doesnot exist or not included in root folder.
    if(inFile.fail()) {

        std::cerr << "Error opening file"<< std::endl ;

        exit(1);
    }

    //if text file exists.
    std::string wordsinfile;
    std::string words[100];
    int count=0,i=0;
    std::string str;


    // writing words from text file into Array.
    while( !inFile.eof()) {
        inFile >> wordsinfile;
        words[i]=wordsinfile;
        count++;
        i++;
    }

    for (i=0;i<100;i++){
        std::cout<< words[i]<<std::endl;
    }


    for(i=0;i<=23;i++) {
        insert(words[i]);
    }


    int choice;
    string z;
    string y;
    while(1) {

        cout << "Enter choice. 1) Insert\n 2) Search\n 3) Exit\n";
        cin >> choice;
        switch (choice) {
            case 1:
                cin>>y;
                insert(y);
                break;
            case 2:

                cin>>z;
                search(z);
                break;
            case 3:
                exit(0);
        }
    }
    return 0;
}

My txt file had 38 different words and size of hash table is 27

Here's the correct version of your program. Your hashtable is supposed to be a collection of strings and since you are using indexing in the search function, using an STL vector for hashtable should do the trick.

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

std::vector<std::string> hashTable[27];
int hashTableSize = 27;
#define MAX_LEN 27
using namespace std;


int hashFunc(std::string s)
{
    // A simple hashing, no collision handled
    int sum=0,index=0;
    for(std::string::size_type i=0; i < s.length(); i++)
    {
        sum += s[i];
    }
    index = sum % MAX_LEN;
    return index;
}

void insert(std::string s)
{
    // Compute the index using Hash Function
    int index = hashFunc(s);
    // Insert the element in the linked list at the particular index
    hashTable[index].push_back(s);
    }

void search(string s)
{
    //Compute the index by using the hash function
    int index = hashFunc(s);
    //Search the linked list at that specific index
    for(int i = 0;i < hashTable[index].size();i++)
    {
        if(hashTable[index][i] == s)
        {
            cout << s << " is found!" << endl;
            return;
        }
    }
    cout << s << " is not found!" << endl;
}

int main(){
    //opening text file
    std::ifstream inFile;
    inFile.open("un.txt");

    // If text file doesnot exist or not included in root folder.
    if(inFile.fail()) {

        std::cerr << "Error opening file"<< std::endl ;

        exit(1);
    }

    //if text file exists.
    std::string wordsinfile;
    std::vector<std::string> words;
    std::string str;


    // writing words from text file into Array.
    while( inFile >> wordsinfile) {
        words.push_back(std::move(wordsinfile));
    }

    std::cout << "Total words read: " << words.size() << std::endl;

    for (int i = 0;i < words.size(); i++){
        std::cout << words[i] << std::endl;
    }


    for(int i=0; i < words.size(); i++) {
        insert(words[i]);
    }


    int choice;
    string z;
    string y;
    while(1) {

        cout << "Enter choice. 1) Insert\n 2) Search\n 3) Exit\n";
        cin >> choice;
        switch (choice) {
            case 1:
                cin>>y;
                insert(y);
                break;
            case 2:

                cin>>z;
                search(z);
                break;
            case 3:
                exit(0);
        }
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM