Function Argument Help

This is the program I'm suppose to build and I'm taking it small steps at a time but I ran into a issue and that's in the main function when I try to call the getinfo() function I'm not sure what argument to place here considering my parameter for that function is a reference. So what could I possibly do to make this function run properly? I'm not too familiar with reference arguments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
The program should use an array of at least 10 structures,
it should let the user enter data into the array,
change the contents of any element, and display all the data
stored in the array. The program should have a menu-driven user
interface.

Input validation: when the data for a new account is entered,
be sure the user enters data for all the field. No negative
account balances should be entered.

Add a function that allows the user to search the structure
array for a particular customer’s account. It should accept part
of the customer’s name as an argument and then search for an
account with a name that matches it. All accounts that match
should be displayed. Of no account matches, a message saying
so should be displayed.
*/


#include <iostream>
#include <string>

using namespace std;

struct Customer{
    string Name;
    string Address;
    string City;
    string State;
    string ZIP;
    string Pnumber; //phone number
    double aBalance; //account balance
    string DataOfLastPaym;
};

void getInfo(Customer&);
void showInfo(Customer);
void Search();

const int SIZE = 10;
Customer info[SIZE] = {};

int main()
{
    int answer;

    // Display a menu.
    cout << "---------- MENU ----------------------" << endl;
    cout << "1. Enter new account information\n";
    cout << "2. Change account information\n";
    cout << "3. Display all account information\n";
    cout << "4. Exit the program\n\n";
    cout << "--------------------------------------" << endl;
    cout << endl;

    cout << "Choose 1-4: ";
    cin >> answer;
    cout << endl;

    while(answer != 4){
        switch(answer){
        case 1:{
            //new account info.
            getInfo(); //what should I put for the argument?
        }
        break;
        case 2:{
            //change account info.

        }
        break;
        case 3:{
            //display info
            //showInfo();
        }
        break;
        default: break;

        }
    }

    return 0;
}

// Function getInfo gets account information from the user
//and stores it in the parameter, which is a Customer structure
//reference variable.
void getInfo(Customer &a){
    cout << "Enter customer's name: ";
    getline(cin,a.Name);
    cout << "Address: ";
    getline(cin,a.Address);
    cout << "City: ";
    cin >> a.City;
    cout << "State: ";
    cin >> a.State;
    cout << "ZIP: ";
    cin >> a.ZIP;
    cout << "Phone Number: ";
    cin >> a.Pnumber;
    cout << "Account Balance: ";
    cin >> a.aBalance;
    cout << "Date of Last Payment: ";
    getline(cin,a.DataOfLastPaym);
}

// Function showInfo displays the customer's name, address,
//account balance, and date of last payment.
//The information is passed into the c structure.
void showInfo(Customer display){
    cout << "\n --- Data for " <<display.Name <<" ---\n";
	cout << "\nAddress:\t" << display.Address << ", " << display.City << ", " << display.State << ", " << display.ZIP;
	cout << "\nTelephone:\t" << display.Pnumber;
	cout << "\nLast Payment:\t" << display.DataOfLastPaym;
	cout << "\nAccount balance:\t" << display.aBalance << endl;
}

// Search the array for account
/*void Search(string theName){
    for(int i = 0; i < SIZE; i++){
        if(theName == info[i]){ //issue here
            //enter what to do if account found
            cout << "Name: " << info[i].Name << endl;
            cout << "Address: "<< info[i].Address << endl;
            cout << "City: " << info[i].City << endl;
            cout << "State: " << info[i].State << endl;
            cout << "ZIP: " << info[i].ZIP << endl;
            cout << "Phone Number: " << info[i].Pnumber << endl;
            cout << "Account Balance: " << info[i].aBalance << endl;
            cout << "Date of Last Payment: " << info[i].DataOfLastPaym << endl;
            break;
        }else{
            cout << "Account not found" << endl;
        }
    }
}
*/
You a few errors. The first thing I notice is your mixing inputs and outputs in a output function.

1
2
3
    
cout << "Enter customer's name: ";
getline(cin,a.Name);


Not sure if it even works, but if it does, it is not great to write code that way. Here is some help. Hopefully this gets your started.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

using namespace std;

struct Customer{
    string Name;
    string Address;
    string City;
    string State;
    string ZIP;
    string Pnumber; //phone number
    double aBalance; //account balance
    string DataOfLastPaym;
};


void getInfo(Customer &a){
    cout << "Enter customer's name: " + a.Name << endl;
    cout << "Address: " + a.Address << endl;
    // etc...
    cout << "City: " << endl;
    cout << "State: "<< endl;
    cout << "ZIP: "<< endl;
    cout << "Phone Number: "<< endl;
    cout << "Account Balance: "<< endl;
    cout << "Date of Last Payment: "<< endl;
}

int main()
{

    // Array of 50 customers
    Customer aArrayOfCustomers[50];

    aArrayOfCustomers[0].Name = "Some name";
    aArrayOfCustomers[0].Address = "Some address";
    // etc...

    getInfo(aArrayOfCustomers[0]);


    return 0;

}



Enter customer's name: Some name
Address: Some address
City: 
State: 
ZIP: 
Phone Number: 
Account Balance: 
Date of Last Payment: 


hope that helps. Let us know if you have any questions.
Last edited on
I rearranged my code and now it works... but not fine. When I try to display the info (pressing 3) it runs infinite. Also when I press 1 it does it's job but this runs infinite as well. Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
The program should use an array of at least 10 structures,
it should let the user enter data into the array,
change the contents of any element, and display all the data
stored in the array. The program should have a menu-driven user
interface.

