dimanche 6 août 2017

Passing value to a fucntion c++

I've been using global variables up until now...I just remembered that it's not a good practice..so, is there any way I can change this? should I pass the variables as value or reference?

this is the code http://ift.tt/2vBPjva

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();
    }

Aucun commentaire:

Enregistrer un commentaire