执行 C++ 代码后出错

Error after execution of c++ code

本文关键字:出错 代码 C++ 执行      更新时间:2023-10-16

执行后,我在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 << "Mtavarin";
cout << "1)    Sawyisi monacemebin";
cout << "2)    Yidvebin";
cout << "3)    Gayidvebin";
cout << "4)    Wignebin";
cout << "5)    Dzieban";
cout << "6)    Wignebis nuskhan";
cout << "7)    Gamosvlan";
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 archevanin";
}
clrscr();
}
out:
}
library *initial(void)
{
clrscr();
library *newl = NULL, *start = NULL, *end = newl;
char ch;
while (1)
{
cout << "nnSheiyvanet y an Yn";
cout << "Gsurt archeva:-";
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
newl = new library;
cout << "nnSheiyvanet 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 << "nnMonacemebi ar arisn";
cout << "nnMtavarin";
cout << "1)    Pirvel adgilze sheyvanan";
cout << "2)    Shuashi chamateban";
cout << "3)    Bolo poziciaze sheyvana n";
cout << "4)    Gamosvlan";
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 << "nnAirchiet pozicia:-";
cin >> pos;
while (cnt != NULL)
{
count++;
cnt = cnt->next;
}
if (pos<1 || pos>count + 1)
{
cout << "nnPozicia arasworian";
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 arasworian";
break;
}
out:
return(head);
}

void stock(library *start)
{
clrscr();
int count = 0;
while (start != NULL)
{
count++;
start = start->next;
}
cout << "nnntWignebis 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 << "nnArsebuli wignebin";
cout << "Girebuleba" << start->price;
return;
}
}
}
cout << "nnVer moidzebnan";
}

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

首先,清理你的代码(我明白了,你发布了HTML格式的代码。因此,"感谢"另存为HTML文件的挑战,在浏览器中打开,然后复制粘贴为纯文本)。

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

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

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

库 *初始(无效); 图书馆*购买(图书馆*); 无效库存(图书馆*); 无效搜索(库*); int main() {//...
  1. clrsrc()函数不再存在。相反,您必须使用此函数:我们如何清除程序集中的控制台?

  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,不如只做<< "nn"

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

#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 << "Mtavarin";
std::cout << "1)    Sawyisi monacemebin";
std::cout << "2)    Yidvebin";
std::cout << "3)    Gayidvebin";
std::cout << "4)    Wignebin";
std::cout << "5)    Dzieban";
std::cout << "6)    Wignebis nuskhan";
std::cout << "7)    Gamosvlan";
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 archevanin";
}
clrscr();
}
out:
return 0;
}
library *initial(void)
{
clrscr();
library *newl = NULL, *start = NULL, *end = newl;
char ch;
while (1)
{
std::cout << "nnSheiyvanet y an Yn";
std::cout << "Gsurt archeva:-";
std::cin >> ch;
if (ch == 'y' || ch == 'Y')
{
newl = new library;
std::cout << "nnSheiyvanet 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 << "nnMonacemebi ar arisn";
std::cout << "nnMtavarin";
std::cout << "1)    Pirvel adgilze sheyvanan";
std::cout << "2)    Shuashi chamateban";
std::cout << "3)    Bolo poziciaze sheyvana n";
std::cout << "4)    Gamosvlan";
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 << "nnAirchiet pozicia:-";
std::cin >> pos;
while (cnt != NULL)
{
count++;
cnt = cnt->next;
}
if (pos<1 || pos>count + 1)
{
std::cout << "nnPozicia arasworian";
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 arasworian";
break;
}
out:
return(head);
}
void stock(library *start)
{
clrscr();
int count = 0;
while (start != NULL)
{
count++;
start = start->next;
}
std::cout << "nnntWignebis 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 << "nnArsebuli wignebin";
std::cout << "Girebuleba" << start->price;
return;
}
}
}
std::cout << "nnVer moidzebnan";
}
void display(library *start)
{
clrscr();
std::cout << std::setw(10) << "Satauri" << std::setw(25) << "Avtori" << std::setw(25) << "Publikacia" << std::setw(20) << "Fasi" << "nn";
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;
}
}

我希望这对你有帮助。

C/C++ 是一种单通道语言。 您必须在使用函数之前定义它们。 即更高。解释了为什么无法识别显示:您需要将函数体放在使用它的位置上方,或者将空声明放入:

void display(library *start);

在首次使用之前。这告诉编译器"显示"是哪种对象。至于clrscr,这取决于您使用的库。检查库的参考资料。

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