oops ! code crashing.. :(

This is a discussion on oops ! code crashing.. :( within the C++ Programming forums, part of the General Programming Boards category; yup.. Im making a new exercise from a book im reading to learn c++ but what i did cant be ...

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    Exclamation oops ! code crashing.. :(

    yup.. Im making a new exercise from a book im reading to learn c++
    but what i did cant be good cause evry time i run it the program bugs.. (not computer tho..)

    here it is:

    Code:
    void program()
    {
    	int i;
    	char juju[20];
    	typedef pair<char,char> twin;
    	twin bob;
    	vector<twin> here;
    	here.push_back(bob);
    	cout<<"Enter a string of maximum 15 characters."<<endl;
    	cin>>juju;
    	for (i=0;i<10;i++)
    	{
    			here[i]=make_pair(juju[i],juju[i+1]);
    	}
    	for (i=0;i<19;i++)
    	{
    		cout <<i<<" " <<here[i].first<< here[i].second <<"\n";
    	}
    }
    what I've been asked to do (in book) is:
    Write a function that counts the number of occurences of a pair of letter in a string and another that does the same in zero-terminated array of char (a C-style string). For example, the pair "AB" appears twice in "XABAACBAXABB".
    So i used vector so it will grow to fit the lenght of th string(right?) and pair holds 2 char which I will compare (eventually) with each other so this way I will know there is a matching pair..

    so what is wrong?
    vector doesnt grow all by itself maybe?

    thx in advance..
    Luigi

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >vector doesnt grow all by itself maybe?

    No, you have to tell it to grow using something like push_back.
    Joe

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This doesn't even compile. There's no possible way it could. You don't have a 'main' function. Take care of that first.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Thank you, captain obvious.
    Joe

  5. #5
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    ofc it doesnt compile i didnt want to spam so i just posted the part im having problem with..
    just add

    Code:
    #include <iostream>
    using namespace std;
    
    void program();
    
    int main()
    {
    program();
    return 0;
    }
    and thats it..
    but i tought it was obvious??

    as for the push_back thing.. is there a way i could make it so it check if needs to grow if yes push_back somethin if not stay as is?

    caus I'd like to make it so that the vector actually only the size needed for every string I enter each time wich will be different size each time.. (see my point?)

    anyway thx again..

    Luigi

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >but i tought it was obvious??<

    It was.

    >as for the push_back thing.. is there a way i could make it so it check if needs to grow if yes push_back somethin if not stay as is?<

    Strictly speaking your vector may not grow if you push_back. It depends on the algorithm used to allocate internally. Using push_back will ensure that the element to be inserted will get put on the end, and if there isn't enough space then you'll get some more memory allocated.
    Joe

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    no way to control the exact capacity of vectors in STL. That is up to the implementors. You could implement your own version, but that would defeat the purpose of standardization wouldn't it.

    Code:
    typedef pair<char,char> twin;
    twin bob;
    vector<twin> here;
    here.push_back(bob);//think twice here. bob has no value. do you really want him in your group?
    
    cout<<"Enter a string of maximum 15 characters."<<endl;
    cin>>juju;
    
    for (i=0;i<strlen(juju) - 1;i++)//don't overread your data!
    {
         here.push_back(make_pair(juju[i], juju[i+1]));
    
    }
    for (s=0;s< i;s++)//likewise, don't overread your data
    {
      cout <<s<<" " <<here[s].first<< ','<< here[s].second <<"\n";
    }

  8. #8
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    Talking

    thx alot elad..
    That really helped me out..

    with ur help found the bug:

    here[i]=make_pair(juju[i],juju[i+1]);


    that is what was making console crash.
    and using strlen(juju) is what i was searching for..


    really thx again..

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    You could implement your own version, but that would defeat the purpose of standardization wouldn't it.
    Not meaning to sound like a pedantic ass; but if the implementation isn't standardised then you're not defeating the purpose of standardisation. Anyone's free to implement a standard compliant version of the STL.

    P'haps I did.
    Joe

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Originally posted by elad
    no way to control the exact capacity of vectors in STL.
    Actually, you can call reserve() to increase the capacity of a vector. Shrinking a vector is a little more complicated, but it can be done.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 01:48 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. crashing code
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 06-09-2003, 11:09 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

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