Thread: Passing argumets to function gives error. Why????

  1. #1
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21

    Question Passing argumets to function gives error. Why????

    Hi, I'm having this problem with passing arguments to functions.
    You don't need to give me the code , just say what did I did wrong and maybe an example of how the thing goes.

    The compiler alerts me that there is one error.

    Here's the code:

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class User{
          public:
                 string k_Name;
                 int k_Free;
          };
    
    void Start_Meni(User *array[]);
    void Creation(User *array[]);
    int Free_Acc_Chk(User *array[]);      
    
    int main(){
        User *p_array = new User[3];
        p_array[0]->k_Free = 1;  //I can't initialise in the class itself..?
        
        Start_Meni(p_array);
        
        system("pause");
        return 0;
        }
    
    void Start_Meni(User p_array){
         int opc;
         do{
             cout<<"(1) Create account"<<endl;
             cout<<"(2) Account deleting"<<endl;
             cout<<"(3) Account access"<<endl;
             cout<<">> ";
             cin>>opc;
             switch(opc){
                         case 1:
                              Creation(p_array);
                              break;
                         case 2:
                              cout<<"Deleting account..."<<endl;
                              break;
                         case 3:
                              cout<<"Accessing account..."<<endl;
                              break;
                         default:
                                 cout<<"Non-existing option..."<<endl;
                         }
             }while(opc!=0);
         }
    
    void Creation(User p_array[]){
         int op = 1,poz;
         string name;
         if(Free_Acc_Chk(p_array)!= 6){
                                  poz = Free_Acc_Chk(p_array);
                                  
                                  cout<<endl;
                                  do{
                                             cout<<"Enter Your name: ";
                                             cin>>name;
                                             cout<<endl;
                                             for(int j=0;j<5;j++){
                                                     if(p_array[j]->k_Name == name){
                                                                    cout<<"You must change the name!"<<endl;
                                                                    op = 0;
                                                                    }
                                                     }
                                  }while(op=0);
                                  p_array[poz]->k_Name = name;
                                  p_array[poz]->k_Free = 0;
                                  cout<<"New account succesfully opened!"<<endl;
                                  }
         else cout<<"Sorry, no more free accounts..."<<endl;
         }
    
    
    int Free_Acc_Chk(User array[]){
         for(int x=0;x<5;x++){
                 if(p_array[x].k_Free != 0){
                                      return x;
                                      }
                 else return 6;
         }
    }

    Now this is the simplified version of real thing but the problem is same.
    I'm doing some mistake about functions arguments and passing.
    Please explain this to me.

    P.S. Sorry for bad english!
    Please don't throw rocks at me !

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    void Start_Meni(User *array[]); //declaration
    void Start_Meni(User p_array)  //definition
    Start_Meni(p_array); //call
    All these disagree what's the type of the parameter (pointer to pointer to object, plain object, pointer to object.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21
    But why the other functions O.K?
    Please don't throw rocks at me !

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The other functions have the same problem.

    If you want to make an dynamically allocated array of Users, you might use a std::vector<User> instead.

    If you prefer your way, you'll need to understand that X* p[] and X p[] are two different things (and you'll need the second in your case).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21
    OK, I used pointers, but in the beggining I tried with array of objects. It still gave me some errors.
    If you want to make an dynamically allocated array of Users, you might use a std::vector<User> instead.
    Vector. Listen I'm just a noob.
    I didn't understand this well, I can't now start lering that.

    So I used pointer operator "value of" in declaration and I shoudn't do it?
    Or should I use it in all declaration/definition/call.
    Please don't throw rocks at me !

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Vector. Listen I'm just a noob.
    vectors should really be learned first if you are new to C++.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, for one thing the Start_Menu function is misnamed The program spends almost all of the time "starting menu". If you like this kind of design, you might actually create the array of users in Start_Mani (renamed to reflect more accurately what it does - like Run) and not need to pass the pointer around.

    For another thing you seem to allocate 3 User objects, but then have magic values like 5 and 6 around referring to the size of the array(?). You should first make the size a (const) variable. Then you'll also need a variable to tell you how many users you have so far.

    If it is school work and you are not allowed to use a std::vector, then too bad but a vector is basically nothing else that a "smart" array and much easier to use than arrays created with new.

    It can't be too hard to learn, either.

    Code:
        std::vector<User> data; //create a container (vector) of User objects.
        data.push_back(some_user); //add a user to the container, 
                                   //no need to worry about having the space
    
        for (unsigned i = 0; i < data.size(); ++i) // loop throught the whole container
            std::cout << data[i].name << '\n'; //access a container element, just like array
    etc
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Argo Argo_Jeude's Avatar
    Join Date
    Jul 2005
    Location
    Bihać, Bosnia
    Posts
    21
    OK I'll need to see more about that "thingy" Vector. Some reading might help.
    I'll see, I'll see...

    Oh, and , it's not a school project, just something I've been working on.
    (badly)
    Please don't throw rocks at me !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM
  4. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM