Reputation: 135
I need a help with proper creating function and then passint it to function. I had a piece of code like this:
struct phones {
char lastname[15];
char number[8];
char city[15];
} *list;
void enter(phones[], int);
int main()
{
int n;
cout << "Enter the number of users: ";
cin >> n;
list = new phones[n];
enter(list, n);
system("pause");
delete[]list;
return 0;
}
void enter(phones list[], int n) {
for (int i = 0; i < n; i++)
{
cout << "User number " << i + 1 << endl;
cout << "Enter user's lastname ";
cin >> list[i].lastname;
cout << "Enter user's number: ";
cin >> list[i].number;
cout << "Enter user's city: ";
cin >> list[i].city;
}
}
My teacher told me that it's not good to create a structure using *list
right in it.
Now i have a question how to create the structure properly, declare a size from user's input and then pass it to function.
Upvotes: 0
Views: 61
Reputation: 82
You could also eliminate the problem in your program by preceding a typedef before struct and using the List
example as follow:
typedef struct Person {
char lastname[15];
char number[8];
char city[15];
} * List;
void enter(List list, int n) ;
void show(List list, int n);
int main()
{
int n;
cout << "Enter the number of users: ";
cin >> n;
List users = new Person[n];
enter(users, n);
cout << "-- Displaying users: --" << endl;
show(users, n);
system("pause");
delete[] users;
return 0;
}
void show(List list, int n)
{
for (int i = 0; i < n; ++i) {
cout << "[User number " << (i + 1) << "]" << endl;
cout << "Lastname: " << list[i].lastname << endl;
cout << "Number: " << list[i].number << endl;
cout << "City: " << list[i].city << endl;
}
}
void enter(List list, int n) {
for (int i = 0; i < n; i++)
{
cout << "User number " << i + 1 << endl;
cout << "Enter user's lastname ";
cin >> list[i].lastname;
cout << "Enter user's number: ";
cin >> list[i].number;
cout << "Enter user's city: ";
cin >> list[i].city;
}
}
Upvotes: 0
Reputation: 8043
Just for fun, I rewrote your program, including a function to display the read data. This is how I would have it:
#include <cstdlib>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
struct User {
char lastname[15];
char number[8];
char city[15];
};
void enter(User users[], int n)
{
for (int i = 0; i < n; ++i) {
cout << "[User number " << (i + 1) << "]" << endl;
cout << "Enter user's lastname: ";
cin >> users[i].lastname;
cout << "Enter user's number: ";
cin >> users[i].number;
cout << "Enter user's city: ";
cin >> users[i].city;
}
}
void show(User users[], int n)
{
for (int i = 0; i < n; ++i) {
cout << "[User number " << (i + 1) << "]" << endl;
cout << "Lastname: " << users[i].lastname << endl;
cout << "Number: " << users[i].number << endl;
cout << "City: " << users[i].city << endl;
}
}
int main()
{
int n;
cout << "Enter the number of users: ";
cin >> n;
User* users = new User[n];
enter(users, n);
cout << "-- Displaying users: --" << endl;
show(users, n);
system("pause");
delete[] users;
return 0;
}
Upvotes: 1
Reputation: 37227
You can always define variables after declaring a struct
:
phones *list;
C++ doesn't have VLA so either use dynamic allocation, which is disallowsled according to your description, or use the special stuff in C++: STL.
Create a vector of struct phones
can make it very simple:
#include <vector>
// After input number
std::vector<phones> list(n);
And you won't have to bother with new
s and delete
s.
Upvotes: 1