CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2003
    Posts
    29

    while loop problems

    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.

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    We really need to see some (relevant) code....
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Mar 2003
    Posts
    29
    anyone?

  4. #4
    Join Date
    Apr 2002
    Location
    Michigan, USA
    Posts
    869
    Originally posted by Graham
    We really need to see some (relevant) code....
    We need to see your code.....
    Verere testudinem! (Fear the turtle)

    Once you can accept the universe as matter expanding into nothing that is something, wearing stripes with plaid comes easy. -Albert Einstein

    Robots are trying to steal my luggage.

  5. #5
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    This was sent to me as a pm:
    Code:
    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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    I suppose the first question to ask about the supplied code is:

    where does it actually read something from the file?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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:
    Code:
    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

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  10. #10
    Join Date
    Jan 2001
    Posts
    588
    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.

  11. #11
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured