简体   繁体   English

整数向量的和

[英]Sum of a vector of integers

I created a program that let's the user input up to 10 integers and then it will print out the sum of the inputted integers.我创建了一个程序,让用户输入最多 10 个整数,然后它会打印出输入整数的总和。 However I want to stop the program when the user tries to input more than 10 integers or when the user types in "DONE".但是,当用户尝试输入超过 10 个整数或用户输入“DONE”时,我想停止程序。 I didn't really get it to work because when the user types in something other than an integer (including the string "DONE"), it will stop.我并没有真正让它工作,因为当用户输入 integer 以外的东西(包括字符串“DONE”)时,它会停止。 I only want it to stop when the user types in "DONE".我只希望它在用户输入“完成”时停止。 If the user types in something like "Hello" I want the program to crash.如果用户输入诸如“Hello”之类的内容,我希望程序崩溃。

This is how it should look:它应该是这样的:

Enter up to 10 numbers:

12
12
4
DONE

The sum of the integers is: 28

What's wrong with my code?我的代码有什么问题? I am referring to my void read_numbers function.我指的是我的 void read_numbers function。 As I mentioned, I want it to stop ONLY when the user inputs the string "DONE" or when the user inputs more than 10 integers.正如我所提到的,我希望它仅在用户输入字符串“DONE”或用户输入超过 10 个整数时停止。

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Number_Type {
  vector<int> numbers;
};

void read_numbers(Number_Type& number) {
  int digit{};
  string line;
  // char space = ' ';

  while ((line != "DONE") && (number.numbers.size() != 10)) {
    cin >> digit;
    getline(cin, line);
    number.numbers.push_back(digit);
  }
}

int number_sum(Number_Type const& number) {
  int sum{};

  for (int i{}; i < number.numbers.size(); ++i) {
    sum += number.numbers[i];
  }

  return sum;
}

int main() {
  Number_Type number;
  cout << "Enter up to 10 numbers: " << endl;
  read_numbers(number);
  cout << "The sum of the integers is: " << number_sum(number);
}

Fist you have to read all your inputs as strings.首先,您必须将所有输入读取为字符串。 Then you can use std::from_chars to check if your input was a valid integer (without having to do exception handling, which std::stoi will need).然后您可以使用 std::from_chars 来检查您的输入是否是有效的 integer (无需进行异常处理,这需要 std::stoi )。 Example here (live demo https://onlinegdb.com/UANchB1mn )此处示例(现场演示https://onlinegdb.com/UANchB1mn

#include <iostream>
#include <string>
#include <vector>
#include <charconv>

auto read_numbers(const std::size_t max_numbers)
{
    std::vector<int> values;
    int value{};
    std::string input;

    do
    {
        std::cout << "Enter number " << values.size() + 1 << " of " << max_numbers << " (or DONE to stop) : ";
        std::cin >> input;

        // std::stoi is an option but then you would need do exception handling
        auto result = std::from_chars(input.data(), input.data() + input.size(), value);
        if (result.ec == std::errc())
        {
            values.push_back(value);
        }
    }
    while ((input != "DONE") && (values.size() < max_numbers));

    return values;
}

int main()
{
    auto values = read_numbers(3);

    std::cout << "you entered : ";
    for (const auto value : values)
    {
        std::cout << value << " ";
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM