验证C ++中仅针对字母和空格输入的字符串

时间:2018-09-12 21:18:07

标签: c++ validation

由于我只是在《 C ++入门》一书的第4章(使用if,else进行决策)上,所以我对函数的了解还不是很多。 在这里,我的问题要求我输入跑步者名称和他们的计时器的输入,以便将他们排名第一,第二和第三。

我很难验证跑步者姓名的字符串输入(仅字母和空格)。 我正在考虑使用loop或bool,但我不知道放置它的正确方法是什么。

有人可以告诉我在这种情况下最能使用什么功能,而无需重复执行多行代码。 预先为我的长版代码感到抱歉,我也在寻找缩短它的方法。

#include <iostream>
#include <string>
#include <iomanip> // for setw, setpresicion, control output
#include <cctype> // for getline, cin.

using namespace std;

int main()
{
    string runner1, runner2, runner3;
    double time1, time2, time3;


    cout << "Enter Runner 1 name: ";
    getline(cin, runner1);
    cout << "Enter Runner 2 name: ";
    getline(cin, runner2);
    cout << "Enter Runner 3 name: ";
    getline(cin, runner3);

    cout << runner1 << "'s finishing time: ";
    cin >> time1;
    while (!cin || (time1 < 0)){              //the # is negative
        cout << "Please enter a non-negative number!\n";
        cin >> time1;
    }
    cout << runner2 << "'s finishing time: ";
    cin >> time2;
    while (!cin || (time2 < 0)){              //the # is negative
        cout << "Please enter a non-negative number!\n";
        cin >> time2;
    }
    cout << runner3 << "'s finishing time: ";
    cin >> time3;
    while (!cin || (time3 < 0)){
        cout << "Please enter a non-negative number!\n";
        cin >> time3;
    }

    // This is for Rank1----------------------------
        cout << "1st place : ";
        if((time1 < time2)&&(time1 < time3))
        {
            cout << left << setw(5) << runner1 << " " << right
            << setw(5) << time1 << endl;
        }
        else if (time2 < time3){
            cout << left << setw(5) << runner2 << " " << right
            << setw(5)  << time2 << endl;
        }
        else {
            cout << left << setw(5) << runner3 << " " << right
            << setw(5) << time3 << endl;
        }
        //Rank2------------------------------------
        cout << "2nd place : ";
        if ((time1 < time2)&&(time3 < time1)){
            cout << left << setw(5) << runner1 << " " << right
            << setw(5) << time1 << endl;
        }
        else if ((time3 < time2)&&(time1 < time3)){
            cout << left << setw(5) << runner3 << " " << right
            << setw(5) << time3 << endl;
        }
        else {
            cout << left << setw(5) << runner2 << " " << right
            <<setw(5) << time2 << endl;
        }
        // RANK 3-----------------------------------
        cout << "3rd place : ";
        if ((time1 > time2)&&(time1 > time3)){
            cout << left << setw(5) << runner1 << " " << right
            << setw(5) << time1 << endl;
        }
        else if ((time1 < time3)&&(time3 > time2)){
            cout << left << setw(5) << runner3 << " " << right
            << setw(5) << time3 << endl;
        }
        else {
            cout << left << setw(5) << runner2 << " " << right
            <<setw(5) << time2 << endl;
        }



    return 0;
}

3 个答案:

答案 0 :(得分:1)

您可能想使用类似的东西

#include <cctype>
#include <string>
#include <iostream>

int main()
{
    std::string foo;

    bool valid;
    do {
        std::getline(std::cin, foo);

        valid = true;
        for (std::size_t i{}; i < foo.length() && valid; ++i) {
            if (!(std::isalpha(static_cast<unsigned char>(foo[i])) ||
                  std::isspace(static_cast<unsigned char>(foo[i]))))
                valid = false;
        }
    } while (!valid);
}

检查每个字符是否为字母或空格。

答案 1 :(得分:0)

此例程将进行验证:

bool validateString(const std::string& s)
{
    for (const char c : s) {
        if (!isalpha(c) && !isspace(c))
            return false;
    }

    return true;
}

答案 2 :(得分:-1)

这是我对解决方案的建议:

#include <iostream>
#include <string>

bool validateString(std::string toCheck) {

bool correct = false;

for (int i = 0; i<toCheck.length(); i++) {

    if ((toCheck[i] >= 65 && toCheck[i] <= 90) || (toCheck[i] >= 97 && toCheck[i] <= 122) || toCheck[i] == 32) {
        correct = true;
    }
    else {
        return false;
    }
}

return correct;
}

int main()
{
    std::cout << validateString("Whats up peeps") << std::endl;
    std::cout << validateString("234235") << std::endl;

    system("pause");
    return 0;
}

使用ASCII Table

注意:可能不是最好的方法,但它可以工作。