-
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 :p .
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.
-
-
...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.
-
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
-
You have to clear the buffer after you use getline() like this:
Code:
void clearBuffer
{
string temp;
getline(cin, temp);
}
-
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? :p
...Dan
-
What do you mean ? Is ProductDesc empty after you called getline() ?
Try pressing enter again after your input.
--Shady
-
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
-
What are you using for displaying the string ? cout, printf() or what?
-
-
Do you get any warnings? If you leave clearBuffer() out, does it work then ?
-
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
-
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.
-
Thanks for the help...well atleast it was a bug and not something I was messing up ;)
Thanks again,
...Dan