Thread: Debugging OK but program does not work

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    14

    Debugging OK but program does not work

    Hi!

    I wrote program that takes in random numbers from user,sorts them and stores in vector. When I debugged my program and went through it line after line, it worked great but after running it, it just crashed after i had entered few numbers. I use code blocks.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    And your question is?

    You will probably also need to show your code if you want any help.


    Jim

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    14
    Question is why does it do so? If debugging shows no errors then why is it not working?
    Some frases are in my native language. Sorry for that.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    
    using namespace std;
    
    
    int valik;
    int arv;
    int n=0;
    
    
    vector<int>tulemused;
    
    
    //------------------------------------------
    int sisestamine ();
    //---------------------------------------------------------------
    
    
    void tulemustemarkimine ()
    
    
    {
    vector<int>::iterator itr=tulemused.begin();
    
    
    if (tulemused.size()==0)
    
    
    {
    tulemused.push_back(arv);
    }
    
    
    else if (arv>=(*itr))
    {
    tulemused.insert(tulemused.begin(),arv);
    }
    else
    {
    vector<int>::iterator itr=tulemused.begin();
    
    
    while (arv<=*itr)
    {
    itr++;
    n++;
    }
    tulemused.insert(tulemused.begin()+n,arv);
    n=0;
    }
    
    
    }
    //---------------------------------------------
    
    
    int sisestamine ()
    
    
    {
    cout<<"1-Sisesta vaartus:\n2-Valju:\n";
    cin>>valik;
    
    
    if (valik==1)
    {
    cout<<"\nSisesta arv:\n";
    cin>>arv;
    cout<<'\n';
    tulemustemarkimine ();
    }
    
    
    else if (valik==2)
    {
    cout<<"Tulemused vektoris 1 \n";
    for (vector<int>::iterator itr=tulemused.begin();itr!=tulemused.end();++itr)
    {
    cout<<*itr<<' ';
    }
    
    
    return 0;
    
    
    }
    
    
    else
    {
    cout<<"Sisesta korrektne valik!\n\n";
    }
    
    
    sisestamine ();
    return 1;
    }
    
    
    int main ()
    {
    sisestamine ();
    
    
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please show your input.
    When I ran the program is ended as expected. Here is what I entered into the program:
    Code:
    1-Sisesta vaartus:
    2-Valju:
    1
    
    Sisesta arv:
    3
    
    1-Sisesta vaartus:
    2-Valju:
    1
    
    Sisesta arv:
    5
    
    1-Sisesta vaartus:
    2-Valju:
    1
    
    Sisesta arv:
    89
    
    1-Sisesta vaartus:
    2-Valju:
    1
    
    Sisesta arv:
    67
    
    1-Sisesta vaartus:
    2-Valju:
    2
    Tulemused vektoris 1 
    89 67 5 3
    Jim

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    14
    Your combination works for me too! But if I try 20,24,10,12,23 it crashes after 24. If I try to enter these numbers in debugging, it does not crash.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How exactly are you entering the numbers? Your pattern works correctly for me as well.

    Jim

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So what is the point of line 98, where you call sisestamine (); recursively?

    If valik is set to some random value, and the input stream is in an error state (say you literally typed in a comma), then it will just blow up immediately with infinite recursion.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jan 2015
    Posts
    14
    Quote Originally Posted by jimblumberg View Post
    How exactly are you entering the numbers? Your pattern works correctly for me as well.

    Jim
    It works for you!? Interesting. After I run the program I´m entering numbers just like you did in your example above. But in debug mode I use "Step into" button over and over again and if program reaches to the point where it asks to insert option (1 or 2) I do it and then enter new number or program starts to print out numbers in right order (depends what was the option I made, 1 or 2). That is all I do.

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You shouldn't be using global variables. Define the vector and input value in main and pass them to the other functions (passing the vector as a reference).

    sisestamine should not call itself recursively. Instead you should use a loop.

    tulemustemarkimine is overly complicated. Couldn't it just be:
    Code:
    void tulemustemarkimine() {
        vector<int>::iterator itr = tulemused.begin();
        while (itr != tulemused.end() && arv <= *itr)
            ++itr;
        tulemused.insert(itr, arv);
    }
    And note Salem's point. Are you entering any extra characters (comma, period, etc)?
    Last edited by algorism; 11-22-2016 at 02:17 PM. Reason: corrected bug

  10. #10
    Registered User
    Join Date
    Jan 2015
    Posts
    14
    I need line 98 because I want program to ask me to make option (1-enter new value or 2-exit) after every cycle. I bet there is better way to do it but I´m noob. What is "error state?"

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use a loop instead of recursion.
    Code:
      bool done = false;
      do {
        cout << "1-Sisesta vaartus:\n2-Valju:\n";
        if ( cin >> valik ) {
          if (valik == 1) {
            cout << "\nSisesta arv:\n";
            cin >> arv;
            cout << '\n';
            tulemustemarkimine();
          }
          else if (valik == 2) {
            cout << "Tulemused vektoris 1 \n";
            for (vector < int >::iterator itr = tulemused.begin();
                itr != tulemused.end(); ++itr) {
              cout << *itr << ' ';
            }
            done = true;
          }
          else {
            cout << "Sisesta korrektne valik!\n\n";
          }
        }
        else {
          // not a number, throw away the junk
          string junk;
          getline(cin,junk);
        }
      } while ( !done );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Jan 2015
    Posts
    14
    Quote Originally Posted by algorism View Post
    You shouldn't be using global variables. Define the vector and input value in main and pass them to the other functions (passing the vector as a reference).

    sisestamine should not call itself recursively. Instead you should use a loop.

    tulemustemarkimine is overly complicated. Couldn't it just be:
    Code:
    void tulemustemarkimine() {
        vector<int>::iterator itr = tulemused.begin();
        while (itr != tulemused.end() && arv <= *itr)
            ++itr;
        tulemused.insert(itr, arv);
    }
    And note Salem's point. Are you entering any extra characters (comma, period, etc)?
    It´s working! I changed my code with yours and it´s OK! Still weird why debugging does not give any error.

    Why are global variables so bad even in such little program as mine?

  13. #13
    Registered User
    Join Date
    Jan 2015
    Posts
    14
    Quote Originally Posted by Salem View Post
    Use a loop instead of recursion.
    Code:
      bool done = false;
      do {
        cout << "1-Sisesta vaartus:\n2-Valju:\n";
        if ( cin >> valik ) {
          if (valik == 1) {
            cout << "\nSisesta arv:\n";
            cin >> arv;
            cout << '\n';
            tulemustemarkimine();
          }
          else if (valik == 2) {
            cout << "Tulemused vektoris 1 \n";
            for (vector < int >::iterator itr = tulemused.begin();
                itr != tulemused.end(); ++itr) {
              cout << *itr << ' ';
            }
            done = true;
          }
          else {
            cout << "Sisesta korrektne valik!\n\n";
          }
        }
        else {
          // not a number, throw away the junk
          string junk;
          getline(cin,junk);
        }
      } while ( !done );
    Thank you for your code.Will do it!

  14. #14
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by huviline View Post
    Why are global variables so bad even in such little program as mine?
    Globals are a bad habit. The fact that you've used globals suggests that you are uncomfortable passing parameters. But it's something you have to learn. And just because your program is small right now doesn't mean you won't think of another function or two to add to it, making things more and more complicated.

    The way you used the global n variable is like spaghetti code for data. Initially it's zero. Then you use it as a loop counter, and after using it you set it back to zero so that it will be zero next time around. Normal code would set it to zero before using it (and define it locally). Much simpler and less error prone.


    BTW, apparently the lack of a test for tulemused.end() in the while loop of tulemustemarkimine caused the crash.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-06-2012, 08:04 AM
  2. debugging program?
    By brack in forum C Programming
    Replies: 4
    Last Post: 11-21-2010, 03:20 PM
  3. Help for debugging a program
    By Saeid87 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 06:44 AM
  4. help debugging my program
    By kenyi in forum C Programming
    Replies: 2
    Last Post: 08-04-2007, 11:26 PM
  5. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM

Tags for this Thread