Thread: Program stops executing

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    18

    Program stops executing

    Code:
    // new code down below

    Hope the above isnt too much code to put here, but after I try to exit the loop early and return the structure in the getData function, the program just stalls. Can somebody please point me in the right direction.

    p.s is the way im deleting p the right way to delete the allocated array?
    Last edited by bargomer; 04-25-2008 at 06:26 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This doesn't compile as is (static keyword misused).

    I would also suggest turning compiler warnings on.
    The most alarming part is this:
    Code:
    Store	*d, *e;
    *d = getData(size, loopie);
    menu(d, loopie);
    *e = reData(d,loopie);
    menu(e,loopie);
    What you are doing here is declaring two uninitialized pointers, then you dereference d to assign a value returned by getData and pass the uninitialized pointer to another function, and repeat the same error.
    Instead getData should return a pointer to the block of memory it allocated. reData probably doesn't need to return the pointer because it is just modifying the array contents?
    All in all this part should look something like this:
    Code:
    Store	*d;
    d = getData(size, loopie);
    menu(d, loopie);
    reData(d, loopie);
    menu(d, loopie);
    Other than this error the indentation leaves much to desire and the architecture is weird. When I look at main, I see this:
    Code:
    int main()
    {
    greeting();
    return 0;
    
    }
    So, the only thing this program does is greet the user?

    Another major weirdness is this:
    Code:
    void display(Store p[], int &loopie)
    {
    //... snip ...
    delete [] p;
    }
    A display function that destroys the data it displays?!
    If possible, the function that allocates the memory should also delete it. Assuming, greeting returns the desired size for the array, your main program might look like this:
    Code:
    int main()
    {
        //ask user, how many Stores they want
        int max_size = Greetings();
    
        //main allocates memory, so we needn't guess which function should deallocate
        Store* stores = new Store[max_size];
    
        //fill it up with data
        int entry_count = GetData(stores, max_size);
    
        //run menu, which allows user to modify and display the entries
        Menu(stores, entry_count);
    
        //main deallocates resources and exits
        delete [] stores;
        return 0;
    }
    You'll need to change your function prototypes for that.
    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
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I find this odd?
    Code:
    void display(Store p[], int &loopie)
    {
    //... snip ...
    delete [] p;
    }
    It is passed an array not a pointer. How is that working?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Raigne View Post
    I find this odd?
    Code:
    void display(Store p[], int &loopie)
    {
    //... snip ...
    delete [] p;
    }
    It is passed an array not a pointer. How is that working?
    When arrays are passed to functions, they are ALWAYS turned into pointers, whether that is explicit (Store *p) or implicit (Store p[]).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    What you are doing here is declaring two uninitialized pointers, then you dereference d to assign a value returned by getData and pass the uninitialized pointer to another function, and repeat the same error.
    Instead getData should return a pointer to the block of memory it allocated. reData probably doesn't need to return the pointer because it is just modifying the array contents?
    All in all this part should look something like this:
    Code:
    Store	*d;
    d = getData(size, loopie);
    d will have the address of the returned value(the array of structures) ?

    Other than this error the indentation leaves much to desire and the architecture is weird. When I look at main, I see this:
    Code:
    int main()
    {
    greeting();
    return 0;
    
    }
    I cant have it so greeting calls all the other functions? Or is it considered good to have all the function calls so someone taking a first glance will know whats going on right away?

    So, the only thing this program does is greet the user?

    Another major weirdness is this:
    Code:
    void display(Store p[], int &loopie)
    {
    //... snip ...
    delete [] p;
    }
    I was gonna have it delete the memory after all the info was displayed. Are you only able to delete memory in int main?



    Code:
    int main()
    {
        //ask user, how many Stores they want
        int max_size = Greetings();
    
        //main allocates memory, so we needn't guess which function should deallocate
        Store* stores = new Store[max_size];
    
        //fill it up with data
        int entry_count = GetData(stores, max_size);
    
        //run menu, which allows user to modify and display the entries
        Menu(stores, entry_count);
    
        //main deallocates resources and exits
        delete [] stores;
        return 0;
    }
    Cool, man. I'll try that out. Thanks for the help.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by bargomer View Post
    Code:
    Store	*d;
    d = getData(size, loopie);
    d will have the address of the returned value(the array of structures) ?
    In this case yes. getData of course should return the pointer not a copy of the contents of the first thing that the user entered. (In my suggested main such code wouldn't be needed, though.)

    I cant have it so greeting calls all the other functions? Or is it considered good to have all the function calls so someone taking a first glance will know whats going on right away?
    Of course you can do that. But the question is, why is this function called greetings, if it not only greets the user but runs the whole program? It makes code much easier to follow if a function achieves one clear task that can be inferred from their name.

    I was gonna have it delete the memory after all the info was displayed. Are you only able to delete memory in int main?
    You can delete memory anywhere you want. Except doing so in completely random and unexpected places makes memory management very hard.
    When you go to an ATM to view your account balance, do you expect this operation to close your bank account?
    In addition, you had this code:
    Code:
    Store	*d, *e;
    *d = getData(size, loopie);
    menu(d, loopie);
    *e = reData(d,loopie);
    menu(e,loopie);
    If the user selected to display the contents the first time menu is called, the memory won't exist anymore so you could re-enter and call menu again.

    Code:
    int main()
    {
        //ask user, how many Stores they want
        int max_size = Greetings();
    
        //...snip...
    }
    Cool, man. I'll try that out. Thanks for the help.
    You are welcome. You'll need to change all your functions to make that code work. For example this main function expects that none of the functions called in between (or functions called from any of those) won't deallocate the 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
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    I'm now getting an Unresolved external error.

    Error: Unresolved external 'getData(Store, int)' referenced from C:\USERS\BARAZIN\DOCUMENTS\PROJECT9.OBJ
    Error: Unresolved external 'menu(Store, int)' referenced from C:\USERS\BARAZIN\DOCUMENTS\PROJECT9.OBJ

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct Store
    {
    	char name[30];
    	int ID;
    	double balance;
    };
    
    int greeting(), getData(Store , int);
    void menu(Store, int),display(Store, int), reData(Store &, int);

    Those errors usually occur when you dont put the right functions right? Can anybody explain why this is happening?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Common error: you may have two or more files and forgot to compile and link all files.
    Or you misspelled the prototype function names and used those instead of the correct names.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Quote Originally Posted by Elysia View Post
    Common error: you may have two or more files and forgot to compile and link all files.
    Or you misspelled the prototype function names and used those instead of the correct names.

    Weird. It looks like my prototypes and headers are the same. I have another program that uses the same structure names, but they aren't related in anyway.


    *edit*

    Not sure how its actually working, but the code pretty much looks like it does in my oringal post. I don't think im using more than one file.
    Last edited by bargomer; 04-25-2008 at 05:34 PM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your reply doesn't make much sense.
    Say you have two or more files, such as a.cpp, b.cpp, c.cpp. Then you need to compile and link them all into a binary. Easiest if you use an IDE that does it for you (the way it should be done).
    Or you could have made a slight error such as:

    Code:
    void fifo();
    void fiifo()
    {
    }
    
    int main()
    {
        fifo();
    }
    Note how the prototype and definition doesn't match. This will also give a linking error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your prototypes don't match the code:
    Code:
    int greeting(), getData(Store , int);
    void menu(Store, int),display(Store, int), reData(Store &, int);
    void menu(Store temp[], int &loopToFun)
    Store reData(Store stru[], int &looper)

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    18

    Red face

    [CODE[/CODE]

    That's how the new code looks like. Was taking anon's advice on changing the structure of my program. Everything looks like its matching up to me :'(
    Last edited by bargomer; 04-26-2008 at 12:33 AM.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bargomer View Post
    Everything looks like its matching up to me :'(
    Then you should look closer (or maybe at all): your prototype for menu is
    Code:
    void Menu(Store, int);
    while the function you define is
    Code:
    void menu(Store bank[], int loopToFun)
    Notice how Store and Store[] are not in fact the same thing.
    Also:
    Code:
    void reData(Store &, int);
    has no parameters at all in common with your function
    Code:
    void reData(Store stru[], int &looper)
    as Store & does not match Store[] and int does not match int&.

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Quote Originally Posted by tabstop View Post
    Then you should look closer (or maybe at all): your prototype for menu is

    as Store & does not match Store[] and int does not match int&.

    AHHHHH!!! Didn't even notice that I hadn't changed some of them again. Figured it was something else causing the problems. Thanks!



    *edit*

    Thanks everybody finally got it to run the I wanted it to.
    Last edited by bargomer; 04-26-2008 at 12:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Problem executing C program in Visual C++ 6.0
    By ladyscarlet99 in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2005, 05:48 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM