首页 > 编程语言 >【C++】购书系统问题测试&功能补充

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

时间:2023-03-08 21:55:42浏览次数:41  
标签:case head 购书 head2 system C++ break 测试 printf

代码来源:舍友大一的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<<"您的身份是:"<<endl;
            cout<<"1)会员\t2)贵宾\t3)普通顾客"<<endl;
            cin>>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"<<endl;
                    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->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<<"请输入正确的序号!"<<endl;system("pause"); } 
            break;
                }
            
    case 3:     system("cls");head->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<<"请输入正确的序号!"<<endl;system("pause"); } 
            break;
            } 
            
            
    case 5:        head2->Addbook(head2,head);system("pause");break;
            
    case 6:
            {
              t_sum=head2->count(head2);
              if(t_sum==-1)
              {
                  system("pause");
                  break;
              }
            cout<<"您的身份是:"<<endl;
            cout<<"1)会员\t2)贵宾\t3)普通顾客"<<endl;
            cin>>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<<"请输入正确的序号!"<<endl;system("pause"); } 
        
            break;
            } 
            
    default:
            {
            cout<<"请输入正确序号!"<<endl;
            system("pause");
            break;
            }
        }
    }
    return 0;
}

 

【问题产生的原因】

由于接收数值的变量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<<"您的身份是:"<<endl; cout<<"1)会员\t2)贵宾\t3)普通顾客"<<endl; cin>>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"<<endl; 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->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<<"请输入正确的序号!"<<endl;system("pause"); } break; } case 3: system("cls");head->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<<"请输入正确的序号!"<<endl;system("pause"); } break; } case 5: head2->Addbook(head2,head);system("pause");break; case 6: { t_sum=head2->count(head2); if(t_sum==-1) { system("pause"); break; } cout<<"您的身份是:"<<endl; cout<<"1)会员\t2)贵宾\t3)普通顾客"<<endl; cin>>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<<"请输入正确的序号!"<<endl;system("pause"); } break; } default: { cout<<"请输入正确序号!"<<endl; system("pause"); break; } } } return 0; }

 

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

 

 

 在功能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;

    }
}

 

 

 

 

 

如此一来,用户便能够清楚地看到自己所购买的书籍内容了

 

标签:case,head,购书,head2,system,C++,break,测试,printf
From: https://www.cnblogs.com/marisa514/p/17196194.html

相关文章

  • [Primer] 第 14 章 C++ 中的代码重用
    第14章C++中的代码重用14.1包含对象成员的类类初始化列表中有多个项目时,初始化的顺序为在类中的声明顺序而不是列表顺序。14.2私有继承使用私有继承,基类的所有公......
  • 搭建 Jenkins 自动化测试平台
     Jenkins自动化测试平台的作用自动化构建平台的执行流程(目标)是:我们将代码提交到代码托管工具上,如github、gitlab、gitee等。1、Jenkins要能够检测到我们的提交。2、Jen......
  • gcc 编译 C/C++ 文件
    gcc编译C/C++文件众所周知,C/C++程序想要得到执行,主要需要执行编译和链接两个过程,这个过程比较繁琐,尤其是程序使用到了其他的头文件的时候。gcc是常用的编译工具,其流程主要......
  • 软件测试常见面试题1000问涵盖一千+公司面试软件测试面试题(全网最全)
    前前后后面试了有20多家的公司吧,最近抽空把当时的录音整理了下,然后给大家分享下开头都是差不多,就让做一个自我介绍,这个不用再给大家普及了吧视频教程:【【呕心沥血】耗时7天......
  • .NET Github Actions 测试覆盖率
    如果熟悉GIthub我们经常可以在一些开源项目的PR上看到会配置测试的验证以及覆盖率的报告,并且可以强制覆盖率不低于设定的值才可以进行MergePR。1.测试创建一个xU......
  • (P01)C++介绍
    文章目录​​1.需要掌握的重要练习​​​​2.为什么要学习C++​​​​3.C++为什么难学​​​​4.C++11值得学习的新特性​​​​5.几本推荐学习C++的书​​​​6.开发工具......
  • (P03)从C到C++:域运算符,new,delet运算符,重载,name managling与extern “C“,带默认参数的函
    文章目录​​1.域运算符​​​​2.new、delete运算符​​​​3.重载​​​​4.namemanagling与extern“C”​​​​5.带默认形参值的函数​​​​6.带默认形参值的函数的......
  • (P05)从C到C++:内联函数,带参数宏,4种强制类型转化
    文章目录​​1.内联函数​​​​2.4种新的类型转换运算符​​1.内联函数当程序执行函数调用时,系统要建立栈空间,保护现场,传递参数以及控制程序执行的转移等等,这些工作需要系......
  • 命令行中使用junit5进行类测试
    还是比较喜欢单独jar包的形式,自己编译又总是不成功。junit4与junit5语法不完全兼容。 用户手册中提到,有个适合在命令行执行的jar包https://junit.org/junit5/docs/curr......
  • 006 固定吞吐量测试
    需求说明:一个用户以20qps的频率来访问服务器,持续10秒钟,查看服务器平均响应时间使用元件:线程组--定时器--固定吞吐量定时器(constantthroughthputtimer)实现步骤创建......