最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

【C++】购书系统问题测试&功能补充

来源:博客园

代码来源:舍友大一的C++作业【代码存在的问题】


(资料图片)

在菜单界面选择对应序号时,若输入值非数字,而是字母等其它符号,会导致程序陷入循环,无法正常进入功能的下一步

原代码展示:

int main() {    system("color 8E");    int k,i,choose,orderflag=1;    double t_sum;    book* head = new book;    trolley* head2 = new trolley;    layfolk* l_head = new layfolk;    member* m_head = new member;    honoured_guest* h_head = new honoured_guest;    historyorder* o_head = new historyorder;        head->Loaddata(head);    l_head->loaddata(l_head);    m_head->loaddata(m_head);    h_head->loaddata(h_head);     string ch;    while (1)    {        system("color 8E");        system("cls");        printf("\n\n\n");        printf("\t\t\t\t+-----------TouhouBookStore-----------+\n");        printf("\t\t\t\t|--------------东方铃奈庵-------------|\n");        printf("\t\t\t\t+-------------网上购书系统------------+\n");        printf("\t\t\t\t+-------------------------------------+\n");        printf("\t\t\t\t|\t1 查看历史订单\t\t      |\n");        printf("\t\t\t\t|\t2 查看个人会员信息\t      |\n");        printf("\t\t\t\t|\t3 查看店内书籍\t\t      |\n");        printf("\t\t\t\t|\t4 搜索店内书籍\t\t      |\n");        printf("\t\t\t\t|\t5 购买书籍\t\t      |\n");        printf("\t\t\t\t|\t6 结算\t\t\t      |\n");        printf("\t\t\t\t|\t7 退出\t\t\t      |\n");        printf("\t\t\t\t+-------------------------------------+\n");        printf("\n\n\n");        printf("\t\t\t\t请输入你想实现的功能前的代号:\n");        printf("\t\t\t\t");        cin >> k;switch(k)        {    case 1:             {               cout<<"您的身份是:"<>choose;                switch(choose)              {                case 1:                    {                        o_head->m_display(head2,m_head);break;                    }                            case 2:                    {                        o_head->h_display(head2,h_head);break;                    }                            case 3:                {                        o_head->l_display(head2,l_head);break;                }                                 default:break;               }                                             break;             }                case 2:                {                    system("cls");                    cout<<"\n请选择您的会员身份:\n"<>k;             while(!k)            {            cin.clear();            cin.sync();            k=-1;            break;            }                        system("cls");            if(k==1)             { m_head->find(m_head);system("pause"); }             else if(k==2)            { h_head->find(h_head);system("pause"); }             else if(k==3)            { l_head->find(l_head);system("pause"); }             else            {            cout<<"请输入正确的序号!"<display(head);system("pause");break;                case 4:            {            cout<<"请选择搜索方式:"<<"\n\n";            cout<<"1.按照书名搜索\t2.按照作者名字搜索\t3.按关键字搜索"<<"\n\n";            cout<<"请输入你想要选择的方式序号:";            cin>>k;            while(!k)            {            cin.clear();            cin.sync();            k=-1;            break;            }            system("cls");            if(k==1)             { head->find_name(head);system("pause"); }             else if(k==2)            { head->find_author(head);system("pause"); }             else if(k==3)            { head->find_critical(head);system("pause"); }             else            {            cout<<"请输入正确的序号!"<Addbook(head2,head);system("pause");break;                case 6:            {              t_sum=head2->count(head2);              if(t_sum==-1)              {                  system("pause");                  break;              }            cout<<"您的身份是:"<>choose;                switch(choose)              {                case 1:                    {                   orderflag=o_head->m_storage(head2,m_head,t_sum);                if(orderflag!=-1)                {                 cout<<"请进行下一步支付步骤"<<"\n\n";                 system("pause");                head2->pay();                head2->remake(head2);                }                 break;                 }                             case 2:                    {                   orderflag=o_head->h_storage(head2,h_head,t_sum);                if(orderflag!=-1)                {                 cout<<"请进行下一步支付步骤"<<"\n\n";                 system("pause");                head2->pay();                head2->remake(head2);                }                 break;                 }                             case 3:                {                   orderflag=o_head->l_storage(head2,l_head,t_sum);                if(orderflag!=-1)                {                 cout<<"请进行下一步支付步骤"<<"\n\n";                 system("pause");                head2->pay();                head2->remake(head2);                }                 break;                 }                 default:break;               }                        break;                        }                case 7:        head->Savedata(head);return 0;                       case 8:        head->Addbook(head);break;                case 9:            {             system("cls");            cout<<"\n请选择要进行添加的顾客信息:\n\n";            cout<<"1.添加新会员\t2.添加新贵宾\t3.添加普通顾客信息\n\n";            cout<<"请输入对应序号:";            cin>>k;                         while(!k)            {            cin.clear();            cin.sync();            k=-1;            break;            }                        system("cls");                        if(k==1)             { m_head->addm(m_head);m_head->savedata(m_head);system("pause"); }                         else if(k==2)            { h_head->addh(h_head);h_head->savedata(h_head);system("pause"); }                         else if(k==3)            { l_head->addl(l_head);l_head->savedata(l_head);system("pause"); }                         else            {cout<<"请输入正确的序号!"<

【问题产生的原因】

由于接收数值的变量k为int类型,在接收字母或其它字符等char类型时,导致程序出现问题

【解决方案】

应用cin.clear()更改cin的异常状态标识,再用cin.sync()函数对缓冲区中的数据流进行清理,然后给变量k赋值为-1,进入switch选择后将跳转至default一列,随后结束此次循环。后续代码运行将恢复正常。

//将这几行代码嵌入至给变量k赋值的 cin>>k; 之后即可
while(!k)        {            cin.clear();            cin.sync();            k=-1;            break;        }

【修改后的代码】

int main() {    system("color 8E");    int k,i,choose,orderflag=1;    double t_sum;    book* head = new book;    trolley* head2 = new trolley;    layfolk* l_head = new layfolk;    member* m_head = new member;    honoured_guest* h_head = new honoured_guest;    historyorder* o_head = new historyorder;        head->Loaddata(head);    l_head->loaddata(l_head);    m_head->loaddata(m_head);    h_head->loaddata(h_head);     string ch;    while (1)    {        system("color 8E");        system("cls");        printf("\n\n\n");        printf("\t\t\t\t+-----------TouhouBookStore-----------+\n");        printf("\t\t\t\t|--------------东方铃奈庵-------------|\n");        printf("\t\t\t\t+-------------网上购书系统------------+\n");        printf("\t\t\t\t+-------------------------------------+\n");        printf("\t\t\t\t|\t1 查看历史订单\t\t      |\n");        printf("\t\t\t\t|\t2 查看个人会员信息\t      |\n");        printf("\t\t\t\t|\t3 查看店内书籍\t\t      |\n");        printf("\t\t\t\t|\t4 搜索店内书籍\t\t      |\n");        printf("\t\t\t\t|\t5 购买书籍\t\t      |\n");        printf("\t\t\t\t|\t6 结算\t\t\t      |\n");        printf("\t\t\t\t|\t7 退出\t\t\t      |\n");        printf("\t\t\t\t+-------------------------------------+\n");        printf("\n\n\n");        printf("\t\t\t\t请输入你想实现的功能前的代号:\n");        printf("\t\t\t\t");        cin >> k;  while(!k) {     cin.clear();     cin.sync();     k=-1; break;   }switch(k)        {    case 1:             {               cout<<"您的身份是:"<>choose;                switch(choose)              {                case 1:                    {                        o_head->m_display(head2,m_head);break;                    }                            case 2:                    {                        o_head->h_display(head2,h_head);break;                    }                            case 3:                {                        o_head->l_display(head2,l_head);break;                }                                 default:break;               }                                             break;             }                case 2:                {                    system("cls");                    cout<<"\n请选择您的会员身份:\n"<>k;             while(!k)            {            cin.clear();            cin.sync();            k=-1;            break;            }                        system("cls");            if(k==1)             { m_head->find(m_head);system("pause"); }             else if(k==2)            { h_head->find(h_head);system("pause"); }             else if(k==3)            { l_head->find(l_head);system("pause"); }             else            {            cout<<"请输入正确的序号!"<display(head);system("pause");break;                case 4:            {            cout<<"请选择搜索方式:"<<"\n\n";            cout<<"1.按照书名搜索\t2.按照作者名字搜索\t3.按关键字搜索"<<"\n\n";            cout<<"请输入你想要选择的方式序号:";            cin>>k;            while(!k)            {            cin.clear();            cin.sync();            k=-1;            break;            }            system("cls");            if(k==1)             { head->find_name(head);system("pause"); }             else if(k==2)            { head->find_author(head);system("pause"); }             else if(k==3)            { head->find_critical(head);system("pause"); }             else            {            cout<<"请输入正确的序号!"<Addbook(head2,head);system("pause");break;                case 6:            {              t_sum=head2->count(head2);              if(t_sum==-1)              {                  system("pause");                  break;              }            cout<<"您的身份是:"<>choose;                switch(choose)              {                case 1:                    {                   orderflag=o_head->m_storage(head2,m_head,t_sum);                if(orderflag!=-1)                {                 cout<<"请进行下一步支付步骤"<<"\n\n";                 system("pause");                head2->pay();                head2->remake(head2);                }                 break;                 }                             case 2:                    {                   orderflag=o_head->h_storage(head2,h_head,t_sum);                if(orderflag!=-1)                {                 cout<<"请进行下一步支付步骤"<<"\n\n";                 system("pause");                head2->pay();                head2->remake(head2);                }                 break;                 }                             case 3:                {                   orderflag=o_head->l_storage(head2,l_head,t_sum);                if(orderflag!=-1)                {                 cout<<"请进行下一步支付步骤"<<"\n\n";                 system("pause");                head2->pay();                head2->remake(head2);                }                 break;                 }                 default:break;               }                        break;                        }                case 7:        head->Savedata(head);return 0;                       case 8:        head->Addbook(head);break;                case 9:            {             system("cls");            cout<<"\n请选择要进行添加的顾客信息:\n\n";            cout<<"1.添加新会员\t2.添加新贵宾\t3.添加普通顾客信息\n\n";            cout<<"请输入对应序号:";            cin>>k;                         while(!k)            {            cin.clear();            cin.sync();            k=-1;            break;            }                        system("cls");                        if(k==1)             { m_head->addm(m_head);m_head->savedata(m_head);system("pause"); }                         else if(k==2)            { h_head->addh(h_head);h_head->savedata(h_head);system("pause"); }                         else if(k==3)            { l_head->addl(l_head);l_head->savedata(l_head);system("pause"); }                         else            {cout<<"请输入正确的序号!"<

【二次开发所添加的功能】

在功能5购买书籍中,用户在使用的过程体验并不佳,因为无法直观看到自己已经选中了哪些书籍,故在此添加一个“购物车”形式的展示功能

【代码实现】

void trolley::display(trolley* t){    if (t->next == NULL)    {        cout << "目前购物车内无信息!\t\t" << endl;        cout << "—————————————————————————————————————————————————————————\n";    }    else    {        cout << "\n您的购物车中目前有以下书籍:" << endl;        cout << "—————————————————————————————————————————————————————————\n";        t = t->next;        int n = 1;        while (1)        {            cout << "NO." << n << ":" << "\t" << endl;            cout << "—————————————————————————————————————————————————————————\n";            cout << "书号:" << t->book_ID << "\t";            cout << "书名:《" << t->book_name << "》" << "\t";            cout << "作者:" << t->author << "\t";            cout << "出版社:" << t->publishing << "\t";            cout << "定价:" << t->price << "\t";            cout << "购买份数:" << t->num << endl;            cout << "—————————————————————————————————————————————————————————\n";            if (t->next == NULL)                break;            else            {                t = t->next;                n++;            }        }    }}void trolley::Addbook(trolley* t, book* m){    trolley* hd = t;    book* hd2 = m;    trolley* p;    trolley* p1;    string t_name, ch;    int n=0, flag = 0;    while (1)    {        system("cls");        flag = 0;        t->display(hd);        cout << "\n是否继续添加书籍?\n";        cout << "Y)继续添加\tN)返回主菜单\tD)删除订单" << endl;        cin >> ch;        if (ch == "Y" || ch == "y");        else if (ch == "N" || ch == "n")return;        else if (ch == "D" || ch == "d")        {            hd->Delete_t(hd);            system("pause");            continue;        }        else {            cout << "请输入正确指令" << endl; system("pause"); continue;        }        while (t->next != NULL)        {            t = t->next;        }        p = new trolley;        t->next = p;        p->next = NULL;        cout << "请输入想购买的书名:" << endl;        cin >> t_name;        m = hd2;        while (1)        {            if (m->book_name == t_name)            {                ++n;                cout << "NO." << n << ":" << "\t" << endl;                cout << "—————————————————————————————————————————————————————————\n";                cout << "书号:" << m->book_ID << "\t";                cout << "书名:《" << m->book_name << "》" << "\t";                cout << "作者:" << m->author << "\t";                cout << "出版社:" << m->publishing << "\t";                cout << "定价:" << m->price << "\n";                cout << "—————————————————————————————————————————————————————————\n\n";                cout << "请问是否确认购买该书籍?" << "\n\n";                cout << "1)Y\t2)N" << "\n\n";                cin >> ch;                if (ch == "N" || ch == "n")                {                    t->next = t->next->next;                }                if (ch == "Y" || ch == "y")                {                    cout << "请输入您想要购买的份数:";                    cin >> n;                    p1 = hd;                    while (p1 != t)                    {                        if (p1->next->book_name == t_name)                        {                            p1->next->num += n;                            t->next = t->next->next;                            flag = 1;                            break;                        }                        p1 = p1->next;                    }                    if (flag == 1)break;                    p->book_ID = m->book_ID;                    p->book_name = t_name;                    p->author = m->author;                    p->publishing = m->publishing;                    p->price = m->price;                    p->num = n;                    t = p;                }            }            if (m->next == NULL && n == 0)            {                cout << "没有名为《" << t_name << "》的书籍" << endl;                break;            }            if (m->next == NULL)            {                break;            }            m = m->next;        }        cout << "是否继续购买?" << endl;        cout << "Y)继续购买\tN)返回主菜单" << endl;        cin >> ch;        if (ch == "Y" || ch == "y");        if (ch == "N" || ch == "n")break;    }}
如此一来,用户便能够清楚地看到自己所购买的书籍内容了

关键词: