2017-07-28 11 views
-4

を行うので、私はいくつかの簡単な数学の計算をやろうとしているが、私はしても、変数に数値を置くことができません。初心者C++ - いくつかの簡単な計算

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; 

を行う場合、それは何の問題もなく値を出力します..あなたはintにstd::stringを変換する必要が

おかげ

+7

読みますエラーメッセージ。 –

+2

'int apples = cur-> get_text(2);'あなたは何が起こると思いますか? – DimChtz

+0

私は 'cur-> get_text(0)'をintに変換する必要がありますか? –

答えて

2

からstoi()を使用してみてください:

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

私はこれを 'std :: string'からの実行不可能な変換( 'basic_string 、アロケータ>')を 'int' 'にします。 –

+0

あなたは 'リンゴ'とチェリー?ああ、私。 – LogicStuff

+0

はい、もちろん - –

関連する問題