Input validation: when the data for a new account is entered,
be sure the user enters data for all the field. No negative
account balances should be entered.

Add a function that allows the user to search the structure
array for a particular customer’s account. It should accept part
of the customer’s name as an argument and then search for an
account with a name that matches it. All accounts that match
should be displayed. Of no account matches, a message saying
so should be displayed.
*/


#include <iostream>
#include <string>

using namespace std;

struct Customer{
    string Name;
    string Address;
    string City;
    string State;
    string ZIP;
    string Pnumber; //phone number
    double aBalance; //account balance
    string DataOfLastPaym;
};

void EnterChangeAccInfo(Customer&);
void showInfo(Customer);
//void Search();

const int SIZE = 10;

int main()
{
    int answer;

    //here I started to think, maybe I need to have parts of the info initialized?
    Customer aArrayOfCustomers[SIZE];

    aArrayOfCustomers[0].Name = "Jake Smith";
    aArrayOfCustomers[0].Address = "888 Forest Hill";
    aArrayOfCustomers[0].City = "Peoria";
    aArrayOfCustomers[0].State = "IL";
    aArrayOfCustomers[0].ZIP = "61550";
    aArrayOfCustomers[0].Pnumber = "888-712-9881";
    aArrayOfCustomers[0].aBalance = 800.25;
    aArrayOfCustomers[0].DataOfLastPaym = "08/15/2011";


    // Display a menu.
    cout << "---------- MENU ----------------------" << endl;
    cout << "1. Enter new account information      |\n";
    cout << "2. Change account information         |\n";
    cout << "3. Display all account information    |\n";
    cout << "4. Exit the program                   |\n";
    cout << "--------------------------------------" << endl;
    cout << endl;

    while(answer > 4 || answer < 1){
    cout << "Choose 1-4: ";
    cin >> answer;
    }
    cout << endl;

    while(answer != 4){
        switch(answer){
        case 1:{
            //new account info.
            EnterChangeAccInfo(aArrayOfCustomers[0]);

        }
        break;
        case 2:{
            //change account info.

        }
        break;
        case 3:{
            //display info
            showInfo(aArrayOfCustomers[0]); // when I display info it runs infinite?

        }
        break;
        default: break;

        }
    }

    return 0;
}

// Function getInfo gets account information from the user
//and stores it in the parameter, which is a Customer structure
//reference variable.
void EnterChangeAccInfo(Customer &a){
    cout << "Enter customer's name: ";
    cin.ignore();
    getline(cin,a.Name);
    cout << "Address: ";
    getline(cin,a.Address);
    cout << "City: ";
    cin >> a.City;
    cout << "State: ";
    cin >> a.State;
    cout << "ZIP: ";
    cin >> a.ZIP;
    cout << "Phone Number: ";
    cin >> a.Pnumber;
    cout << "Account Balance: ";
    cin >> a.aBalance;
    cout << "Date of Last Payment: ";
    cin.ignore();
    getline(cin,a.DataOfLastPaym);
}

// Function showInfo displays the customer's name, address,
//account balance, and date of last payment.
//The information is passed into the c structure.
void showInfo(Customer display){
    cout << "\n --- Data for " <<display.Name <<" ---\n";
	cout << "\nAddress:\t" << display.Address << ", " << display.City << ", " << display.State << ", " << display.ZIP;
	cout << "\nTelephone:\t" << display.Pnumber;
	cout << "\nLast Payment:\t" << display.DataOfLastPaym;
	cout << "\nAccount balance:\t" << display.aBalance << endl;
}

// Search the array for account
/*void Search(string theName){
    for(int i = 0; i < SIZE; i++){
        if(theName == info[i]){ //issue here
            //enter what to do if account found
            cout << "Name: " << info[i].Name << endl;
            cout << "Address: "<< info[i].Address << endl;
            cout << "City: " << info[i].City << endl;
            cout << "State: " << info[i].State << endl;
            cout << "ZIP: " << info[i].ZIP << endl;
            cout << "Phone Number: " << info[i].Pnumber << endl;
            cout << "Account Balance: " << info[i].aBalance << endl;
            cout << "Date of Last Payment: " << info[i].DataOfLastPaym << endl;
            break;
        }else{
            cout << "Account not found" << endl;
        }
    }
}
*/
hello,
a bad problem: info is an struct, not string
this function would be better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Search(string theName){
    for(int i = 0; i < SIZE; i++){
        if(theName == info[i].Name){ //issue here
            //enter what to do if account found
            cout << "Name: " << info[i].Name << endl;
            cout << "Address: "<< info[i].Address << endl;
            cout << "City: " << info[i].City << endl;
            cout << "State: " << info[i].State << endl;
            cout << "ZIP: " << info[i].ZIP << endl;
            cout << "Phone Number: " << info[i].Pnumber << endl;
            cout << "Account Balance: " << info[i].aBalance << endl;
            cout << "Date of Last Payment: " << info[i].DataOfLastPaym << endl;
            break;
        }else{
            cout << "Account not found" << endl;
        }
    }
}

if any problem, tell here
I'll focus on that issue after I find out the solution to my first couple problems.
When I try to display the info (pressing 3) it runs infinite. Also when I press 1 it does it's job but this runs infinite as well.
When I try to display the info (pressing 3) it runs infinite. Also when I press 1 it does it's job but this runs infinite as well.

The loop is infinite, no matter the choice. answer does not change inside the loop, therefore the loop condition never changes.
Topic archived. No new replies allowed.