Пользовательская строка и значение int в одной строке разделены пробелом - PullRequest
0 голосов
/ 12 октября 2019

В моем домашнем задании мой учитель хочет, чтобы я выполнил программу, которая рассчитывает, у кого больше всего долгов, и он сожалеет, что ввод должен быть в одной строке, например:

Андрей 4

Петр 5

И вот мой вопрос: как мне ввести строку и значение int в одну строку, разделенную пробелом.

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

using namespace std;

int main()
{
    //n is the number of names and k is number of how many debts i can delete but dont bother with that
    int n,k,SizeDebt;
    string str1;
    vector <string> Names;
    vector <int> Debt;

    do
    {
        cin >> n >> k;

    } while (n<1 || k>1000000);


    for (int i = 0; i < n; i++)
    {
        getline(cin, str1);
        cin >> SizeDebt;
        Names.push_back(str1);
        Debt.push_back(SizeDebt);

    }


    cin.get();
}

1 Ответ

0 голосов
/ 12 октября 2019
#include <cstddef>
#include <cstdlib>

#include <iostream>
#include <string>
#include <utility>


int main()
{
    using namespace std;
    pair<size_t, string> max_debt {};

    size_t n, k, debt {};
    string name;

    // use k

    cin >> n >> k;

    // Integral types (Boolean, Character and Integer) implicitly converts to the bool. 
    // For integrals that aren't equal to 0 result of the conversion is true, else - false.
    // So, when we write --n, that means to compare whether n is equal to zero, and then decrement n.

    while (n-- && cin >> name && cin >> debt)    // answer
        if (debt > max_debt.first)
            max_debt = make_pair(debt, name);

    cout << max_debt.second << " has max debt – " << max_debt.first << endl;
    return EXIT_SUCCESS;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...