Thread: about c program

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    about c program

    Hi,

    I am making a program a Teller class for queue and it is class which has a Teller, customer and a counter .

    Code:
    class Teller
    {
    
    public:
    
    Teller();//constructor/
    
    ~Teller();//destructor/
    
    int enqueue();//to add customer to queue/
    
    int dequeue();//to remove customer from the queue/ 
    
    
    private;
    
    
    
     int *front; //front pointer for the queue/
     
     int *rear; //rear pointer for the queue/
     
     
     Teller *nextptr; //next pointer/
    
     int node;//node variable for customer/
    
     int newNode;// node for the new customer/
    
    };
    
    
    
    
    main()
    
    
    Teller teller; //object for class Teller/
     
     { 
     teller.enqueue()
     
     
     newNode->setnext(Null);
     
     rear->setnext(newNode);
     
     rear=newNode; 
    
    }
    --------------------------------------------------------------------------

    I have just created one object "teller" for this class and this object is basically a counter. I have used the method "enqueue()" to add customers to this counter(teller).

    I wanted to know that for the object "teller" ,if we use method "enqueue()" to add customers to the queue, can we define its return type if we pass an argument of "int node" to it?



    Thanks
    Last edited by student111; 11-27-2013 at 11:25 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can make its return type anything you want it to be, unless you have a spec sheet that says otherwise. What do you want it to be?

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    thanks for the reply,

    I want to pass "int node" as an argument to this method "enqueue()" but ,it must also have a return type, but when I write it as:

    "int teller.enqueue(int node);"

    and when i try to compile it, the compiler gives an error message of syntax error. I am not able to understand how I should define its return type.
    Last edited by student111; 11-27-2013 at 11:57 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You specify the return type and the parameters when you define the function, not when you call it. After all, look at these lines from your code:
    Code:
    newNode->setnext(Null);
      
     rear->setnext(newNode);
    Do you see any "int" or anything like that in those calls?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Quote Originally Posted by tabstop View Post
    You specify the return type and the parameters when you define the function, not when you call it. After all, look at these lines from your code:
    Code:
    newNode->setnext(Null);
      
     rear->setnext(newNode);
    Do you see any "int" or anything like that in those calls?
    I am using these nodes as I want to implement "Queues" using linked list.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by student111 View Post
    I am using these nodes as I want to implement "Queues" using linked list.
    I don't at all want you to focus on why those lines are there. I want you to focus on what they look like -- the syntax you use when you call a function.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Sir, I have declared the "int node" and "int newNode" in this program , I have seen some of the linked list programs which are written this way. I basically want to set the next pointer of newNode as Null and want to join the new node to the queue list.

    But can you tell me what exactly is the mistake I am making when using enqueue method?

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    when I compile this program ,it gives me some errors including the error on line where I have
    newNode->setnext(Null);

    Also how do I define or declare a null pointer?
    Last edited by student111; 11-28-2013 at 06:06 AM.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Don't modify the first post, it makes it hard to track the conversation.

    Some problems:
    *main needs a return type, namely int.
    *Teller teller is currently declared as if it were a K&R style arguement to main; that's not where it should be.
    *A queue is not a node; it doesn't have a pointer to the next queue. A queue may contain nodes, but they should be of a different type then the queue.
    *enque should logically take as parameters what you intend to enqueue
    *when you access a member of a class, you need to do it throught the instance of the class. In this case the syntax would look like "teller.rear" for instance.
    *main only call enque and deque. nodes and other private variables can and should only be modified by class methods.
    *nullptr is the null pointer constant in C++11. You can use the literal 0 for earlier versions of C++, but you should learn C++11.

    And remember, that this is an academic exercise, and most of the time when you want a queue, you should use std::deque
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by King Mir
    most of the time when you want a queue, you should use std::deque
    Nah, use std::queue if you want a queue, and std::deque if you want a double ended queue. It so happens that std::queue will adapt a std::deque by default, but the type and hence the interface signals the intention.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Good point. I had forgotten about that container adapter.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM