Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 向量字符串验证问题_C++_String_Validation_Vector - Fatal编程技术网

C++ 向量字符串验证问题

C++ 向量字符串验证问题,c++,string,validation,vector,C++,String,Validation,Vector,我正在做一个简单的人口统计程序。输入数据并将其输出到csv文件。我在验证名称的数据输入时遇到问题。如果只输入数字,则验证工作正常,但如果我输入两次字母数字字符串,则验证无效。例如,如果我键入Max1,我会得到异常,如果我再次键入Max1,它只会转到下一个函数调用。但是,如果我只输入一串数字,它将不会移动,直到我输入正确的纯字母字符串或两个字母数字字符串 #include <iostream> #include <vector> #include "

我正在做一个简单的人口统计程序。输入数据并将其输出到csv文件。我在验证名称的数据输入时遇到问题。如果只输入数字,则验证工作正常,但如果我输入两次字母数字字符串,则验证无效。例如,如果我键入Max1,我会得到异常,如果我再次键入Max1,它只会转到下一个函数调用。但是,如果我只输入一串数字,它将不会移动,直到我输入正确的纯字母字符串或两个字母数字字符串

    #include <iostream>
    #include <vector>
    #include "People.h"

    int main()
    {
        const short elements = 2;
        PersonalData Demographics;
        std::string input;
        std::vector<std::string> response;

        for (int i = 0; i < elements; i++)
        {
            std::cout << "Please enter first name of child: " << i+1 << std::endl;
            std::cin >> input;
            response.push_back(input)
            Demographics.setName(response);
        }
        return 0;
    }

    People.hpp

#ifndef PEOPLE_H_INCLUDED
#define PEOPLE_H_INCLUDED
#include <vector>

    class PersonalData
    {
        private:
            std::vector<std::string> name;
        public:
            void setName(std::vector<std::string>&); //get the names of each person
    };

    #endif // PEOPLE_H_INCLUDED



People.cpp

#include <iostream>>
#include "People.h"
#include <vector>

    void PersonalData::setName(std::vector<std::string> &names)
    {
        string retry;
        bool valid=false; // we start by assuming that the entry is not valid
        std::string::const_iterator it;
        while(!valid) //start validation loop
        {
            for(int i=0; i <names.size(); i++)
            {
                 for(it = names[i].begin(); it != names[i].end(); ++it) //start loop to check type of each char in string
                {
                    if(isalpha(*it)) //if it is char set name to user names
                    {
                        name=names;
                        valid = true; //the entry was valid, jump out of loop
                    }
                    else //it was not just char, try again
                    {
                        std::cout << "Name should be alphabetic only.  Try again: " <<endl;
                        std::cin >> retry;
                        names.erase(names.begin()+i); // remove the bad non-alphabetic entry from the names array
                        names.push_back(retry);
                        break;
                    }
                }
            }
        }
    }
#包括
#包括
#包括“People.h”
int main()
{
常量短元素=2;
个人数据统计;
std::字符串输入;
病媒反应;
for(int i=0;i对于(int i=0;i,我建议您首先测试名称是否有效,如果有效,然后将其添加到名称向量。您当前的代码添加无效名称,然后尝试删除它们,这没有太大意义

检查名称是否有效(包含所有字母字符)的方法是使用算法函数:

#include <iostream>
#include <vector>
#include <ccytpe>
#include <algorithm>
//...
using namespace std;
int main()
{
    const short elements = 2;
    PersonalData Demographics;
    std::string input;
    std::vector<std::string> response;

    for (int i = 0; i < elements; i++)
    {
        bool input_good;
        do
        {
            std::cout << "Please enter first name of child: " << i+1 <<  std::endl;
            std::cin >> input;
            input_good = std::all_of(input.begin(), input.end(), ::isalpha);
            if ( !input_good )
               cout << "Sorry, bad input.  Try again...\n";
        } while (!input_good);
        response.push_back(input)
    }

    // now we know that all the names are valid.  Place in 
    // Demographics
    Demographics.setName(response);
    return 0;
}

void PersonalData::setName(std::vector<std::string> &names)
{
   name = names;  // simple
}
#包括
#包括
#包括
#包括
//...
使用名称空间std;
int main()
{
常量短元素=2;
个人数据统计;
std::字符串输入;
病媒反应;
for(int i=0;istd::整个循环可以用一个简单的lambda替换为
std::remove_if/erase
。还有,为什么你要在向量中加上坏名字?为什么不在你把名字放在向量中之前先检查一下?好的,这很有意义。只需删除任何不是字母字符的东西。不知道remove如果选项存在。正如你所知,我还是很新的。不确定lamda是什么,但我可以研究它。是的,这更有意义,谢谢。我需要熟悉算法库,我不知道所有的。我似乎无法让所有的都工作。它一直说它不在这个范围内,即使我有算法库包含了你使用的编译器是什么?<代码> STD::AlcOfof < /Cuff>是C++ 11兼容编译器的支持。这里有一个简单的例子:GNU GCC代码::块IDE。我如何检查我在使用什么?G+有一个命令行选项来检查版本(我想它是代码>版本< /代码>)。还有一个选项可以开启C++ 11的兼容。