database functions

This is a discussion on database functions within the C++ Programming forums, part of the General Programming Boards category; hi im struggeling with some database software its really siplified but im trying to get multipul entries better to show ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Exclamation 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.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    sorry by the way this is on c++ sorry if its in teh wrong section

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    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

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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?

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    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

    Code:
    tdata.enter(0);
    or somthing else

    hope this makes things clear

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

  8. #8
    0x01
    Join Date
    Sep 2001
    Posts
    88
    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);
    }
    Last edited by knave; 05-12-2007 at 10:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 02:09 PM
  3. Database / Tex
    By petermichaux in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2004, 09:28 PM
  4. trouble defining member functions
    By dP munky in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2003, 09:52 PM
  5. Replies: 1
    Last Post: 05-31-2002, 11:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21