Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- // Enter any number of integers and
- // display how many numbers there are and the sum.
- int main()
- {
- vector<int> numbers; // The values entered.
- int sum = 0; // The sum of the values entered.
- string input; // Input.
- int current_number = 0; // Input converted to integer.
- cout << "Enter any number of integers, one at a time.\n"
- "Enter 'x' on a line by itself when you're finished." << endl;
- cin >> input; // Store input.
- while (input != "x") // Unless you're finished.
- {
- current_number = stoi(input); // Convert the input to an integer
- numbers.push_back(current_number); // Store the integer.
- sum += current_number; // Add the number to the total.
- cin.clear(); // Reset error flags.
- cin.ignore(100, '\n'); // Ignore up to the new line.
- cout << "Enter your next number: " << endl; // Display the sum.
- cin >> input; // Store input.
- }
- cout << endl; // Blank line.
- cout << "The sum of " << numbers.size()
- << " numbers is: " << sum << endl; // Display the sum.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement