-1
到目前爲止,我一直在使用全局變量。我只記得這不是一個好習慣。那麼,有什麼辦法可以改變這個嗎?我應該通過變量作爲值還是參考?將值傳遞給C++函數
這是代碼 https://pastebin.com/JZaaR2Qd
using namespace std;
string user_name;
string str_age;
unsigned short int user_age;
char yes_no_prompt;
void user_biodata_input()
{
cin.clear();
cout << "Name : "; getline(cin >> ws, user_name);
cout << "Age : "; getline(cin, str_age); //taking age as a string
stringstream(str_age) >> user_age ; //extract int from a string
//Check if user input is not numeric, if so, repeat
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Input invalid! Please re-enter your age in numeric..." << endl;
cin >> user_age;
}
}
int main()
{
//Speed up cin and cout
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//Start
cout << "Hi User, my name is Sirius. I'm your Digital Guidance (DG), nice to meet you..." << endl;
cout << "Please provide your data..." << endl;
user_biodata_input();
show_user_biodata();
while (yes_no_prompt == 'N' || yes_no_prompt == 'n')
{
cout << "Please re-enter your biodata..." << endl;
user_biodata_input();
show_user_biodata();
}
當然,你可以通過引用傳遞。你有什麼問題? –