C ++根据用户输入添加到矢量

时间:2018-04-24 02:07:16

标签: c++ vector reference cin

所以我尝试将数据输入std::vector,但我特意尝试将用户放入y的数据分配到向量中的y位置。

例如,如果用户输入1,3,它会将值1放入向量中的第3个位置。

出于某种原因,我似乎无法克服这个错误,有什么建议吗?

#include <string>
#include <vector>
#include <utility>

int main()
{
    vector<int> poly1(100);
    vector<int> poly2(100);
    int x;
    int y;

    int choice;

    std::cout << "What do you wish to do?" << std::endl;
    std::cout << "1. Add two polynomials" << std::endl;
    std::cout << "2. Multiply two polynomials" << std::endl;
    std::cout << "3. Evaluate one polynomial at a given value" << std::endl;
    std::cout << "4. Find Coefficent for a given polynomial and given exponent" << std::endl;
    std::cout << "5. Find the leading exponent for a given polynomial" << std::endl;
    std::cout << "6. Exit "<< std::endl;

    std::cin >> choice;

    if (choice < 1 || choice > 6)
    {
        do
        {
            std::cout << "Invalid entry: please reenter choice" << std::endl;
            std::cin >> choice;

        } while (choice < 1 || choice > 6);
    }

    if (choice == 1)
    {
        std::cout << "Please input the first polynomial in the form of: (non-zero coefficient, exponent) pairs" << std::endl;
        std::cin >> x >> y;

        poly1.at[y] = x;
        std::cout << "done" << std::endl;

        system("pause");
    }
    if (choice == 2)
    if (choice == 3)
    if (choice == 4)
    if (choice == 5)
    if (choice == 6)

    system("pause");   
}

0 个答案:

没有答案