debuging problem

This is a discussion on debuging problem within the C++ Programming forums, part of the General Programming Boards category; Hi! I have written the following piece of code, it compiles and works fine, i can enter data in it, ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    debuging problem

    Hi! I have written the following piece of code, it compiles and works fine, i can enter data in it, but the proble is: when i am tryng to dusplay entered data(numbers in my case) it crashes. I thnks that i am missng one small line of code, could anyone please tell me which line is it, and where am i suppose to put it? thanks.
    Code:
     
    #include <stdio.h>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    void mergesort(int [], int [], int [], int, int);
    
    int main()
    
    {
        int A[50], B[50], C[100], m, n, i;
        
        cout << "*****************************************************************************" << endl;
        cout << setw(45) << " MERGE SORT PROGRAM " << endl;
        cout << "*****************************************************************************" << endl;
    
        cout << " How many elements would you like to have in your first array? (max 50): " << endl;
        cin >> m;
    
        cout << " Please enter the array of elements in ascending order: " << endl;
        for (i=0; i<m; i++)
             cin >> A[i];
    
        cout << " How many elements would you like to have in your second array? (max 50): " << endl;
             cin >> n;
    
        cout << " Please enter the array of elements in ascending order:" << endl;
    
        for (i=0; i<n; i++)
             cin >> B[i];
             
        mergesort(A, B, C, m, n);
    
        cout << "\n------The result of your sorted arrays is----" << endl;
    
        for (i=0; i<m+n; i++)
    
             cout << C[i];
                 
        return 0;
    }
    
    void mergesort(int A[], int B[], int C[], int m, int n)
    {
        int a=0, b=0, c=0;
    
        for (a=0, b=0, c=0; a<m && b<n;)
        {
            if (A[a]<B[b])
    
                C[c++] = A[a++];
            else
    
                C[c++] = B[b++];
        }
        if (a<m)
            while (a<m)
    
                C[c++] = A[a++];
        else
            while (b<n)
    
                C[c++] = B[b++];
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I see nothing wrong with your code, and it runs fine for me. What line is it crashing on?

    I would note that the "mergesort" function is a bit misnamed ... it's more of a merge.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    the code itself is ok, it crashes on the following line:
    Code:
    mergesort(A, B, C, m, n);
    
        cout << "\n------The result of your sorted arrays is----" << endl;
    
        for (i=0; i<m+n; i++)
    
             cout << C[i];
                 
        return 0;
    before this line comes into action i can add numbers into the first array and second ,but for some reason it won't display the sorted array that uses entered numbers.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    The only reason that code would crash is if you elected to enter more than 50 values into the first or second array.

    Don't just let your program misbehave if the user enters a number too large. It's not hard to do something other than just letting a buffer overrun occur.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps the program just finishes execution and closes the console window.
    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).

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    thats what it does, it just finishes execution and closes the window. But it supposed to show the results(output) for some reason it doesn't.If any one have some solution to it, please helppppp....

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It probably DOES show the result - for 1/100th of a second, and then goes away. Because there is nothing in your code that makes the program "wait" after displaying the result.

    Run it in a command prompt, or put some input into the code to wait for the user to hit enter [beware of newlines left behind by scanf].

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

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Or use an IDE that does keep programs open after they terminate, like CodeBlocks.
    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).

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    Or use an IDE that does keep programs open after they terminate, like CodeBlocks.
    Yes, that's an option. I believe that Visual Studio does too if you use the right command to run the code (not F5).

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

  10. #10
    Registered User
    Join Date
    May 2007
    Posts
    147
    In Visual Studio, I usually just set a breakpoint at the tail of main, so it waits until I let the application return from main.

    I don't know of a way to leave the console window in view.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JVene View Post
    In Visual Studio, I usually just set a breakpoint at the tail of main, so it waits until I let the application return from main.

    I don't know of a way to leave the console window in view.
    Just for my own sanity, I tested it: CTRL-F5 ends the program with a "Press any key to continue".

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

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    16
    im not sure how much this will help at all but...


    do a cin.get(); after every cin << , this hasnt been much of an issue w/ me but i did notice a few times that the end of line or enter key left in the input gave me issues when compiling with visual c++.

    the other thing is right before return 0; do another cin.get(); to keep the console up. if it still closes add another cin.get(); before return 0;

    maybe thats the issue, i dunno tho.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    16
    yep, i was 100% correct, if you press enter or space or anything after each character it stays in the input buffer, and gets ignored by cin <<, anyway, try this, should work.

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    void mergesort(int [], int [], int [], int, int);
    
    int main()
    
    {
        int A[50], B[50], C[100], m, n, i;
        
        cout << "*****************************************************************************" << endl;
        cout << setw(45) << " MERGE SORT PROGRAM " << endl;
        cout << "*****************************************************************************" << endl;
    
        cout << " How many elements would you like to have in your first array? (max 50): " << endl;
        cin >> m;
    	cin.get();
    
        cout << " Please enter the array of elements in ascending order: " << endl;
        for (i=0; i<m; i++)  
    	{
    		cin >> A[i];
    		cin.get();
    	}
        cout << " How many elements would you like to have in your second array? (max 50): " << endl;
             cin >> n;
    
        cout << " Please enter the array of elements in ascending order:" << endl;
    
        for (i=0; i<n; i++)
    	{
             cin >> B[i];
    		cin.get();
    	}
    
        mergesort(A, B, C, m, n);
    
        cout << "\n------The result of your sorted arrays is----" << endl;
    
        for (i=0; i<m+n; i++)
    
             cout << C[i];
    	cin.get();
        return 0;
    }
    
    void mergesort(int A[], int B[], int C[], int m, int n)
    {
        int a=0, b=0, c=0;
    
        for (a=0, b=0, c=0; a<m && b<n;)
        {
            if (A[a]<B[b])
    
                C[c++] = A[a++];
            else
    
                C[c++] = B[b++];
        }
        if (a<m)
            while (a<m)
    
                C[c++] = A[a++];
        else
            while (b<n)
    
                C[c++] = B[b++];
    }
    you will notice i made your for loops that had a cin >> have a full code block and added cin.get() after each one, then i also added a cin.get(); right before the return 0;


    that was the problem, the code wasn't crashing, there just was nothing left to execute and keep the console open. therefor terminating the process.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Before all these cin.get()s one should put a cin.ignore(), something like

    Code:
    cin.ignore(128, '\n');
    Now cin.get() won't have anything to read immediately (left-over newlines, other unread input).
    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).

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    16
    Quote Originally Posted by anon View Post
    Before all these cin.get()s one should put a cin.ignore(), something like

    Code:
    cin.ignore(128, '\n');
    Now cin.get() won't have anything to read immediately (left-over newlines, other unread input).
    wait, tell me what that does.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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