Thread: output of struct? confused....

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    103

    output of struct? confused....

    The output makes no sense to me, am i recording the info into the struct wrong?

    i changed from struct customer makeCustomer to void; the one that returns a customer outputs values just as weird, with no name output, time is 0, and items is a huge number

    Code:
    struct customer {
       char name[30];
       int items;
       int time;
    };
    
    void makeCustomer(struct customer a,char n[30], int i, int t) {
       strcpy(a.name,n);    
       a.items=i;
       a.time=t;
    }
    
    int main() {
       struct customer test;
       char b[30];
       b[0]='j';
       b[1]='a';
       b[2]='s';
       int items=5;
       int time=60;
       makeCustomer(test,b,items,time);
       printf("Name: %s--Items: %d--Time: %d\n",test.name,test.items,test.time);
       system("pause"); 
    }
    
    OUTPUT: Name: H*b--Items: 236--Time: 4602016

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Soulzityr
    The output makes no sense to me, am i recording the info into the struct wrong?
    In a way. The problem is that makeCustomer() operates on a copy of the object passed to it. You should pass the address of the object that you wish makeCustomer() to operate on, i.e., a should be a pointer to struct customer (and you should use a name that is more descriptive than a). Of course, you need to change the implementation of the function accordingly.
    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

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    so i have to make everything pointers, including in main????

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Soulzityr
    so i have to make everything pointers, including in main?
    No. Recall the use of the address-of operator, e.g., &test results in a pointer to test.
    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

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    Code:
    void makeCustomer(struct customer &a,char n[30], int i, int t) {
       strcpy(a.name,n);    
       a.items=i;
       a.time=t;
    }
    Heh my output now works, thanks ois this what you meant though?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, no. That is the use of a reference parameter in C++, but you are writing a C program. I suggest that you check to make sure that you are compiling as C, otherwise you could get very confused.
    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

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    oh you are right, i had it saved as cpp file, and now youre right, compile says its an error :P

    hmm how do i access address for function then with the & or w/e >.<

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should do something like this:
    Code:
    void makeCustomer(struct customer *a, char n[], int i, int t) {
       strcpy(a->name, n);
       a->items = i;
       a->time = t;
    }
    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

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    ugh sorry i never learned this well at all; so i made my method look at yours and i tried various ways of calling it

    havnig

    struct customer* test2

    and then makeCustomer(test2...) crashes the program

    struct customer test2; then makeCustomer(test2...) doesnt run cuz of argument error which makes sense

    then struct customer test2 and makeCustomer(*test2...) also fails. sorry haha i am not quite sure on how to write the syntax >.<

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall my previous post about using the address-of operator:
    Code:
    makeCustomer(&test, b, items, time);
    By the way, time is not a good variable name to use since it can conflict with a function named time from <time.h>.
    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
    Jan 2010
    Posts
    103
    ahh hmm i will change it then, thanks as for what you said before about address-of operators, it sort of passes over my head; im sorry >.< my understanding of it is kinda fuzzy......and what it means in terms of when icall it

    EDIT: hmm nvm...i think i got it...going to test the output

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    heheh sorry thanks for helping me with that it works now

    i tried the same thing with my enqueue method but it doesnt work. is it okay if i ask you for help with that? >.<

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  4. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM