CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2004
    Posts
    28

    get() function and string reading

    I have a small program to put strings into vector like this,
    Code:
    #include<vector>
    #include<iostream>
    #include<string>
    
    int main(){
    
    std::vector<std::string> str;
    std::string _holdInput;
    
    while(std::cin>>_holdInput){
    
    str.push_back(holdInput);
    }
    for(int i=0;i<str.size()-1;i++)
    
    std::cout<<str[i]<<std::endl;
    
    return 0;
    }
    My question is, if at the while loop's condition I would like to use std::cin.get(*charGot) in which charGot is declared beforehands as char*, and instead of using std::string in my vector, I change into character array whose object is declared as *str or str[], and in the while loop, I will push back charaters input from user into each vector's char array, will such a program turns out to work fine again, the same as when it is written in the above code ? If it doesn't work, can you tell me why ? if it does, why get() function is not oftenly used in most cases ? I have not written it yet because I think it may not work, I just can think that might be a solution to the problem I am having and the fact is that it is still quite difficult for me to make such a program, can you spend a little time writing that program for me ^,^
    I am really thankful for that.
    As stated in some other posts, I am still new to C++, I am so sorry for such a question....

    I really hope I will be able to get any help from you...

    Thank you so much in advance,

    Regards,

    -Milano

  2. #2
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    So you basically want to stop using std::string and std::vector, and instead use c-style strings and arrays?

    Well it will work. But why on earth would you want to do that? Stay with std::vector and std::string, and save yourself A LOT of trouble!

  3. #3
    Join Date
    Apr 2004
    Posts
    28
    I think I already got the point now, I will use std::vector and std::string to deal with this kind of problem...

    Thanks a lot,

    -Milano

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