Thread: Point out my error please

  1. #1
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166

    Point out my error please

    I thought this would be pretty simple but Windows keeps giving me this message "This application has requested the Runtime to terminate it in an unusal way." Why is this happening?

    Note: At frist I wrote the split function by myself, but when that didn't work, i took one straight from my book, so i can't imagine it being wrong. maybe it's in my main() function?

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <cctype>
    
    using std::cout;
    using std::cin;
    using std::string;
    using std::vector;
    using std::endl;
    
    vector<string> split( const string& s ){
                   
                   vector<string> ret;
                   typedef string::size_type string_size;
                   string_size i = 0;
                   
                   while ( i != s.size() ){
                         
                         while ( i != s.size() && isspace(s[i]))
                         i++;
                         
                         string_size j = i;
                         while ( j != s.size() && !isspace(s[j]))
                         j++;
                         
                         if ( i != j ){
                              
                              ret.push_back(s.substr(i, j - i));
                              }
                         }
                         return ret;
                         }
    
    int main(){
        
        cout << "Enter a sentence: " << endl;
        string v;
        cin >> v;
        vector<string> sen = split(v);
        
        cout << "The words in the sentence are: " << endl;
        vector<string>::size_type i = 0;
        while ( i != sen.size() ){
              cout << sen[i] << endl;
              i++;
        }
        
        
        }
    EDIT: Problem solved. There was something wrong with the program's logic that i completely overlooked.
    Last edited by dra; 07-10-2005 at 04:00 AM.

  2. #2
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    You have forgotten to return something in your main() function. Not that this is causing your error, but it's still not good.
    Code:
    void function(void)
     {
      function();
     }

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Jaken Veina
    You have forgotten to return something in your main() function. Not that this is causing your error, but it's still not good.
    there's no need to return anything in main... sometimes that sparks a holy-war, but that's where I stand: you don't have to, but it's good to explicitly do so.

    I don't know much about vectors, but one thing I noticed in your main function: you tried to use cin to get a sentence. cin stops on the first whitespace (space, tab, newline, etc.)--what you want to use here is getline:

    Code:
    std::string v;
    std::cout<<"Enter a Sentence:\n> "<<std::flush;
    getline(std::cin,v,'\n');
    the way you had it, v contained the first word of whatever they put in. this way, v will include everything they put in until a newline. in other words, whatever sentence they enter.
    Last edited by major_small; 07-09-2005 at 10:04 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    This is a link for getline,
    http://msdn.microsoft.com/library/de...line_class.asp

    You might want to add some debugging statements.
    Code:
    	while ( i != s.size() )
    	{
    		cout << "i = " << i << "\ns.size() = " << s.size() << endl;
    		while ( i != s.size() && isspace(s[i]))
    		{
    			cout << "Do I ever get here!!" << endl;
    			i++;
    		}
    and add cin.get here,
    Code:
    		if( i != j )
    		{
    			ret.push_back(s.substr(i, j - i));
    		}
    		cin.get();
    	}
    are you getting the result you wanted?

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Anubis
    I'm sorry, but that's a terrible reference for getline: http://www.cppreference.com/cppstring/getline.html
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by major_small
    I'm sorry, but that's a terrible reference for getline: http://www.cppreference.com/cppstring/getline.html
    I'm sorry, but are your post alway so pointless.
    I will not argue the value of the link I posted. If you wish to post another link fine. But you should be a little more open minded. If everyone thought like you there would only be one book and one website for reference.
    I am also interested your explanation as to why the link you posted is better?

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    because I don't like you.

    here's your MSDN link, if it'll make you happier.

    and if everybody thought like me, there would not be one book/resource. that's just stupid of you to think. If everybody thought like me, you'd have readable resources that made sense for people who are trying to grasp the concept as well as people who already have a firm grasp on it. like the links I posted.
    Last edited by major_small; 07-09-2005 at 11:42 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by Anubis
    I'm sorry, but are your post alway so pointless.
    I will not argue the value of the link I posted. If you wish to post another link fine. But you should be a little more open minded. If everyone thought like you there would only be one book and one website for reference.
    I am also interested your explanation as to why the link you posted is better?
    Sorry I didnt find your link (Anubis) that usefull especially for beginners. It didnt give examples or really much explanation only syntax which also wasn't really worded great for beginners.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by JoshR
    Sorry I didnt find your link (Anubis) that usefull especially for beginners. It didnt give examples or really much explanation only syntax which also wasn't really worded great for beginners.
    Well you are just as stupid as major_small. Clearly if you can read,
    it has a link to an example at bottom.

    Quote Originally Posted by major_small
    because I don't like you.
    You are must have a major small brain.

    Quote Originally Posted by major_small
    I don't know much about vectors
    Well my conclusion that you do not know much is true.

    Quote Originally Posted by major_small
    getline(std::cin,v,'\n');
    Does not have a clue.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    what's wrong with that last one?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Quote Originally Posted by major_small
    there's no need to return anything in main...
    If you've declared main() to return something, you should return something. void main() doesn't have to return anything, no, but int main() must.
    Code:
    void function(void)
     {
      function();
     }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you've declared main() to return something, you should return something.
    That rule applies to all functions except the global main() function.

    void main() doesn't have to return anything, no, but int main() must.
    That's only true if main() here is not the global main() function. Generally, the global main() function cannot have a return type of void.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by Anubis
    Well you are just as stupid as major_small. Clearly if you can read,
    it has a link to an example at bottom.
    Ya but I dont feel like its that great of an example. Its pure preference dude, no need to get all heated about it.

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    This thread is awfully close to being locked - take your argument elsewhere.

  15. #15
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Sorry actually there WAS something wrong with the split function that I overlooked (do'h!).

    I forgot to set the value of i to j after the if statement in the function which i think kept it in an infinite loop that continuously pushed back the same substring into the vector until i got the erorr message (the program it self would take a few seconds, then close with the error message).

    Sorry for all the trouble.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying a binary tree.
    By nempo in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2008, 09:44 PM
  2. Help realy needed
    By ZohebN in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2008, 09:37 AM
  3. Getting a floating point exception
    By SnertyStan in forum C Programming
    Replies: 13
    Last Post: 03-25-2008, 11:00 AM
  4. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  5. floating point binary program, need help
    By ph34r me in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 07:10 AM