-
database functions
hi im struggeling with some database software its really siplified but im trying to get multipul entries
better to show you
Code:
void people::enter(int i)
{
char a == "y";
while (a = "y");
{
cout << "Enter the name of the person ";
gets(np[i].name);
cout << "Enter the phone number of the person ";
gets(np[i].phone);
cout << "Enter the full address of the person ";
gets(np[i].address);
cout << "Enter the Email address of the person";
gets(np[i].email);
cout << "Enter the political party the person belongs to (conservative, labour, liberal, other)";
gets(np[i].party);
cout << "Do you wish to enter another member ? (y/n)";
cin >> a;
}
}
// end of enter member function
k this is the add member function
previously we were given a program which allowed two instances and the function had no loop
so in the main previously we used
Code:
tdata.enter(0);
tdata.enter(1);
this worked for two but how can i link this call fuction to my loop so i can have an infinet number of entries.
-
sorry by the way this is on c++ sorry if its in teh wrong section
-
Yes, this should be in the C++ section.
Whether using C or C++, gets() should not be used, since, among other things, usage of it can be exploited and your program taken over by a malicious user.
Another problem in your while() loop is that you have a = "y" as your condition. This is wrong for two reasons. First of all, a = "y" doesn't test if a is equal to char 'y'. Instead, it sets a to "y", and then tests if whatever value a is set to is true. It will be since in C and C++, every number resolves to true except 0. This means your loop will always be true because "y" is always resolved to be true.
Secondly, your whole way of assigning a to "y" both in the declaration of variable a and in the while loop is wrong because variable a is of type char, and you're assigning it a C string. When you use doublequotes around a character or a group of characters, the compiler will interpret them as a C-style string. Unless the compiler is smart and will correct your mistake, it'll take "y" as a C string of two chars: 'y' and '\0'. To be more strictly accurate, I would hope this would not even compile.
If I'm wrong on any of these points, I imagine someone will correct me.
-
k i fixed the 'y' problem and the gets() thing is ok coz its just a university assesment but i still need help on the other problem
-
Eventually, you'll need to write code properly. It's a good idea to learn how to do things right now while you're at university, so you'll not be used to bad habits that will end up hurting you in the long run.
Anyway, I don't understand what other problem you have. Are you asking how to have an unlimited number of people objects or whatever type np is?
-
k in my notes i called the add function by using
Code:
tdata.enter(0);
tdata.enter(1);
but this will ony allow me to add two sets of data to my database
i have used a loop with in my add function to add multipule sets of data
so my question is do i have to just use
or somthing else
hope this makes things clear
-
What type is np? I'm assuming it's an array of some struct type. If you're trying to make unlimited entries, you would need to increase the size of the array to however large you need it to be, or you would need to change it from an array to a vector, which would probably be the best way of doing it.
-
A guess.
Code:
int main()
{
unsigned short i = 0;
char a;
people tdata // ??
do
{
tdata.enter(i++);
cout << "Do you wish to enter another member ? (y/n)";
cin >> a;
}
while (a != 'n')
return 0;
}
...
Code:
void people::enter(int i)
{
cout << "Enter the name of the person ";
gets(np[i].name);
cout << "Enter the phone number of the person ";
gets(np[i].phone);
cout << "Enter the full address of the person ";
gets(np[i].address);
cout << "Enter the Email address of the person";
gets(np[i].email);
cout << "Enter the political party the person belongs to (conservative, labour, liberal, other)";
gets(np[i].party);
}