C++ 无法解决此问题

C++ 无法解决此问题,c++,database,user-input,C++,Database,User Input,在下面代码中的添加用户部分,我无法为“添加另一个人?(y/n):”问题键入任何字符。它只是跳回进入年龄。我该如何解决这个问题 我尝试将ans更改为字符串,实现while循环以强制显示问题,以及其他许多事情。似乎什么都不管用,我已经试了两个小时了 #include <iostream> #include <string> #include <Windows.h> using namespace std; int main() { char ans;

在下面代码中的添加用户部分,我无法为
“添加另一个人?(y/n):”
问题键入任何字符。它只是跳回进入年龄。我该如何解决这个问题

我尝试将
ans
更改为字符串,实现
while
循环以强制显示问题,以及其他许多事情。似乎什么都不管用,我已经试了两个小时了

#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

int main()
{
    char ans;
    int people;
    int option;
    int count = 0;
    struct data
    {
        string name;
        int age;
        char gender;
        string comments;
    }person[100];
    // homescreen 
homescreen:
    cout << "Welcome to the Data Base!" << endl;
    cout << endl;
    // displaying all people
    for (int list = 0; list < count; list++)
    {
        cout << list << ".) " << person[list].name << endl;
    }
    cout << endl;
    cout << "[1] View Person" << endl;
    cout << "[2] Add Person" << endl;
    cout << "[3] Edit Person" << endl;
    cout << "[4] Delete Person" << endl;
    cout << "[5] Exit" << endl;
    cout << "Choose Option: ";  cin >> option;
    // using options
    while (option != 5)
    {
        if (option == 1)
        {
        view:
            for (int list2 = 0; list2 < count; list2++)
            {
                cout << list2 << ".) " << person[list2].name << endl;
            }
            cout << endl;
            cout << "Enter number of person you want: ";  cin >> people;

            system("cls");
            cout << "Name: " << person[count].name << endl;
            cout << "Age: " << person[count].age << endl;
            cout << "Gender: " << person[count].gender << endl;
            cout << "Comments: " << person[count].comments << endl << endl;

            cout << "View another person?(y/n): ";      cin >> ans;
            if (ans == 'y')
            {
                system("cls");      goto view;
            }
            else if (ans == 'n')
            {
                system("cls");      goto homescreen;
            }
        }
        if (option == 2)
        {
        add:
            system("cls");
            cout << "Name: ";  cin >> person[count].name;

            system("cls");
            cout << "Age: ";  cin >> person[count].age;

            system("cls");
            cout << "Gender(M/F/H): ";  cin >> person[count].gender;

            system("cls");
            cout << "Comments: ";  cin >> person[count].comments;

            count++;
            system("cls");
            cout << "Add another person?(y/n): ";   cin >> ans;

            if (ans == 'y')
            {
                system("cls");
                goto add;
            }
            else if (ans == 'n')
            {
                system("cls");
                goto homescreen;
            }
        }
    }
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
查尔安斯;
国际人士;
int选项;
整数计数=0;
结构数据
{
字符串名;
智力年龄;
性别;
字符串注释;
}人[100];
//主屏幕
主屏幕:
库特
  • 代码中的
    goto
    语句使程序非常好 结构

    因此,不要考虑其他选项,比如无限 while循环,一旦用户进入
    n
    或移动,该循环将中断 将代码添加到函数中

  • 其次,如果您没有输入任何
    人员
    并选择 选项
    1
    。您仍然将
    人员的属性作为
    
    count
    至少初始化为零。请记住属性为 此时未初始化。正在访问未初始化的 变量将调用。因此, 在执行选项
    1
    中的代码之前,请提供一个检查(
    if(count>0
    ))

  • 除此之外,请记住 刷新输出缓冲区,而
    '\n'
    则不刷新。因此,大多数 你需要的箱子

  • <>但不是最不重要的是,<强>使用< /St>而不是使用具有预定义大小的C样式数组。如果用户有超过<代码> 100 >代码>输入,如果C++中的解决方案是“代码> STD::vector < /代码>,它可以自动扩展,因为它的存储被自动处理。 以下是您的计划的可能解决方案,其中的评论将引导您完成我上面提到的事情

    #include <iostream>
    #include <string>
    #include <vector>
    #include <windows.h>
    
    struct Data
    {
        std::string name;
        int age;
        char gender;
        std::string comments;
        Data(const std::string& n, int a, char g, const std::string& c) // provide a Constructor
            :name(n), age(a), gender(g), comments(c)
        {}
    };
    
    void debugMsg(const std::string& msg)
    {
        system("cls");
        std::cout << "\n\n\t\t" << msg << "\n\n";
        Sleep(3000);
    }
    
    int main()
    {
        std::vector<Data> person; // use std::vector to store the datas
        while (true)  // loop: 1
        {
            system("cls");
            std::cout << "Welcome to the Data Base! \n\n";
            std::cout << "[1] View Person\n";
            std::cout << "[2] Add Person\n";
            std::cout << "[3] Edit Person\n";
            std::cout << "[4] Delete Person\n";
            std::cout << "[5] Exit\n";
            std::cout << "Choose Option: ";  
            int option;  std::cin >> option; 
            switch (option) // use switch to validate the options
            {
            case 1:
            {
                while (true)   // loop - 2 -> case 1
                {
                    // if no data available to show -> just break the loop 2 and return to the outer loop(i.e, loop 1) 
                    if (person.empty()) { debugMsg("No person available to show ....going to main manu...."); break; }
                    // otherwise: displaying all people
                    for (std::size_t index = 0; index < person.size(); ++index) 
                        std::cout << index << ".) " << person[index].name << "\n";
                    std::cout << "\nEnter number of person you want: ";  
                    std::size_t index;  std::cin >> index;
                    // if the index is not valid -> just break the loop 2 and return to the outer loop(i.e, loop 1) 
                    if (index < 0  || index >= person.size()) { debugMsg("Sorry, wrong index!... returning to the main menu......");  break; }
                    system("cls");
                    std::cout << "Name: " << person[index].name << std::endl;
                    std::cout << "Age: " << person[index].age << std::endl;
                    std::cout << "Gender: " << person[index].gender << std::endl;
                    std::cout << "Comments: " << person[index].comments << std::endl << std::endl;
                    std::cout << "View another person?(y/n): ";      
                    char ans;  std::cin >> ans;
                    if (ans == 'y') { system("cls");    continue; } // just continue looping
                    else if (ans == 'n') { break; }                 // this will break the loop 2 and return to the outer loop(i.e, loop 1)                 
                    else { debugMsg("Sorry, wrong option!... returning to the main menu......");  break; }
                }
            } break;
            case 2:
            {
                while (true)   // loop - 3 -> case 2
                {
                    system("cls");
                    std::string name, comments; int age; char gender;
                    std::cout << "Name: ";           std::cin >> name;
                    std::cout << "Age: ";            std::cin >> age;
                    std::cout << "Gender(M/F/H): ";  std::cin >> gender;
                    std::cout << "Comments: ";       std::cin >> comments;
                    // simply construct the Data in person vector in place
                    person.emplace_back(name, age, gender, comments);
                    std::cout << "\n\nAdd another person?(y/n): ";
                    char ans;  std::cin >> ans;
                    if (ans == 'y') { system("cls"); continue; }
                    else if (ans == 'n') { system("cls"); break; } // same as case 1
                    else { debugMsg("Sorry, wrong option!... returning to the main menu......"); break; }
                }
            } break;
            case 3: { /*code*/ debugMsg("Sorry, Not implemented!... returning to the main menu......"); } break;
            case 4: { /*code*/ debugMsg("Sorry, Not implemented!... returning to the main menu......"); } break;
            case 5: return 0; // if its 5, just retun the main
            default: break;
            }
        }
        return 0;
    }
    
    #包括
    #包括
    #包括
    #包括
    结构数据
    {
    std::字符串名;
    智力年龄;
    性别;
    字符串注释;
    Data(const std::string&n,int a,char g,const std::string&c)//提供一个构造函数
    :姓名(n)、年龄(a)、性别(g)、评论(c)
    {}
    };
    void debugMsg(const std::string&msg)
    {
    系统(“cls”);
    标准::cout
    
  • 代码中的
    goto
    语句使程序非常好 结构

    因此,不要考虑其他选项,比如无限 while循环,一旦用户进入
    n
    或移动,该循环将中断 将代码添加到函数中

  • 其次,如果您没有输入任何
    人员
    并选择 选项
    1
    。您仍然将
    人员的属性作为
    
    count
    至少初始化为零。请记住属性为 此时未初始化。正在访问未初始化的 变量将调用。因此, 在执行选项
    1
    中的代码之前,请提供一个检查(
    if(count>0
    ))

  • 除此之外,请记住 刷新输出缓冲区,而
    '\n'
    则不刷新。因此,大多数 你需要的箱子

  • <>但不是最不重要的是,<强>使用< /St>而不是使用具有预定义大小的C样式数组。如果用户有超过<代码> 100 >代码>输入,如果C++中的解决方案是“代码> STD::vector < /代码>,它可以自动扩展,因为它的存储被自动处理。 以下是您的计划的可能解决方案,其中的评论将引导您完成我上面提到的事情

    #include <iostream>
    #include <string>
    #include <vector>
    #include <windows.h>
    
    struct Data
    {
        std::string name;
        int age;
        char gender;
        std::string comments;
        Data(const std::string& n, int a, char g, const std::string& c) // provide a Constructor
            :name(n), age(a), gender(g), comments(c)
        {}
    };
    
    void debugMsg(const std::string& msg)
    {
        system("cls");
        std::cout << "\n\n\t\t" << msg << "\n\n";
        Sleep(3000);
    }
    
    int main()
    {
        std::vector<Data> person; // use std::vector to store the datas
        while (true)  // loop: 1
        {
            system("cls");
            std::cout << "Welcome to the Data Base! \n\n";
            std::cout << "[1] View Person\n";
            std::cout << "[2] Add Person\n";
            std::cout << "[3] Edit Person\n";
            std::cout << "[4] Delete Person\n";
            std::cout << "[5] Exit\n";
            std::cout << "Choose Option: ";  
            int option;  std::cin >> option; 
            switch (option) // use switch to validate the options
            {
            case 1:
            {
                while (true)   // loop - 2 -> case 1
                {
                    // if no data available to show -> just break the loop 2 and return to the outer loop(i.e, loop 1) 
                    if (person.empty()) { debugMsg("No person available to show ....going to main manu...."); break; }
                    // otherwise: displaying all people
                    for (std::size_t index = 0; index < person.size(); ++index) 
                        std::cout << index << ".) " << person[index].name << "\n";
                    std::cout << "\nEnter number of person you want: ";  
                    std::size_t index;  std::cin >> index;
                    // if the index is not valid -> just break the loop 2 and return to the outer loop(i.e, loop 1) 
                    if (index < 0  || index >= person.size()) { debugMsg("Sorry, wrong index!... returning to the main menu......");  break; }
                    system("cls");
                    std::cout << "Name: " << person[index].name << std::endl;
                    std::cout << "Age: " << person[index].age << std::endl;
                    std::cout << "Gender: " << person[index].gender << std::endl;
                    std::cout << "Comments: " << person[index].comments << std::endl << std::endl;
                    std::cout << "View another person?(y/n): ";      
                    char ans;  std::cin >> ans;
                    if (ans == 'y') { system("cls");    continue; } // just continue looping
                    else if (ans == 'n') { break; }                 // this will break the loop 2 and return to the outer loop(i.e, loop 1)                 
                    else { debugMsg("Sorry, wrong option!... returning to the main menu......");  break; }
                }
            } break;
            case 2:
            {
                while (true)   // loop - 3 -> case 2
                {
                    system("cls");
                    std::string name, comments; int age; char gender;
                    std::cout << "Name: ";           std::cin >> name;
                    std::cout << "Age: ";            std::cin >> age;
                    std::cout << "Gender(M/F/H): ";  std::cin >> gender;
                    std::cout << "Comments: ";       std::cin >> comments;
                    // simply construct the Data in person vector in place
                    person.emplace_back(name, age, gender, comments);
                    std::cout << "\n\nAdd another person?(y/n): ";
                    char ans;  std::cin >> ans;
                    if (ans == 'y') { system("cls"); continue; }
                    else if (ans == 'n') { system("cls"); break; } // same as case 1
                    else { debugMsg("Sorry, wrong option!... returning to the main menu......"); break; }
                }
            } break;
            case 3: { /*code*/ debugMsg("Sorry, Not implemented!... returning to the main menu......"); } break;
            case 4: { /*code*/ debugMsg("Sorry, Not implemented!... returning to the main menu......"); } break;
            case 5: return 0; // if its 5, just retun the main
            default: break;
            }
        }
        return 0;
    }
    
    #包括
    #包括
    #包括
    #包括
    结构数据
    {
    std::字符串名;
    智力年龄;
    性别;
    字符串注释;
    Data(const std::string&n,int a,char g,const std::string&c)//提供一个构造函数
    :姓名(n)、年龄(a)、性别(g)、评论(c)
    {}
    };
    void debugMsg(const std::string&msg)
    {
    系统(“cls”);
    正如上面提到的,使用“goto”是一种不好的风格,所以我建议稍微构造一下你的程序。下面是我的版本。
    当然,我没有添加任何检查和控制,作者可以自己完成。但是主要逻辑应该可以工作。当然,最好使用向量而不是静态数组

    #include <iostream>
    #include <string>
    #include <Windows.h>
    using namespace std;
    
    enum options { OPT_VIEW = 1, OPT_ADD = 2, OPT_EDIT = 3, OPT_DELETE = 4, OPT_EXIT = 5 };
    
    struct data
    {
        string name;
        int age;
        char gender;
        string comments;
    };
    
    class App
    {
    private:
        data person[100];
        int  count = 0;
    public:  
        App();
        void Run();
        int HomeScreen();
        void View();
        void Add();
    };
    
    App::App() : count(0)
    {}
    
    void App::Run()
    {
        int option = HomeScreen();
        while(option != OPT_EXIT)
        {
            switch(option)
            {
            case OPT_VIEW:
                View();
                break;
            case OPT_ADD:
                Add();
                break;
            }
            option = HomeScreen();
        }
    }
    
    int App::HomeScreen()
    {
        int option = 0;
        cout << "Welcome to the Data Base!" << endl;
        cout << endl;
        // displaying all people
        for(int list = 0; list < count; list++)
        {
            cout << list << ".) " << person[list].name << endl;
        }
        cout << endl;
        cout << "[1] View Person" << endl;
        cout << "[2] Add Person" << endl;
        cout << "[3] Edit Person" << endl;
        cout << "[4] Delete Person" << endl;
        cout << "[5] Exit" << endl;
        cout << "Choose Option: ";  cin >> option;
        return option;
    }
    
    void App::View()
    {
        char ans = 0;
        do
        {
            int people = 0;
    
            for(int list2 = 0; list2 < count; list2++)
            {
                cout << list2 << ".) " << person[list2].name << endl;
            }
            cout << endl;
            cout << "Enter number of person you want: ";  cin >> people;
    
            system("cls");
            cout << "Name: " << person[people].name << endl;
            cout << "Age: " << person[people].age << endl;
            cout << "Gender: " << person[people].gender << endl;
            cout << "Comments: " << person[people].comments << endl << endl;
    
            cout << "View another person?(y/n): ";      cin >> ans;
        }
        while(ans == 'y');
    
        system("cls");
    }
    
    void App::Add()
    {
        char ans = 0;
        do
        {
            system("cls");
            cout << "Name: ";  cin >> person[count].name;
    
            system("cls");
            cout << "Age: ";  cin >> person[count].age;
    
            system("cls");
            cout << "Gender(M/F/H): ";  cin >> person[count].gender;
    
            system("cls");
            cout << "Comments: ";  cin >> person[count].comments;
    
            count++;
            system("cls");
            cout << "Add another person?(y/n): ";   cin >> ans;
        }
        while(ans == 'y');
    
        system("cls");
    }
    
    int main()
    {
        App program;
        program.Run();
    }
    
    #包括
    #包括
    #包括
    使用名称空间std;
    枚举选项{OPT_VIEW=1,OPT_ADD=2,OPT_EDIT=3,OPT_DELETE=4,OPT_EXIT=5};
    结构数据
    {
    字符串名;
    智力年龄;
    性别;
    字符串注释;
    };
    类应用程序
    {
    私人:
    资料员[100];
    整数计数=0;
    公众:
    App();
    无效运行();
    int主屏幕();
    void视图();
    无效添加();
    };
    App::App():计数(0)
    {}
    void应用程序::Run()
    {
    int选项=主屏幕();
    while(选项!=OPT_退出)
    {
    开关(选件)
    {
    案例选项视图:
    视图();
    打破
    案例选项添加:
    添加();
    打破
    }
    选项=主屏幕();
    }
    }
    int应用程序::主屏幕()
    {
    int选项=0;
    cout如上所述,使用“goto”是一种不好的风格,所以我建议稍微构造一下程序。下面是我的版本。
    当然,我没有添加任何检查和控制,作者可以自己完成。但是主要逻辑应该可以工作。当然,最好使用向量而不是静态数组

    #include <iostream>
    #include <string>
    #include <Windows.h>
    using namespace std;
    
    enum options { OPT_VIEW = 1, OPT_ADD = 2, OPT_EDIT = 3, OPT_DELETE = 4, OPT_EXIT = 5 };
    
    struct data
    {
        string name;
        int age;
        char gender;
        string comments;
    };
    
    class App
    {
    private:
        data person[100];
        int  count = 0;
    public:  
        App();
        void Run();
        int HomeScreen();
        void View();
        void Add();
    };
    
    App::App() : count(0)
    {}
    
    void App::Run()
    {
        int option = HomeScreen();
        while(option != OPT_EXIT)
        {
            switch(option)
            {
            case OPT_VIEW:
                View();
                break;
            case OPT_ADD:
                Add();
                break;
            }
            option = HomeScreen();
        }
    }
    
    int App::HomeScreen()
    {
        int option = 0;
        cout << "Welcome to the Data Base!" << endl;
        cout << endl;
        // displaying all people
        for(int list = 0; list < count; list++)
        {
            cout << list << ".) " << person[list].name << endl;
        }
        cout << endl;
        cout << "[1] View Person" << endl;
        cout << "[2] Add Person" << endl;
        cout << "[3] Edit Person" << endl;
        cout << "[4] Delete Person" << endl;
        cout << "[5] Exit" << endl;
        cout << "Choose Option: ";  cin >> option;
        return option;
    }
    
    void App::View()
    {
        char ans = 0;
        do
        {
            int people = 0;
    
            for(int list2 = 0; list2 < count; list2++)
            {
                cout << list2 << ".) " << person[list2].name << endl;
            }
            cout << endl;
            cout << "Enter number of person you want: ";  cin >> people;
    
            system("cls");
            cout << "Name: " << person[people].name << endl;
            cout << "Age: " << person[people].age << endl;
            cout << "Gender: " << person[people].gender << endl;
            cout << "Comments: " << person[people].comments << endl << endl;
    
            cout << "View another person?(y/n): ";      cin >> ans;
        }
        while(ans == 'y');
    
        system("cls");
    }
    
    void App::Add()
    {
        char ans = 0;
        do
        {
            system("cls");
            cout << "Name: ";  cin >> person[count].name;
    
            system("cls");
            cout << "Age: ";  cin >> person[count].age;
    
            system("cls");
            cout << "Gender(M/F/H): ";  cin >> person[count].gender;
    
            system("cls");
            cout << "Comments: ";  cin >> person[count].comments;
    
            count++;
            system("cls");
            cout << "Add another person?(y/n): ";   cin >> ans;
        }
        while(ans == 'y');
    
        system("cls");
    }
    
    int main()
    {
        App program;
        program.Run();
    }
    
    #包括
    #包括
    #包括
    使用名称空间std;
    枚举选项{OPT_VIEW=1,OPT_ADD=2,OPT_EDIT=3,OPT_DELETE=4,OPT_EXIT=5};
    结构数据
    {
    字符串名;
    智力年龄;
    性别;
    字符串注释;
    };
    类应用程序
    {
    普里瓦