For Loop Question, Please Help!

This is a discussion on For Loop Question, Please Help! within the C++ Programming forums, part of the General Programming Boards category; My IDE is Dev 4.0 I'm trying to figure out this for loop. It is supposed to count until the ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    3

    For Loop Question, Please Help!

    My IDE is Dev 4.0

    I'm trying to figure out this for loop. It is supposed to count until the user terminates the loop but it's not working! Typing in "N" makes it go and I think it supposed to stop after each number, not keep going even if you type in "Y".

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
          int count;
          int response;
    
          for (count = 1; response != 'N'; count++)
    {
        cout << count << endl;
        cout << "Continue (Y/N): ";
        cin >> response;
        cin.get();
    } 
    cin.get();
    return 0;
    }
    Also could someone explain the "cin.get();" thing to me? What exactly does it mean? It is not what is screwing this up but I'm just curious. As far as I've figured it out, it goes before "return 0;" and after any "cin >> something;". Thank you.
    Last edited by Denarius11; 06-25-2004 at 08:41 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    char response;

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Code:
    char response;
    not too good on C++, so check with others regarding security ramifications.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    I think this is the first time I actually saw someone try to use a for loop for prompting a response like this.

    1. Why don't use a do/while loop?

    2. Why do you need count?

    3. Do you have any references to help you? book, teacher, etc.

    Mr. C.
    Mr. C: Author and Instructor

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    3

    .

    The reason I am using a for loop for this is because I am going through the about.com C++ tutorials and it said to try it out (they provide the code) and it ain't working.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > Also could someone explain the "cin.get();" thing to me?
    You press 2 keys
    Y
    <enter>

    cin >> response; reads your Y or N
    cin.get(); just throws away your newline
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    3
    The problem is that when I enter 'N' it goes and it is not supposed to do that. Also, from the tutorial I am reading, the way I understand it is that when I enter 'Y', it should increase by one and then ask you again if you want to continue. It doesn't do that... it just keeps going forever.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >it just keeps going forever.
    Read the first reply and make that change. operator>> is type independent, it determines how to act based on the type of the object you give it. When you give it an int, it will try to read a number. If it reads a character such as 'Y' or 'N', it'll fail because that isn't a number. It doesn't extract the offending character, so you continue to fail on it. The cin.get() call also fails because cin is in a failure state, so you have an infinite loop or whenever signed integer overflow causes undefined behavior, then I can't specify what will happen.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21