Help With 'Do...While' Loop

This is a discussion on Help With 'Do...While' Loop within the C++ Programming forums, part of the General Programming Boards category; First posts are always the greatest. Anyway, I'm trying to make a simple program in C++ that basically just stores ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Help With 'Do...While' Loop

    First posts are always the greatest.

    Anyway, I'm trying to make a simple program in C++ that basically just stores information until close, such as name, age, and eventually other personal preferences that will just basically test the knowledge that I've gathered so far and for me to get used to it if I want to be a game programmer.

    I'm using Dev-C++ 4.9.9.2 as my compiler. This is what I have:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    
    {
     char name[256];
     int age;
     int confirm;
    
    
    cout<<"Hey there.\n ";
     
    
    do
     {
     cout<<"What is your name? ";
     cin.getline (name, 256, '\n');
      } 
    while (confirm==0);
     
     cout<<"\n"<<name<<" is your name? \n\n Press 1 to confirm \n\n Press 2 to change \n\n", name;
     cin>> confirm;
      
      if (confirm==1)
      {
       cout<<"\nGot it.\nAnd how old are you? ";
       cin>> age;
       cout<<"\nSo you're "<<age<<"? Alright, thanks.\n\n ";
      }
    
      if (confirm==2)
      {
       cout<<"\nOh. Well then...\n";
       confirm=0;
       cout<<"Comfirm = "<<confirm<<""; //Just to prove that the variable sets back to 0, so it SHOULD loop.
      }
    
     cin.ignore(); //These are just here because, well, they are. I'm not exactly most efficient yet.
     cin.ignore();
     cin.get();
     return 0;
      
    }
    This is my theory: It'll ask for your name, and you say 1 or 2. If you say 1, it continues. Great. But if you say 2, I want it to go back and give you a chance to re-enter the name, since it sets confirm back to 0, and the loop is to be done while confirm equals 0.

    The issue? It never goes back to the loop.

    Please help me fix this issue. You don't necessarily need to rewrite anything, just point me in the right direction.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you should put the question WITHIN the loop?

    That also helps against the fact that your current code would hang there if confirm happens to be zero - we have no way of knowing what value it has at the end of the do-while at present, it could be anything.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Quote Originally Posted by matsp View Post
    Perhaps you should put the question WITHIN the loop?
    Code:
    do
     {
     cout<<"What is your name? ";
     cin.getline (name, 256, '\n');
      } 
    while (confirm==0);
    The question is in the loop, unless the loop was set up wrong.

    Quote Originally Posted by matsp View Post
    That also helps against the fact that your current code would hang there if confirm happens to be zero - we have no way of knowing what value it has at the end of the do-while at present, it could be anything.
    Do you have any suggestions as to how to go about fixing this?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But your confirm question is outside of the loop, which is what I meant to say in the first post, but apparently didn't get that message across clearly enough.

    Code:
    do {
      ...
    } while( condition );
    will perform what is INSIDE the loop until the condition is false. In your case, that would be asking for the name until confirm is non-zero. Since your complaint is NOT that it stays inside the loop forever, confirm is obviously not zero to begin with, so it exits the loop, then asks to confirm the value. Once that is done, it goes on down the code until you reach the end of main. There is no code to make the code go back to the do - while loop. Hence, you should move the question about the confirmation to inside the do while.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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