Thread: string problem

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    77

    string problem

    I was trying to implement my own "String" class.
    I have written the class and the following error occurs.

    1.When I create 2 String objects in main function only one string is getting inputted and the other completely ignored

    Code:
    #include <iostream>#include <cstring>
    #include <conio.h>
    
    
    using namespace std;
    enum{MAX=50};
    class String
    {
    	private:
    			char S[MAX];
    	public:
    			String()
    			{S[0]='\0';}
    			void getString();
    			void displayString();			
    		
    };
    void String::getString()
    {
    	cout<<"Enter String :"<<endl;
    	cin.get(S,MAX);
    }
     void String::displayString()
     {
     	cout<<S<<endl; 	
     	
     }
     int main()
     {
     	String S,M;
     	S.getString();
     	 M.getString();
     	S.displayString();
     	M.displayString();
    	 getch();
     	return 0;
     }
    object M is getting ignored..please mention why ..
    please provide the correct code if possible.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Remember get() doesn't extract the end of line character from the input buffer, it leaves this character in the buffer for the next operation. If you continue to use get() you'll need to extract this character before the next call to get(). I recommend you consider using getline() instead, and remember that there are two versions of getline() one for a std::string and another for C-strings.


    Jim

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Jim hit it right on the nail as usual but just to caveat off that if you are using std::getline(std::cin,SomeString,'\n'); and it is skipping it whenever you hit the enter button but in cin.ignore() and that should fix the problem. To give you a better understanding on why this works read:
    Using cin to get user input. - C++ Forum

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Odd use of an enum.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Quote Originally Posted by hk_mp5kpdw View Post
    Odd use of an enum.
    Yea I agree. The way your using that enum it would be a lot better to have done something like:

    Code:
    const int MAX = 50;

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It's a C++03 idiom for non-addressable constants. In C++11, the preferred way is to do this:

    Code:
    constexpr int MAX{50};
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2013, 10:11 PM
  2. Replies: 0
    Last Post: 04-27-2013, 06:36 AM
  3. Replies: 1
    Last Post: 04-27-2013, 04:36 AM
  4. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM