初学者C ++ - 做一些简单的计算

时间:2017-07-28 17:38:52

标签: c++ database sqlite

所以我试图做一些简单的数学计算,但我甚至无法在变量中加上数字。

int choice;
cout << "\nEnter your choice..." << endl;
cin >> choice;

if (choice == 1) {
    cout << "All entries:" << endl;

    sqlite::sqlite db( "student_marks.sqlite" ); 
    auto cur = db.get_statement();         
    cur->set_sql( "SELECT * FROM marks;" );
    cur->prepare(); 

    while( cur->step() )  {
        int apples = cur->get_text(2);
        int cherries = cur->get_text(3);
        int sumFruits = apples + cherries
    }
}

给我错误

error: no viable conversion from 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'int'

我该如何解决这个问题?如果我做

cout << "Bananas number : " << cur->get_text(3) << endl;

它打印出没有任何问题的值..

由于

1 个答案:

答案 0 :(得分:2)

您需要将std::string转换为int - 尝试使用stoi()

int apples = std::stoi(cur->get_text(2));
int cherries = std::stoi(cur->get_text(3));