CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2009
    Posts
    16

    [RESOLVED] Question about infinite word/phrase input, terminated by a specific word

    Hey everyone. I'm new here, and I'm kinda newbie with C++. So, here's my question.

    I want to make a program in C++ which receives phrases by the user, strings or char vectors, not decided which one to use yet. So, I want it to use a pointer, but not sure exactly how to program it. My idea is that there'll be a cicle which creates a new vector or a string with the pointer for each next word.... And the cicle ends when I write END.

    To picture it a little more explicit:

    Program example input:
    (enter is for when the user presses enter)

    Start inputing words:

    Word (enter)
    This is a word (enter)
    this is not a word (enter)
    It's a phrase (enter)
    END (enter)

    So, I'm not really sure if this can be done in C++, I mean I can do it with an array, but the size of it is fixed, so I can't really input an infinite list of words of phrases.

    Any ideas?

    Thanks in advance

    PS: It's a console program, not a graphic one, I'm not there yet
    Last edited by gtbgmaniak; January 5th, 2009 at 06:01 PM. Reason: Add PS

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Question about infinite word/phrase input, terminated by a specific word

    Curious that you used the word "vector" in your description, since that's exactly what you need....a vector of strings.

  3. #3
    Join Date
    Jan 2009
    Posts
    16

    Re: Question about infinite word/phrase input, terminated by a specific word

    So, you're saying I should learn how to use the vector library?

    What about the infinite cycle, terminated by "END"?

  4. #4
    Join Date
    Jan 2009
    Posts
    16

    Re: Question about infinite word/phrase input, terminated by a specific word

    So, what you basically mean is a vector in a cycle?

  5. #5
    Join Date
    Jan 2009
    Posts
    16

    Re: Question about infinite word/phrase input, terminated by a specific word

    I mean the idea of the program is:

    The user starts inputting words or phrases, the word or phrase ends when the user presses enter. The program will keep asking for a word or a phrase until the user writes the word 'END'. So, the other thing is it should be dynamically assigned. That's my problem, can you help me?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Question about infinite word/phrase input, terminated by a specific word

    You seem to know what needs to be done; what precisely is your problem in just doing it?

  7. #7
    Join Date
    Jan 2009
    Posts
    16

    Re: Question about infinite word/phrase input, terminated by a specific word

    Could you put an example with C++ code, because I don't really know how to use vectors yet. I mean I did something similar with an array but it was finite. I have never used vector before, I more or less understand what it is, but never really used it.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Question about infinite word/phrase input, terminated by a specific word

    Why don't you post your array code, and I'll show you how to modify it to use a vector. (A vector is basically just a wrapper around a dynamic array.)

  9. #9
    Join Date
    Jan 2009
    Posts
    16

    Post Re: Question about infinite word/phrase input, terminated by a specific word

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    #include <string>
    
    const int MAX=255;
    
    int main(){
    
         char phrase[MAX];
         char phrases[MAX][MAX];
    
         cout<<"Please write all the phrases you want."<<endl;
         cout<<"To finish write \"END\""<<endl;
         cout<<endl;
    
         int x=0;
    	
         char terminator[]="END";
         char temp[MAX];
    	
         for(int i=0;i<MAX;i++){
    		
    	     ++x;
    	     cin.getline(phrase,MAX);
    	     strcpy(temp,phrase); 
    	     strcpy(&phrases[i][0],phrase); 
    	     if (strcmp(temp,terminator)==0)
    		     break;		
         }
    
         return 0;
    }
    Here it is. It's only working for 255 phrases... I know they are pretty much but I need the infinite version of this

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Question about infinite word/phrase input, terminated by a specific word

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    using std::vector;
    using std::getline;
    
    int main(){
    
         string phrase;
         vector<string> phrases;
    
         cout<<"Please write all the phrases you want."<<endl;
         cout<<"To finish write \"END\""<<endl;
         cout<<endl;
    	
         char terminator[]="END";
    	
         while (phrase != terminator){
    	     getline(cin,phrase);//note we are use the std::string version of getline, not istream::getline.
    	     phrases.push_back(phrase); 
         }
    
         return 0;
    }
    Here's the update. If you want to avoid adding "END" to phrases, you can just put a pop_back after the loop, or an if statement around the push_back.

    The code will be more efficient if you can .reserve() a certain number of phrases you think you're likely to get beforehand.
    Last edited by Lindley; January 6th, 2009 at 06:06 PM.

  11. #11
    Join Date
    Jan 2009
    Posts
    16

    Thumbs up Re: Question about infinite word/phrase input, terminated by a specific word

    That was just what I needed, put in a very easy to understand way.

    Thanks a lot Lindley!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured