处理用户以某种格式输入某些数据的任务(例如,“欧内斯特·海明威,9岁”),然后把这些信息放在一个表格里。
我必须考虑到用户输入中的错误。不要放逗号,不要放太多逗号,要检查",“后面的数据是不是有效的整数。
例如。欧内斯特·海明威9
错误:字符串中没有逗号。
欧内斯特,海明威,9岁
错误:输入中的逗号太多。
欧内斯特·海明威,九岁(输入我有问题)
错误:逗号后面没有整数。
欧内斯特·海明威,9岁
数据字符串:欧内斯特·海明威数据整数:9
下面是我当前的代码
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,如果这有助于为上面的代码提供上下文。
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“。
发布于 2020-05-24 05:41:38
您需要使用从std::string到整数的转换器。在C++中有几种方法可以做到这一点,但我认为使用stoi函数将涵盖所有情况(请注意,如果您尝试转换不是整数的字符串,stoi将抛出异常)。
下面是一个小示例,说明如何实现
#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;
}
https://stackoverflow.com/questions/61981882
复制