-1

After execution, I am getting following errors in Visual C++ 2015:

  1. while (c< pos - 1)

    error: identifier c undefined

  2. clrscr();

    error: identifier clrscr(); is undefined

  3. case 6: display(head);

    error: identifier display is undefined

Here is my code:

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

using namespace std;
struct library
{
    char author[20], title[20], pub[20];
    int price;
    library *next;
};


int sum = 0;

void main()
{
    clrscr();
    library *head = NULL;
    library *initial(void);
    library *purchase(library *);
    void stock(library *);
    void search(library *);

    int choice;
    while (1)
    {
        cout << "Mtavari\n";
        cout << "1)    Sawyisi monacemebi\n";
        cout << "2)    Yidvebi\n";
        cout << "3)    Gayidvebi\n";
        cout << "4)    Wignebi\n";
        cout << "5)    Dzieba\n";
        cout << "6)    Wignebis nuskha\n";
        cout << "7)    Gamosvla\n";
        cout << "Airchiet:-";
        cin >> choice;
        switch (choice)
        {
        case 1: head = initial();
            getch();
            break;
        case 2: head = purchase(head);
            getch();
            break;
            getch();
            break;
        case 5: search(head);
            getch();
            break;
        case 6: display(head);
            getch();
            break;
        case 7: goto out;
        default: cout << "\nShecdomiti archevani\n";
        }
        clrscr();
    }
out:
}

library *initial(void)
{
    clrscr();
    library *newl = NULL, *start = NULL, *end = newl;
    char ch;
    while (1)
    {
        cout << "\n\nSheiyvanet y an Y\n";
        cout << "Gsurt archeva:-";
        cin >> ch;
        if (ch == 'y' || ch == 'Y')
        {
            newl = new library;
            cout << "\n\nSheiyvanet wignis avtori:-";
            cin >> newl->author;
            cout << "Sheiyvanet satauri:-";
            cin >> newl->title;
            cout << "Sheiyvanet gamomcemloba:-";
            cin >> newl->pub;
            cout << "Sheiyvanet fasi:-";
            cin >> newl->price;
            sum = sum + newl->price;
            if (start == NULL)
                start = newl;
            else
                end->next = newl;
            end = newl;
            end->next = NULL;
        }
        else break;
    }
    return(start);
}

library *purchase(library *start)
{
    clrscr();
    int pos, count = 1, choice;
    library *newl, *cnt = start, *head = start;
    if (start == NULL)
        cout << "\n\nMonacemebi ar aris\n";

    cout << "\n\nMtavari\n";
    cout << "1)    Pirvel adgilze sheyvana\n";
    cout << "2)    Shuashi chamateba\n";
    cout << "3)    Bolo poziciaze sheyvana \n";
    cout << "4)    Gamosvla\n";
    cout << "Airchiet:-";
    cin >> choice;

    if (choice >= 1 && choice <= 3)
    {
        newl = new library;
        cout << "Avtoris saxeli :-";
        cin >> newl->author;
        cout << "Wignis satauri :-";
        cin >> newl->title;
        cout << "Gamomcemloba :-";
        cin >> newl->pub;
        cout << "Fasi:-";
        cin >> newl->price;
        sum = sum + newl->price;
    }

    switch (choice)
    {
    case 1:
        newl->next = head;
        head = newl;
        break;

    case 2:
    read:
        cout << "\n\nAirchiet pozicia:-";
        cin >> pos;
        while (cnt != NULL)
        {
            count++;
            cnt = cnt->next;
        }
        if (pos<1 || pos>count + 1)
        {
            cout << "\n\nPozicia arasworia\n";
            goto read;
        }

        {
            while (c<pos - 1)
            {
                c++;
                start = start->next;
            }
        }
        newl->next = start->next;
        start->next = newl;
        break;

    case 3:
        start = start->next;

        start->next = newl;
        newl->next = NULL;
        break;

    case 4:         goto out;

    default:       cout << "\nArchevani arasworia\n";
        break;

    }
out:
    return(head);
}


void stock(library *start)
{
    clrscr();
    int count = 0;
    while (start != NULL)
    {
        count++;
        start = start->next;
    }
    cout << "\n\n\n\tWignebis raodenoba  " << count << endl;
    cout << "\tMtliani fasi  " << sum;
}


