0

right now I am making a hands on project on bookshop inventory by using C++ program in Visual Studio Code.

PROBLEM STATEMENT OF THE PROJECT: A book shop maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether it is available or not. If it is not, an appropriate message is displayed. If it is, then the system displays the book details and requests for the number of copies required.

#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

 class book
  {
    private:
    char *author, *title, *publisher;
    float *price;
    int *stock;

  public:
    book()
   {
    author = new char[20];
    title = new char[20];
    publisher = new char[20];
    price = new float;
    stock = new int;
   }
    void feeddata();
    void editdata();
    void showdata();
    int search(char[], char[]);
    void buybook();
    };

    void book::feeddata()
  {
    cin.ignore();
    cout << "\nEnter Author Name: ";
    cin.getline(author, 20);
    cout << "Enter Title Name: ";
    cin.getline(title, 20);
    cout << "Enter Publisher Name: ";
    cin.getline(publisher, 20);
    cout << "Enter Price: ";
    cin >> *price;
    cout << "Enter Stock Position: ";
    cin >> *stock;
  }

    void book::editdata()
  {

   cout << "\nEnter Author Name: ";
   cin.getline(author, 20);
   cout << "Enter Title Name: ";
   cin.getline(title, 20);
   cout << "Enter Publisher Name: ";
   cin.getline(publisher, 20);
   cout << "Enter Price: ";
   cin >> *price;
   cout << "Enter Stock Position: ";
   cin >> *stock;
  }

   void book::showdata()
  {
   cout << "\nAuthor Name: " << author;
   cout << "\nTitle Name: " << title;
   cout << "\nPublisher Name: " << publisher;
   cout << "\nPrice: " << *price;
   cout << "\nStock Position: " << *stock;
  }

    int book::search(char tbuy[20], char abuy[20])
  {
   if (strcmp(tbuy, title) == 0 && strcmp(abuy, author) == 0)
      return 1;
   else
      return 0;
  }

    void book::buybook()
  {
    int count;
    cout << "\nEnter Number Of Books to buy: ";
    cin >> count;
    if (count <= *stock)
  {
    *stock = *stock - count;
    cout << "\nBooks Bought Sucessfully";
    cout << "\nAmount: Rs. " << (*price) * count;
  }
  else
    cout << "\nRequired Copies not in Stock";
  }

 int main()
    {
      book *B[20];
      int i = 0, r, t, choice;
      char titlebuy[20], authorbuy[20];
      while (1)
      {
         cout << "\n\n\t\tMENU"
              << "\n1. Entry of New Book"
              << "\n2. Buy Book"
              << "\n3. Search For Book"
              << "\n4. Edit Details Of Book"
              << "\n5. Exit"
              << "\n\nEnter your Choice: ";
         cin >> choice;

    switch (choice)
    {
    case 1:
        B[i] = new book;
        B[i]->feeddata();
        i++;
        break;

    case 2:
        cin.ignore();
        cout << "\nEnter Title Of Book: ";
        cin.getline(titlebuy, 20);
        cout << "Enter Author Of Book: ";
        cin.getline(authorbuy, 20);
        for (t = 0; t < i; t++)
        {
            if (B[t]->search(titlebuy, authorbuy))
            {
                B[t]->buybook();
                break;
            }
        }
        if (t == 1)
            cout << "\nThis Book is Not in Stock";

        break;
    case 3:
        cin.ignore();
        cout << "\nEnter Title Of Book: ";
        cin.getline(titlebuy, 20);
        cout << "Enter Author Of Book: ";
        cin.getline(authorbuy, 20);

        for (t = 0; t < i; t++)
        {
            if (B[t]->search(titlebuy, authorbuy))
            {
                cout << "\nBook Found Successfully";
                B[t]->showdata();
                break;
            }
        }
        if (t == i)
            cout << "\nThis Book is Not in Stock";
        break;

    case 4:
        cin.ignore();
        cout << "\nEnter Title Of Book: ";
        cin.getline(titlebuy, 20);
        cout << "Enter Author Of Book: ";
        cin.getline(authorbuy, 20);

        for (t = 0; t < i; t++)
        {
            if (B[t]->search(titlebuy, authorbuy))
            {
                cout << "\nBook Found Successfully";
                B[t]->editdata();
                break;
            }
        }
        if (t == i)
            cout << "\nThis Book is Not in Stock";
        break;

    case 5:
        exit(0);
    default:
        cout << "\nInvalid Choice Entered";
    }
}

return 0;

}

