Home>

A program that reads a text file in C ++ and applies a binary search to words stored in a vector array to search for the specified word.
After sorting in lexicographic order, I want to put a loop that repeats search word acceptance and search execution. The search is repeated until an end indication mark ($$) is received.
I modified the code as follows,
if (! inFile) {
cout<<"Cannot open input file"< return;
}
In the return part of the error, "'main': The function is declared to return a value, but there is no 'return' statement in the function definition" is displayed. How can I fix my code to clear the error?
Please let me know if there is anything else wrong.
Thank you.

# include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include 
using namespace std;
// Main function
int main () {
    ifstream inFile;// input file
    string word;// word
    vector<string>vector;// vector array
    // Open the input file
    inFile.open ("test.txt");
    // Exit if the input file cannot be opened
    if (! inFile) {
        cout<<"Cannot open input file"<<endl;
        return;
    }
    // read words from file
    inFile >>word;
    // If the file is not at the end, repeat the following process
    while (! inFile.eof ()) {
        // store the word
        vector.push_back (word);
        // read a new word
        inFile >>word;
    }
    // sort words stored in vector array in dictionary order
    sort (vector.begin (), vector.end ());
    string a;
    while ("$$" == a) {
        cout<<"search word?";
        cin >>a;
        binary_search (vector.begin (), vector.end (), a);
        if (true) {
            cout<<"exists"<<endl;
        }
        else {
            cout<<"does not exist"<<endl;
        }
    }
}
  • Answer # 1

    Is this all right?

    # include
    #include
    #include
    #include
    #include 
    // Main function
    int main () {
        using namespace std;
        string word;// word
        vectorwords;// word string
       // read words from file
       {
            ifstream inFile ("test.txt");// input file
            // Exit if the input file cannot be opened
            if (! inFile.is_open ()) {
                cout<<"Cannot open input file"<>word) {
                // store the word
                words.push_back (word);
            }
        }
        // sort words stored in vector array in dictionary order
        sort (words.begin (), words.end ());
        while (true) {
            cout<<"Search word?"<>word;
            if (word == "$$") {
                break;
            }
            if (binary_search (words.begin (), words.end (), word)) {
                cout<<"exists"<

  • Answer # 2

    Search (Linear Search/Binary Search/STL Use) Summary

    Understand and use the source.
    main () and return;have not been fixed either ...
    "Appendix" If you don't understand ifstream's eof, the loop won't turn correctly

  • Answer # 3

    There are some parts that seem to be wrong, but for the time being ...

      

    I want to include a loop that repeats search word acceptance and search execution. The search is repeated until an end indication mark ($$) is received.

    If you read this sentence,

    A loop is needed,

    It is necessary to write "search word input processing" and "search processing" inside the loop,

    Decision processing to exit the loop is required depending on the judgment result of whether the input is the end instruction mark

    Don't understand that ...?
    At least

    loop
    {
      ・ Enter search words
      ・ If (The input is the end instruction mark) {Exit the loop}
      ·Explore
    ・ ・ (I don't know what I want to do with the search result information, but just display it or do the necessary processing)
    }


    Let's write the code like this.

    For the current error, it means that it is contradictory to return a return value with return even though main () is written as a function with no return value.
    If you see only that error
    I'd like to revise it according to what you want to do (whether you want to return something) or not.
    The program itself will not move as expected when only that point is solved now,
    For now, why not give priority to modifying the code itself as described above?
    I think that the current error will disappear without permission in the process.

Related articles