0

I'm writing a book program that enables the user to Add and Find books to a library. Now, I need to figure out how to receive keyboard input from the user using conio.h/any other alternative. So that if the user presses '1', the AddMenu() gets called, but if he/she presses 2, FindMenu() gets called. Here is the code:

#include <iostream>
#include <conio.h>

using namespace std;


class Book {
public:
    string title; //title of da book
    double sepnum; //unique identification code (ex. 1111, 0009, etc.)
};

void AddMenu() {

    Book book1;

    cout << "********ADD MENU********" << endl;
    cout << endl;
    cout << endl;
    cout << "Enter Book Name:" << endl;
    cin >> book1.title;
    cout << "Enter Book Callsign (Ex: 0001, 5672):" << endl;
    cin >> book1.sepnum;
    cout << "Succesfully added" << endl;
    cout << endl;
    cout << "************************" << endl;

}

void FindMenu() {

}

int main()
{
    cout << "**********MENU**********" << endl;
    cout << endl;
    cout << endl;
    cout << "1. Add Book" << endl;
    cout << "2. Find Book" << endl;
    cout << endl;
    cout << "************************" << endl;

    return 0;
}
  • Maybe you want [`_getch`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=msvc-160)? – mediocrevegetable1 Aug 11 '21 at 13:30
  • 2
    I would skip the `conio.h` and just use standard `c++`. Meaning use `std::cin >> someCharVar;` and have the user press enter. – drescherjm Aug 11 '21 at 13:54
  • 1
    Looks like you don't need `conio.h`. Try removing it. – Thomas Matthews Aug 11 '21 at 14:53
  • SomeCharVar would work, but I have two parameters (i.e. Two Functions) and a specific char needs to be entered for each function. So I considered conio.h – user16460338 Aug 11 '21 at 15:03
  • Get the `char` then use a switch / case there are many examples of what you are trying to do on this site. – drescherjm Aug 11 '21 at 15:04
  • Unrelated: don't use `double` for unique identifiers. Trying to compare `double`s for exact equality will only cause trouble. Use `int` or `long int` if you expect integers, or use `string` if you want to preserve the entire input as-is with decimals, leading zeroes, extra letters and so on. – alter_igel Aug 11 '21 at 15:40
  • According to your description, I wonder if you want to call different functions based on user input? If so, I suggest you could try to use [unordered_map Class](https://learn.microsoft.com/en-us/cpp/standard-library/unordered-map-class?view=msvc-160) – Jeaninez - MSFT Aug 12 '21 at 07:10

0 Answers0