It is a really important project to me...

In this project when When I am running this code following lines are coming on the terminal..

PS D:\C_C++ PROJECTS> cd "d:\C_C++ PROJECTS\" ; if ($?) { g++ b_i.cpp -o b_i } ; if ($?) { .\b_i }
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x39): 
undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

If anyone know how to fix it, it is helpful to me....

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
AVI RAJ
  • 55
  • 1
  • 11
  • 1
    Do you have multiple cpp files? I suspect you are getting this error because VSCode only builds the active file by default. I expect your active file does not have int main() and you did not modify your tasks.json to have VSCode use all of your cpp files. – drescherjm Aug 27 '21 at 16:05
  • 1
    Your statement of the problem probably can be deleted. Your actual code problem is very common and there are many duplicates. – drescherjm Aug 27 '21 at 16:06
  • Your code compiles fine. You only need to configure your development environment properly. You may check for instance this: https://code.visualstudio.com/docs/cpp/config-mingw or search for other locations. – Palo Aug 27 '21 at 16:10
  • @drescherjm No. it is a only one file. in my current file I also have a int main() function. – AVI RAJ Aug 27 '21 at 16:20
  • I asked because when compiling with mingw I believe if it does not find an int main() it attempts to find WInMain and if that is not found you get a linker error. You will find a lot of duplicates for `undefined reference to WinMain@16` – drescherjm Aug 27 '21 at 16:23
  • @https://stackoverflow.com/users/2587326/palo I have already configured development environment properly , and it's good. it is not a undefined reference I have taken it from https://code.visualstudio.com/docs/cpp/config-mingw. ..and it's original one. – AVI RAJ Aug 27 '21 at 16:24
  • Can you add the text of your `tasks.json`? – drescherjm Aug 27 '21 at 16:24
  • WinMain@16? Are you compiling a windows app instead of a console app? Just a remark, but consider using std::string instead of managing all the memory yourself (author = new char[20]). And strcmp function just become '==' operations. It's also more c++ then 'c' that way. – Pepijn Kramer Aug 27 '21 at 16:27
  • Possibly a duplicate of : https://stackoverflow.com/questions/6626397/error-lnk2019-unresolved-external-symbol-winmain16-referenced-in-function – Pepijn Kramer Aug 27 '21 at 16:28
  • @https://stackoverflow.com/users/487892/drescherjm I will add the text of tasks.json.. and let you know about it..it's working or not... – AVI RAJ Aug 27 '21 at 16:29
  • @ https://stackoverflow.com/users/16649550/pepijn-kramer it's not the duplicate one sir. – AVI RAJ Aug 27 '21 at 16:30
  • I would say this is certainly related to the many `WinMain@16` duplicates. – drescherjm Aug 27 '21 at 16:35
  • @https://stackoverflow.com/users/487892/drescherjm ok sir. is there any other problem in my code that the compiler is not added to the PATH IN A COMPUTER ENVIRONMENT , like this..is it possible to happen such things..due to that my code is not running. – AVI RAJ Aug 27 '21 at 16:38
  • You can try adding a `-mconsole` argument to gcc in your `tasks.json` and remove a `-mwindows` if it exists. – drescherjm Aug 27 '21 at 16:42
  • Also before we go on make sure that your `b_i.cpp` file in the "d:\C_C++ PROJECTS\" folder contains the code that you expect. Use some other editor to look at that file. You would get this same error if this file is empty. – drescherjm Aug 27 '21 at 16:44
  • 2
    Unrelated to your compilation issues, do **not** use `char *` and `new` in your project. Use `std::string` and `std::vector` instead. – n. m. could be an AI Aug 27 '21 at 17:30
  • 1
    Questions on Stack Overflow should present a [mre] instead of full programs. Have you tried to reduce the size of your code? What happens if you reduce your code down to `int main() {}`? If you get the same error with this trivial program, there is no need for all the distracting, extra fluff. – JaMiT Aug 27 '21 at 19:03
  • Does this answer your question? [undefined reference to \`WinMain@16' error](https://stackoverflow.com/questions/61065807/undefined-reference-to-winmain16-error). If not, you'll need to provide some details that will distinguish your question from that one. – JaMiT Aug 27 '21 at 19:16
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Aug 28 '21 at 11:05

0 Answers0