Click to See Complete Forum and Search --> : while loop problems
dc83
March 4th, 2003, 12:52 PM
I'm tryin to read text from an input file on letter at a time in strings. The text is formatted like this
D 5
A 9
and so on. I made my while loop and everything but when I run the program after the user inputs the input filename the program stalls and the cursor just blinks. I can type stuff in but the program doesn't exit. Anyone have any suggestions.
Graham
March 4th, 2003, 01:27 PM
We really need to see some (relevant) code....
dc83
March 4th, 2003, 02:28 PM
anyone?
Tom Frohman
March 4th, 2003, 02:33 PM
Originally posted by Graham
We really need to see some (relevant) code....
We need to see your code.....
PaulWendt
March 4th, 2003, 02:35 PM
Originally posted by dc83
anyone?
To preempt another "anyone?", I would like to see some more
code too. Just read your own post and see if you'd know what
the person was talking about if you weren't thoroughly familiar
with the problem.
Without seeing your code, it sounds like you've got either an
endless loop or your code is still waiting for input to be processed.
How do you fix that? Show us the code and we can help :)
--Paul
Graham
March 5th, 2003, 03:45 AM
This was sent to me as a pm:
cout << "please specify the name for the input file about the cards \n";
cin >> cardsfile;
file.open(cardsfile.c_str());
if (file.fail())
{
cout << "The file " << cardsfile << " you specified is invalid\n";
return 1;
}
while (file.good()){
if (cards == "C"){
if (cards >="1" && cards <="9"){
clubsnonfacecards ++;
}
dc83: please post to the forum thread, not as a private message. I won't answer questions through private messages, since that deprives other people of the chance to see or challenge what I say.
Graham
March 5th, 2003, 05:34 AM
I suppose the first question to ask about the supplied code is:
where does it actually read something from the file?
PaulWendt
March 5th, 2003, 05:37 AM
Well, you're in a while loop ... looping as long as the input file has
more data to read in. We don't have any idea what
"clubsnonfacecards" is; is that an integer data type? Unless it's
an istream_iterator, you're never getting any data from the file so
your code will enter the while loop and never stop. Also, what
is "cards" ? I'm assuming it's a std::string since you use the c_str
function in another area of your code. You're going to need to
do something like:
file >> inputHolder;
That'll need to be inside of your while loop.
Also, try to make your code examples be thorough. Don't use a
variable unless you've got a definition for it in the code too :) The
BEST approach is to narrow the problem down to the smallest
code set possible and post an entirely functional main().
Also, do you have a debugger? You could have set a break point
and determined this while() loop problem on your own if you did :)
--Paul
Graham
March 5th, 2003, 05:38 AM
The second point to raise is that, if
cards == "C"
is true, then
cards >= "1" && cards <= "9"
is almost certainly false, unless the character ordering on the particular machine is very peculiar.
And there's a missing brace, so it's possible that the (real) code doesn't actually do what it appears to say.
Bob Davis
March 5th, 2003, 09:19 AM
Also, when using the lines
cards == "C" and cards >= "1" && cards <= "9"
, I believe you need single quotes (') instead of the double quotes ("). To represent a single character you use the single quotes, while a double quoted expression is taken as a string literal. I'm not sure, but wouldn't the comparison using the double quotes actually use the memory address of the constant string "C"/"1"/"9"? That's obviously not what you want. A string is represented by a pointer, so if you're wanting to compare contents, use a function like strcmp, or even better, use std::string.
Graham
March 5th, 2003, 09:43 AM
If the cards variable is a std::string, then the comparison is valid, since the string literal can be implictly cast to a std::string.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.