每次在此函數中輸入類型都有錯誤時,它會自動將* _cost的值設置爲0.爲什麼會發生這種情況?使用getline和cin指定用戶輸入
void Item::setCost(string input){
float entered;
istringstream stin;
stin.str(input);
if(!(stin >> entered)){
do{
cout << "invalid input" << endl;
stin.clear();
getline(cin, input);
stin.str(input);
*_cost = entered;
}
while(!(stin >> entered));
}
else{
*_cost = entered;
}
}
我使用的功能在我的主要功能如下:
istringstream stin;
string input;
cout << "enter cost" << endl;
getline(cin, input);
items[i]->setCost(input);
_'我是一個新的C++程序員。'_這個病,只是浪費帶寬!我們通過查看你的問題來了解我(我今天真的很**脾氣暴躁,因爲我喜歡浪費帶寬)...... –