首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >创建“if not an integer”if语句条件(C++)

创建“if not an integer”if语句条件(C++)
EN

Stack Overflow用户
提问于 2020-05-24 13:07:32
回答 1查看 231关注 0票数 0

处理用户以某种格式输入某些数据的任务(例如,“欧内斯特·海明威,9岁”),然后把这些信息放在一个表格里。

我必须考虑到用户输入中的错误。不要放逗号,不要放太多逗号,要检查",“后面的数据是不是有效的整数。

例如。欧内斯特·海明威9

错误:字符串中没有逗号。

欧内斯特,海明威,9岁

错误:输入中的逗号太多。

欧内斯特·海明威,九岁(输入我有问题)

错误:逗号后面没有整数。

欧内斯特·海明威,9岁

数据字符串:欧内斯特·海明威数据整数:9

下面是我当前的代码

代码语言:javascript
代码运行次数:0
运行
复制
    int commaCount = 0; // keeps track of commas in users input
    int currentIndex = 0; // creating variable to create elements in the vectors

    do
    {
        cout << "Enter a data point (-1 to stop input) :" << endl;
        getline(cin, userInput);

        if (userInput == "-1") { break; } // meant to break out of loop if the user inputs "-1" on their first attempt

        for (int i = 0; i < userInput.length(); i++) {
            if (userInput.at(i) == ',') {
                commaCount++;
            }
        }

        if (commaCount == 0) {
            cout << "Error: No comma in string." << endl;
        }

        else if (commaCount > 1) {
            cout << "Error: Too many commas in input." << endl;
        }

        //else if ( second half of string != a POSITIVE int) "If entry after the comma is not an integer"

        else {
        breakPoint = userInput.find(',');

        listOfAuthors.push_back(userInput.substr(0, breakPoint));
        novelsPerAuthor.push_back(stoi(userInput.substr(breakPoint + 1, userInput.length() - breakPoint + 1)));


        cout << "Data string: " << listOfAuthors.at(currentIndex) << endl;
        cout << "Data integer: " << novelsPerAuthor.at(currentIndex) << endl;
        currentIndex++;

        }

        commaCount = 0; // resets comma count between entries

        cout << " " << endl;
    } while (userInput != "-1"); //exits user input loop if "-1" is inputed

下面是我的代码,我如何分离字符串并将后半部分转换为int,如果这有助于为上面的代码提供上下文。

代码语言:javascript
代码运行次数:0
运行
复制
breakPoint = userInput.find(',');

        listOfAuthors.push_back(userInput.substr(0, breakPoint));
        novelsPerAuthor.push_back(stoi(userInput.substr(breakPoint + 1, userInput.length() - breakPoint + 1)));

如果userInput.substr(breakPoint + 1,userInput.length() - breakPoint +1)不等于一个正整数,程序将输出"Error: Comma not not an integer“。

EN

回答 1

Stack Overflow用户

发布于 2020-05-24 13:41:38

您需要使用从std::string到整数的转换器。在C++中有几种方法可以做到这一点,但我认为使用stoi函数将涵盖所有情况(请注意,如果您尝试转换不是整数的字符串,stoi将抛出异常)。

下面是一个小示例,说明如何实现

代码语言:javascript
代码运行次数:0
运行
复制
#include <iostream>
#include <string>

std::string /* error string */ SplitLine(const std::string& inLine, std::string& outString, int& outNumber)
{
    const std::string separator = ",";
    auto pos = inLine.find(separator);
    auto pos2 = inLine.rfind(separator);

    if (pos == std::string::npos) {
        return "No comma in string.";
    }

    if (pos != pos2 && pos2 != std::string::npos) {
        return "Too many commas in input.";
    }

    outString = inLine.substr(0, pos);
    std::string remain_part = inLine.substr(pos+1);
    try {
        outNumber = stoi(remain_part);
    }
    catch (...) {
        return "Comma not followed by an integer.";
    }

    return "";
}

void ProcessLine(const std::string& inLine)
{
    std::string result;
    int num = 0;
    auto error = SplitLine(inLine, result, num);

    if (!error.empty()) {
        std::cout << "Error: " << error << std::endl;
    }
    else {
        std::cout << "Data string: " << result << " Data integer: " << num << std::endl;
    }

}

int main()
{
    ProcessLine("Ernest Hemingway 9");
    ProcessLine("Ernest, Hemingway, 9");
    ProcessLine("Ernest Hemingway, nine");
    ProcessLine("Ernest Hemingway, 9");

    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61981882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档