void search(library *start)
{
    clrscr();
    char author[20], title[20];
    cout << "Sheiyvanet sadziebo fraza(avtori an wigni...)\n";
    cin >> title >> author;
    while (start != NULL)
    {
        if (title == start->title)
        {
            if (author == start->author)
            {
                cout << "\n\nArsebuli wignebi\n";
                cout << "Girebuleba" << start->price;
                return;
            }
        }
    }
    cout << "\n\nVer moidzebna\n";
}


void display(library *start)
{
    clrscr();
    cout << setw(10) << "Satauri" << setw(25) << "Avtori" << setw(25) << "Publikacia" << setw(20) << "Fasi" << endl << endl;
    for (int i = 0;i<40;i++)
        cout << "=*";
    cout << endl;
    while (start != NULL)
    {
        cout << setw(10) << start->title << setw(25) << start->author << setw(25) << start->pub << setw(20) << start->price << endl;
        start = start->next;
    }
}
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
f0xtr0d
  • 37
  • 7

2 Answers2

2

At first, clean-up your code (I see, you posted HTML-formated code. So, "thanks" for a challenge to save as HTML-file, open in a browser, then copy-paste away as plain text).

For now, I'm able to tell you what's wrong:

  1. replace void main() with int main(), void main() is legacy and no more correct. Also, you must return 0; at end or return any other number if you wish to tell that your program finished with errors (for example, missing argument or not existing file).

  2. Don't put function prototypes inside main(), put them over main! I mean:


    library *initial(void);
    library *purchase(library *);
    void stock(library *);
    void search(library *);
    int main()
    {
    //...

  1. clrsrc() function is no more exist. Instead, you have to use this function: How do we clear the console in assembly?

  2. missing display() function is caused you forgot to put prototype to it over main() function: void display(library *start);

  3. missing c in while(c < pos - 1): just put int c = 0; over that while loop.

  4. I have been removed using namespace std; and added the std:: to every function requires it.

  5. Tip: Instead of having << std::endl << std::endl you can just do << "\n\n"

So, the full and fixed code will be:

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#ifdef _WIN32
// for Windows
#include <conio.h>
#include <windows.h>
#else
// for Linux (required to install the ncurses-dev and link with -lncurses!)
#include <ncurses.h>
#endif

void clrscr()
{
#ifdef _WIN32
    // for Windows
    char  fill = ' ';
    COORD tl = {0,0};
    CONSOLE_SCREEN_BUFFER_INFO s;
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(console, &s);
    DWORD written, cells = s.dwSize.X * s.dwSize.Y;
    FillConsoleOutputCharacter(console, fill, cells, tl, &written);
    FillConsoleOutputAttribute(console, s.wAttributes, cells, tl, &written);
    SetConsoleCursorPosition(console, tl);
#else
    // for Linux
    system("clear");
#endif
}

struct library
{
    char author[20], title[20], pub[20];
    int price;
    library *next;
};

library *initial(void);
library *purchase(library *);
void stock(library *);
void search(library *);
void display(library *start);

int sum = 0;
int main()
{
    clrscr();
    library *head = NULL;
    int choice;
    while (1)
    {
        std::cout << "Mtavari\n";
        std::cout << "1)    Sawyisi monacemebi\n";
        std::cout << "2)    Yidvebi\n";
        std::cout << "3)    Gayidvebi\n";
        std::cout << "4)    Wignebi\n";
        std::cout << "5)    Dzieba\n";
        std::cout << "6)    Wignebis nuskha\n";
        std::cout << "7)    Gamosvla\n";
        std::cout << "Airchiet:-";
        std::cin >> choice;
        switch (choice)
        {
        case 1: head = initial();
            getch();
            break;
        case 2: head = purchase(head);
            getch();
            break;
            getch();
            break;
        case 5: search(head);
            getch();
            break;
        case 6: display(head);
            getch();
            break;
        case 7:
            goto out;
        default: std::cout << "\nShecdomiti archevani\n";
        }
        clrscr();
    }
out:
    return 0;
}

library *initial(void)
{
    clrscr();
    library *newl = NULL, *start = NULL, *end = newl;
    char ch;
    while (1)
    {
        std::cout << "\n\nSheiyvanet y an Y\n";
        std::cout << "Gsurt archeva:-";
        std::cin >> ch;
        if (ch == 'y' || ch == 'Y')
        {
            newl = new library;
            std::cout << "\n\nSheiyvanet wignis avtori:-";
            std::cin >> newl->author;
            std::cout << "Sheiyvanet satauri:-";
            std::cin >> newl->title;
            std::cout << "Sheiyvanet gamomcemloba:-";
            std::cin >> newl->pub;
            std::cout << "Sheiyvanet fasi:-";
            std::cin >> newl->price;
            sum = sum + newl->price;
            if (start == NULL)
                start = newl;
            else
                end->next = newl;
            end = newl;
            end->next = NULL;
        }
        else break;
    }
    return(start);
}
library *purchase(library *start)
{
    clrscr();
    int pos, count = 1, choice;
    library *newl, *cnt = start, *head = start;
    if (start == NULL)
        std::cout << "\n\nMonacemebi ar aris\n";
    std::cout << "\n\nMtavari\n";
    std::cout << "1)    Pirvel adgilze sheyvana\n";
    std::cout << "2)    Shuashi chamateba\n";
    std::cout << "3)    Bolo poziciaze sheyvana \n";
    std::cout << "4)    Gamosvla\n";
    std::cout << "Airchiet:-";
    std::cin >> choice;
    if (choice >= 1 && choice <= 3)
    {
        newl = new library;
        std::cout << "Avtoris saxeli :-";
        std::cin >> newl->author;
        std::cout << "Wignis satauri :-";
        std::cin >> newl->title;
        std::cout << "Gamomcemloba :-";
        std::cin >> newl->pub;
        std::cout << "Fasi:-";
        std::cin >> newl->price;
        sum = sum + newl->price;
    }
    switch (choice)
    {
    case 1:
        newl->next = head;
        head = newl;
        break;
    case 2:
    read:
        std::cout << "\n\nAirchiet pozicia:-";
        std::cin >> pos;
        while (cnt != NULL)
        {
            count++;
            cnt = cnt->next;
        }
        if (pos<1 || pos>count + 1)
        {
            std::cout << "\n\nPozicia arasworia\n";
            goto read;
        }
        {
            int c = 0;
            while(c < pos - 1)
            {
                c++;
                start = start->next;
            }
        }
        newl->next = start->next;
        start->next = newl;
        break;
    case 3:
        start = start->next;
        start->next = newl;
        newl->next = NULL;
        break;
    case 4:
        goto out;
    default:
        std::cout << "\nArchevani arasworia\n";
        break;
    }
out:
    return(head);
}

void stock(library *start)
{
    clrscr();
    int count = 0;
    while (start != NULL)
    {
        count++;
        start = start->next;
    }
    std::cout << "\n\n\n\tWignebis raodenoba  " << count << std::endl;
    std::cout << "\tMtliani fasi  " << sum;
}
void search(library *start)
{
    clrscr();
    char author[20], title[20];
    std::cout << "Sheiyvanet sadziebo fraza(avtori an wigni...)\n";
    std::cin >> title >> author;
    while (start != NULL)
    {
        if (title == start->title)
        {
            if (author == start->author)
            {
                std::cout << "\n\nArsebuli wignebi\n";
                std::cout << "Girebuleba" << start->price;
                return;
            }
        }
    }
    std::cout << "\n\nVer moidzebna\n";
}

void display(library *start)
{
    clrscr();
    std::cout << std::setw(10) << "Satauri" << std::setw(25) << "Avtori" << std::setw(25) << "Publikacia" << std::setw(20) << "Fasi" << "\n\n";
    for (int i = 0;i<40;i++)
        std::cout << "=*";
    std::cout << std::endl;
    while (start != NULL)
    {
        std::cout << std::setw(10) << start->title << std::setw(25) << start->author << std::setw(25) << start->pub << std::setw(20) << start->price << std::endl;
        start = start->next;
    }
}

I hope this will help you.

Community
  • 1
  • 1
Wohlstand
  • 161
  • 1
  • 9
1

c/c++ is a single-pass language. you have to define functions before they're used. i.e. higher up. The explains why display isn't recognized: you need to either have the function body above where it's used or put an empty declaration in:

void display(library *start);

before it's first used. this tells the compiler what sort of object "display" is. As for clrscr, that depends on the library you're using. check the references for your library.

As for c, you never made a variable called "c", so the compiler doesn't know what that's supposed to be. Neither do we.

Jason Lang
  • 1,079
  • 9
  • 17