【问题标题】:Error after execution of c++ code执行c++代码后出错
【发布时间】:2017-03-13 09:31:43
【问题描述】:

执行后,我在 Visual C++ 2015 中遇到以下错误:

  1. while (c< pos - 1)

    错误:标识符 c 未定义

  2. clrscr();

    错误:标识符 clrscr(); 未定义

  3. case 6: display(head);

    错误:标识符 显示 未定义

这是我的代码:

#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;
    }
}

【问题讨论】:

  • c/c++ 是一种单程语言。您必须在使用之前定义函数。即更高。
  • 请不要使用goto:S

标签: c++


【解决方案1】:

首先,清理您的代码(我明白了,您发布了 HTML 格式的代码。所以,“感谢”您挑战保存为 HTML 文件,在浏览器中打开,然后复制粘贴作为纯文本)。

现在,我可以告诉你出了什么问题:

  1. void main() 替换为int main()void main() 是旧版,不再正确。此外,如果您想告诉您的程序以错误结束(例如,缺少参数或不存在文件),您必须在结尾处 return 0; 或返回任何其他数字。

  2. 不要将函数原型放在 main() 中,将它们放在 main 之上! 我的意思是:

库 *initial(void); 图书馆*购买(图书馆*); 无效库存(库 *); 无效搜索(库 *); 主函数() { //...
  1. clrsrc() 函数已不存在。相反,您必须使用此功能:How do we clear the console in assembly?

  2. 缺少display() 函数是因为您忘记将原型放在main() 函数上:void display(library *start);

  3. while(c &lt; pos - 1) 中缺少 c:只需将 int c = 0; 放在 while 循环上即可。

  4. 我已被删除 using namespace std; 并将 std:: 添加到每个需要它的功能中。

  5. 提示:您可以使用&lt;&lt; "\n\n",而不是&lt;&lt; std::endl &lt;&lt; std::endl

因此,完整且固定的代码将是:

#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;
    }
}

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    c/c++ 是一种单遍语言。您必须在使用之前定义函数。即更高。解释了为什么无法识别显示:您需要在上面使用函数体或在其中放置一个空声明:

    void display(library *start);
    

    在首次使用之前。这告诉编译器“显示”是什么类型的对象。至于 clrscr,这取决于您使用的库。检查您的图书馆的参考资料。

    至于c,你从来没有创建一个名为“c”的变量,所以编译器不知道它应该是什么。我们也没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多