Thread: Back to where I started...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    76

    Back to where I started...

    Well, I thought I successfully learned how to erase a user given location in a vector...but since I switched over to Visual Studio 2005... I've been getting strange bugs...

    My Code...
    Code:
    void Phonebook::Delete()
    {
         if(vEntries.empty())
         {
                CLEARSCREEN;
                cout<<"\n\t\t\tCannot Delete...Entry list is empty!"<<endl;
                cout<<"\t\t\tBeing sent back to menu";
                Sleep(500);
                cout<<".";
                Sleep(500);
                cout<<".";
                Sleep(500);
                cout<<".";
                Sleep(500);
                CLEARSCREEN;
                return;
         }
         unsigned int ieDelete=0;
    
         cout<<"\n\t\t\tDelete which entry? ( 1 - "<<vEntries.size()<<" )";
         cout<<"\n\t\t\t->: ";
         cin>>ieDelete;
         if(ieDelete == 1)
         {
                     vEntries.erase(vEntries.begin());
         }
         else if(ieDelete == vEntries.size())
         {
                     vEntries.erase(vEntries.end());
         }
         else if(ieDelete < 1)
         {
              cout<<"\n\t\t\tChoice less than 1. Returning...";
              cin.get();
              CLEARSCREEN;
         }
         else if(ieDelete > vEntries.size())
         {
              cout<<"\n\t\t\tChoice higher than number of entries. Returning...";
              cin.get();
              CLEARSCREEN;
         }
         else
         {
              vEntries.erase(vEntries.begin() + (ieDelete-1));
         }
         CLEARSCREEN;
    }
    The code underlined is where the program has a fatal error... and the VS debugger pops up... I don't really get how to use the debugger yet... but I do know that I did have a problem with the same line before... but I fixed it in Dev C++ to the line you see above. But in VS it doesn't work right in the program...

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Why would you want to be able to do such a thing to a phonebook.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    condition two not right

    Kuphryn

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    condition two not right
    MAN that was sharp!


    I must tell you that insert/delete operations in random vector positions are extremely computationally expensive.


    EDIT 1: By the way, why are you clearing the screen in some of the conditionals if you're going to do it again in the end?


    EDIT 2: Using a macro is bad, especially if you don't need one in the first place. This approach is preferred, and safer to boot:

    Code:
    void clearscreen()
    {
        //insert clearscreen code here
    }
    Last edited by jafet; 06-05-2006 at 01:25 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    I must tell you that insert/delete operations in random vector positions are extremely computationally expensive.
    So, what is another way to do this without the expense?

    By the way, why are you clearing the screen in some of the conditionals if you're going to do it again in the end?
    Well, whenever I open up the IDE, it's usually at night, so the code is probably sloppish...

    Using a macro is bad, especially if you don't need one in the first place. This approach is preferred, and safer to boot:

    Code:
    Code:
    void clearscreen()
    {
        //insert clearscreen code here
    }
    I will keep this information for future reference...but for right now... lol ... its not really needed since it isn't a large program.

    condition two not right
    Care to give any details?

    Why would you want to be able to do such a thing to a phonebook.
    Well, this program started out at Phonebook1 then Phonebook2 etc. and now the final one is on Phonebook4. At first, I had a simple program that would output phonebook info to a .txt file in a formatted style. And now at Phonebook4, I learned about vectors, and this program was simply an excercise. And, in the program you can open a file... and it displays entries with 1-(vector size). So you can edit the phonebook info through the program... its just an addon to work with vectors...

    **Can anybody give any advice on the underlined line?

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Here's your Condition II

    Code:
         else if(ieDelete == vEntries.size())
    Compare

    Code:
    int a[10] = {0};
    cout << a[10];
    You're overflowing by 1.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    Thanks for the comparison... but what does it mean?

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    remember that array subscripting starts at 0. As such an array with 10 elements is traversed from 0 to 9.

    You are trying to output element 11 of an array with 10 elements.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    No, he's trying to erase element 11 of an array with 10 elements.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. UInt to CString and back
    By xlnk in forum Windows Programming
    Replies: 6
    Last Post: 08-27-2003, 03:08 PM
  2. Some woman back ended my car today, and back hurts
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 08-20-2003, 12:42 AM
  3. Yey Im Back!
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2003, 03:07 PM
  4. I'm back
    By Fool in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-28-2001, 09:16 AM