执行c ++代码后出错

时间:2017-03-13 09:31:44

标签: c++

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

  1. while (c< pos - 1)

    错误:标识符c未定义

  2. clrscr();

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

  3. case 6: display(head);

    错误:标识符 显示 未定义

  4. 这是我的代码:

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

2 个答案:

答案 0 :(得分:2)

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

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

  1. void main()替换为int main()void main()是遗产,不再正确。此外,如果您希望告知程序已完成错误(例如,缺少参数或不存在现有文件),则必须return 0;结束或返回任何其他编号。

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

  3. 
        library *initial(void);
        library *purchase(library *);
        void stock(library *);
        void search(library *);
        int main()
        {
        //...
    
    
    1. clrsrc()功能不再存在。相反,您必须使用此功能:How do we clear the console in assembly?

    2. 缺少display()功能导致您忘记通过main()函数将原型放入其中:void display(library *start);

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

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

    5. 提示:您可以<< std::endl << std::endl

    6. 而不是<< "\n\n"

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

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

      我希望这会对你有所帮助。

答案 1 :(得分:1)

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

void display(library *start);

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

对于c,你从未创建一个名为&#34; c&#34;的变量,因此编译器不知道它应该是什么。我们也不是。