Shizu
Shizu

Reputation: 66

How to pass struct as parameter to other function for input, then output back at the original function

This is part of my project codes.

About the struct customer that I did, from welcomeScreen function I call the getInfo function for user to input the details (as you can see) and then return back the value to welcomeScreen function for output.

I can compile the codes, but the problem is there is no output for all the details after I input it (just blank)? Sorry if this is a dumb question cause im still a student.

struct customer
{
    string name;
    string email;
    int number;
};
void welcomeScreen(); //prototype
void getInfo(struct customer cust); //prototype

void welcomeScreen()
{
    struct customer cust; // struct declaration
    const int SIZE=5;

    system("CLS");
    cout << setfill ('-') << setw (55) << "-" << endl;
    cout << "\tWelcome to Computer Hardware Shop" << endl;
    cout << setfill ('-') << setw (55) << "-" << endl;

    cout << endl << "Type of hardwares that we sell:" << endl;
    string item[SIZE]={"Monitor","CPU","RAM","Solid-State Drive","Graphic Card"};
    for(int i=0;i<SIZE;i++)
        cout << "\t" << i+1 << ". " << item[i] << endl;

    getInfo(cust);

    cout << endl;
    cout << fixed << showpoint << setprecision (2);
    cout << "Name: "<< cust.name << endl; // struct output
    cout << "Email: "<< cust.email << endl;
    cout << "Phone Number: " << cust.number << endl;
    cout << endl;
}

void getInfo(struct customer cust)
{
    cout << endl << "Enter name: ";
    cin >> cust.name;
    cout << "Enter email: ";
    cin >> cust.email;
    cout << "Enter phone number: ";
    cin >> cust.number; 
}

Upvotes: 0

Views: 55

Answers (1)

user2191247
user2191247

Reputation:

You probably want to pass a pointer or a reference, in this case recommend a reference because it means fewer changes to your code:

void getInfo(struct customer &cust); //prototype

Remember to change your function parameter as well.

Upvotes: 2

Related Questions