Thread: Strings allowing spaces?

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Strings allowing spaces?

    Well I have this string in my class and i try to type two words or more and it will mess up and input those values into my other cin statements. I'm sure there must be away to allow whitespaces but I have yet to discover it from searching on the net. I removed all the other methods to make it a lot easier to find .

    Also, is there any way to limit strings? I have looked on the net about it but from what I can tell strings predefine their length and I have seen no ways to change that.

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    class cPRODUCT
    {
    	private:
    		long nSerialNum;
    		string ProductDesc;
    		float fWholesale;
    		float fRetailcost;
    		int nInStock;
    		float fTotalValue;
    	public:
    		cPRODUCT(void) 
    		{ 
    			nSerialNum = 0;
    			ProductDesc = "";
    			fWholesale = 0;
    			fRetailcost = 0;
    			nInStock = 0;
    			fTotalValue = 0;
    		}
    		void enterProdInfo(void)
    		{
    			cout << "Please enter the product description (max 18 characters): ";
    			cin >> ProductDesc;
                    }
    
    };
    Any help you guys can give would be great.

    Thanks,

    ...Dan

    Hopefully this isn't a really obvious answer like my previous two lol.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    use cin.getline

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    ...acutally, since you are reading into a string type, you should use:

    getline(cin, yourStringTypeVar);

    That will read in a whole line of input--spaces and all--until a '\n' character is encountered, which is added to the end of the input when the user hits return.

    Warning: if you alternate between using cin>> and getline() in your program you are going to run into problems, which will require that you take some extra steps.
    Last edited by 7stud; 02-04-2006 at 10:07 AM.

  4. #4
    System-7
    Join Date
    Nov 2005
    Posts
    65
    I put it in like this

    Code:
    getline(cin, ProductDesc);
    but when i type one word it messes up the other inputs...as well as if i put in two words.

    ...Dan

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    20
    You have to clear the buffer after you use getline() like this:

    Code:
    void clearBuffer
    {
        string temp;
        getline(cin, temp);
    }

  6. #6
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Are you saying to go and do this with a string temp variable?

    Code:
    void clearBuffer(void)
    {
            string temp;
    	getline(cin, temp);
    }
    void enterProdInfo(void)
    {
    
    	cout << "Please enter the product description (max 18 characters): ";
    	getline(cin, ProductDesc);
    
    	clearBuffer();
    
            //bunch more inputs in here but deleted since they serve no purpose for the moment
    }
    I did it this way just to test and if doesnt show any input from the ProductDesc string...just blank like it was using the input from temp.

    Or is it that I sub in ProductDesc for temp?

    ...Dan

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    20
    What do you mean ? Is ProductDesc empty after you called getline() ?

    Try pressing enter again after your input.

    --Shady
    Last edited by QuietWhistler; 02-04-2006 at 11:13 AM.

  8. #8
    System-7
    Join Date
    Nov 2005
    Posts
    65
    well i'll type in a sentence and then press enter...then it will go to the next line with a flashing line meaning i need to press enter again. When i go to display it, all it shows is an empty space like nothing was even inputted.

    ...Dan

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    20
    What are you using for displaying the string ? cout, printf() or what?

  10. #10
    System-7
    Join Date
    Nov 2005
    Posts
    65
    cout

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    20
    Do you get any warnings? If you leave clearBuffer() out, does it work then ?

  12. #12
    System-7
    Join Date
    Nov 2005
    Posts
    65
    no warnings...i take clearbuffer() out and it will send part of what i input into the next input and mess up that value...even if i only put in one word.

    like using cin i can insert one word...but i need to be able to input 2 or more.

    ...Dan

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you using VC++ 6? It has a bug in getline. http://www.dinkumware.com/vc_fixes.html - Scroll to "Fix to <string>" and follow the instructions.

    >> You have to clear the buffer after you use getline() like this
    You don't have to clear the buffer after getline. You have to clear the buffer of the newline when you use operator>> before using getline.

  14. #14
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Thanks for the help...well atleast it was a bug and not something I was messing up

    Thanks again,

    ...Dan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading strings with spaces
    By dezz101 in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 06:02 AM
  2. reading strings with spaces using sscanf
    By kotoko in forum C Programming
    Replies: 2
    Last Post: 06-14-2008, 05:06 PM
  3. Getting scaf to accept strings with spaces in them?
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 11-12-2005, 09:57 AM
  4. accepting long strings with spaces
    By trang in forum C Programming
    Replies: 2
    Last Post: 01-05-2004, 04:17 PM
  5. strings with spaces
    By Wanted420 in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2003, 10:33 PM