Say you read in 'h' for the first char. How many times does that loop execute?

You can use a version of get() or getline() that will do most of the work for you. I would use the version of getline() that takes three parameters: (a char array name, the size of the array, the delimiter) Specify the delimiter as '#', and getline will read all the char's up to the # sign into the array, and then it will discard the # sign. Then you can use the >> operator to read in what's to the right of the pound sign into a variable of type double like this:

double price = 0;
inFile>>price;

The >> operator will keep reading until it hits whitespace(=carriage returns, tabs, spaces). Check here for some good info:

Make sure to read #4 which is a good explanation of how streams work and alerts you to the problems you can have when you use the >>operator and getline() in sequence to read in data. In your case the delimiter in getline() is the # sign, so the problem mentioned in 4 is avoided.

05-05-2003

Dnyt

That's I have problem in there
If I write:

while (!inData.eof()) {

cout<<ch;

inData.get(ch);

}

It'll work through the file and print each character until the end of the file. I need to seperate the name(hammer) and the price(12.20) but I don't know how to do that.

05-05-2003

7stud

lol. As always seems to be the case, I was editing my post when you replied. Re-read it and see if there's anymore useful information.

05-05-2003

Dnyt

thanks, but I haven't study array yet. The assignment is using the loop to read each character in a line and store them to a variable (newPart += ch) until it hits the pound (#) sign, and then read the number.

05-06-2003

7stud

Quote:

inData.get(ch);

while(ch != '#') {
newPart += ch;
}

Say you read in 'h' for the first char. How many times does that loop execute?

Back to this part then. My point is not that you shouldn't have that while loop but that what you do inside it has to be changed. That loop is an infinite loop given your input. It will read in the first char 'h', and since it doesn't equal '#', it adds it to the variable newPart, but then the loop goes back up and checks the condition, and since ch hasn't changed, it still doesn't equal '#', so it adds 'h' to newPart again, and it will do that an infinite amount of times because you never change ch inside the loop. You have to change it to read in the next char in the loop.

You can't just move the read statement into the loop either:

Code:

while(ch != '#')
{
inData.get(ch);
newPart += ch;
}

because when you hit the EOF, you're going to have problems with it repeating the last char. So, you need to do something like this: