0

I'm creating a code where...

  1. Numbers from a text file are being read into an array
  2. User can search for a number and the program tells you if it's in the text file.
  3. Numbers are sorted and output into another file
  4. I do a binary search from the sorted data files from 3

Issues I'm Facing...

  1. When I search for '0' is works. But if it's ANY other number it comes back not found.
  2. When outputting numbers in another file, a random huge number is added to the list. (i.e. 10 9 8 7 6 5 4 3 2 1 -8723641187 (-8723641187 is suppose to be 0))

I have 4 text files with 10 small digits each. 3 of them have 0's in different sections. One of them doesn't have any 0's.

Code (#define len 10 where suited):

function # 1

 void simpleSearch(std::string inputFile, int value)
    {
        bool found = false ;
        int num,
        count = 0 ;

        std::ifstream file(inputFile.c_str()) ;

        while(!file.eof())
        {
            file >> num ;
            count++ ;

            if (num == value)
            {
                std::cout << "Target Is Found\n" ;
                found = true ;
                break ;
            }
        }

        if(found == false)
        {
            std::cout << "Target Not Found\n" ;
        }    
}

function #2

    void sorting(std::string inputFile, std::string outputFile)
{
    int num,
        first,
        temp,
        i = 0,
        size = len + 1,
        contents[len + 1];

    std::ifstream file(inputFile.c_str()) ;

    while(!file.eof())
    {
        file >> num ;
        contents[i++] = num ; 
    }

    for (i = size - 1; i > 0; i--)
    {
        first = 0 ;
        for(int j = 1; j <= i; j++)
        {
            if(contents[j] < contents[first])
            {
                first = j ;
            }
        }

        temp = contents[first] ;
        contents[first] = contents[i] ;
        contents[i] = temp ;
    }

    std::ofstream outFile(outputFile.c_str()) ;

    for(i = 0; i < len + 1 ; i++)
    {
        outFile << contents[i] << " " ;
    }

    std::cout << "Sorted Values Stored In '" << outputFile << "'\n" ;

    return ; 
}

function #3

int binarySearch(std::string inputFile, int value)
{
    int middle,
        num,
        array[len],
        first = 0,
        size = len + 1,
        last = size - 1,
        position = - 1 ;

    bool found = false ;

    std::ifstream file(inputFile.c_str()) ;

    while(!file.eof())
    {
        file >> num ;
        size++ ;

         middle = (first + last) / 2 ;
         if(array[middle] == value)
         {
            found = true ;
            position = middle ;
         }

         else if (array[middle] > value)
         {
            last = middle - 1 ;
         }

         else
         {
            first = middle + 1 ;
         }


    }

    if(position == value)
    {
      std::cout << inputFile + ": Target Is Found\n" ;
      found = true ;
    }

    else if (position == -1)
    {
        std::cout << inputFile + ": Target Is Not Found\n" ; 
    }

    return position ; 
}

main

int main()
{
    int target,
        num = 0,
        count = 0;
    std::string outputFile ;

    std::ofstream file1("random.txt") ;
    for(int i = 0; i < len + 1; i++)
    {
        file1 << (i + 1) << " " ;
    }

    file1.close() ;

    std::ofstream file2("early.txt") ;
    std::ofstream file3("middle.txt") ;
    std::ofstream file4("end.txt") ;

    std::ifstream file("random.txt") ;

    file2 << num << " " ;

    while(!file.eof())
    {
        file >> num ;

        if (count == len / 2)
        {
            file3 << "0" << " " ;
        }

        file2 << num << " " ;
        file3 << num << " " ;
        file4 << num << " " ;

        count++ ;
    }

    file4 << "0" << " ";

    file.close() ;

    std::cout << "1. Reading Value From File\n" ;
    std::cout << "----------------------------\n" ;
    std::cout << " Testing the files have been read by testing if\n" ;
    std::cout << " the listed files have '0'\n\n" ;

    simpleSearch("random.txt", 0 ) ;
    simpleSearch("early.txt", 0 ) ;
    simpleSearch("middle.txt", 0) ;
    simpleSearch("end.txt", 0) ;

    std::cout << "\n2. Simple Search\n" ;
    std::cout << "----------------------------\n" ;
    std::cout << "Enter a Target Number to Find: \n" ;
    std::cin >> target ;

    simpleSearch("random.txt", target ) ;
    simpleSearch("early.txt", target) ;
    simpleSearch("middle.txt", target) ;
    simpleSearch("end.txt", target) ;

    std::cout << "\n3. Sorting\n" ;
    std::cout << "----------------------------\n" ;
    std::cout << "Enter a File Name to Store Your Numbers: \n" ;
    std::cin >> outputFile ;
    sorting("random.txt", outputFile) ;

    std::cout << "\nEnter a File Name to Store Your Numbers: \n" ;
    std::cin >> outputFile ;
    sorting("early.txt", outputFile) ;

    std::cout << "\nEnter a File Name to Store Your Numbers: \n" ;
    std::cin >> outputFile ;
    sorting("middle.txt", outputFile) ;

    std::cout << "\nEnter a File Name to Store Your Numbers: \n" ;
    std::cin >> outputFile ;
    sorting("end.txt", outputFile) ;

    std::cout << "\n4. Binary Search\n" ;
    std::cout << "----------------------------\n" ;
    std::cout << " Testing the files with binary search\n" ;

    std::cout << "Enter a Target Number to Find: \n" ;
    std::cin >> target ;

    binarySearch("random.txt", target) ;
    binarySearch("early.txt", target) ;
    binarySearch("middle.txt", target) ;
    binarySearch("end.txt", target) ;

    return 0 ;
}

Feel like I'm so close...then anything I've been wrong before. Thank you.

  • 2
    One bug is see is using this:`while(!file.eof())` the reason is here: [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Nov 24 '19 at 15:29
  • What @drescherjm said. Replace `while(!file.eof()) { file >> num; ...` with `while(file >> num) { ...` – Ted Lyngmo Nov 24 '19 at 15:35
  • Thank guys, that fixed the additional number issue! Also, thank you for the link! I'm now going through to figure out why my searches aren't picking up numbers. :) – machill310 Nov 24 '19 at 15:47
  • ***I'm now going through to figure out why my searches aren't picking up numbers.*** Your debugger should help with that. Step through your algorithm 1 line at a time looking at the variables and the flow after each line is executed on an example data that fails to work. – drescherjm Nov 24 '19 at 15:48
  • In your binary search, how is `array` being populated? Why are you trying to search while reading values from a file? – drescherjm Nov 24 '19 at 15:57

0 Answers0