Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program which reads elements from a FILE and then searches value ent

ID: 3764028 • Letter: W

Question

Write a C++ program which reads elements from a FILE and then searches value entered from the keyboard in the FILE. Use arrayListType Binary search template function.

I got this code:

#include <iostream>
#include <string>
#include<conio.h>
#include<fstream>
using namespace std;

template<class elemType>
class orderedArrayListType
{
public:
    static const int length = 20;//Const length of array you can change it accordingly
    int list[length];
    int binarySearch(elemType const&)const;
};

template<class elemType>
int orderedArrayListType<elemType>::binarySearch(const elemType& item) const
{
    int first = 0;
    int last = length - 1;
    int mid;

    bool found = false;

    while (first <= last && !found)
    {
        mid = (first + last) / 2;

        if (list[mid] == item)
            found = true;
        else if (list[mid] > item)
            last = mid - 1;
        else
            first = mid + 1;
    }

    if (found)
        return mid;
    else
        return -1;
}

void main()
{
    std::fstream numberFile("c:/temp/demo.txt", std::ios_base::in);

    orderedArrayListType<int> object;
    int number, a;
    int i = 0;
    int numberToSearch;
    while (numberFile >> a)
    {
        object.list[i] = number;//Initalizing the array
        i++;
    }

    cout << "Enter Number you want to search" << endl;
    cin >> numberToSearch;

    int output = object.binarySearch(numberToSearch);//Make search
    if (output>0)
    {
        cout << "Element found at Index: " << output << endl;
    }
    else
    {
        cout << "Element not Found" << endl;
    }

    getch();

}

***AND IT HAS THE FOLLOWING ERRORS:

documents isual studio 2015projectssearchsearchinarytemplate.cpp(70): error C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _getch. See online help for details.
1> c:program files (x86)windows kitsinclude.0.10150.0ucrtconio.h(452): note: see declaration of 'getch'

Explanation / Answer

This compiler warning indicates that there is a reference to the 'getch()' function in the code. The compiler is suggesting that the code be updated to use the '_getch()' function instead.
Generally, this error will occur in a Microsoft development environment. Way back, a long time ago, Microsoft introduced the 'getch()' [and 'getche()'] functions. These functions would wait for the user to press a key, and then retun that key character back to the caller. The 'getch()' function would not 'echo' the key back to the (DOS or early Windows) screen, while 'getche()' would echo the key pressed to the screen.
The 'getch()' and 'getche()' functions were not adopted by other environments, such as *nix. Eventually, Microsoft elected to rename the functions with a leading underscore to help identify their proprietary (Microsoft) nature.
By the look of the error message, I assume that the '_getch()' function has been adopted by the "ISO C++" standard. (However, I don't do C++ as it is an abomination to many C programmers such as myself).
Note: The *nix 'curses' library also sports a getch() which is somewhat similar to the old Microsoft function.
The compiler warning noted in the question is just that; a warning. Most likely, code will still compile and run (in a Microsoft environment).

As the error says, the name getch() is deprecated (as that was a Microsoft-specific extension), and _getch() is recommended instead. I'm not sure where POSIX comes into it; POSIX does not define getch(), but instead includes getchar() as specified by the ISO C standard (page 298, draft linked to because final standard is not free). The solution would be to do as suggested and use _getch(), or use the standard C getchar().

https://www.daniweb.com/programming/software-development/threads/343925/compiling-error-in-visual-c-

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote