how can i get out of this do while loop?

This is a discussion on how can i get out of this do while loop? within the C++ Programming forums, part of the General Programming Boards category; The idea here is that if the user types "hi", then the loop ends, however it doesn't. Code: void conversation1 ...

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    how can i get out of this do while loop?

    The idea here is that if the user types "hi", then the loop ends, however it doesn't.

    Code:
    void conversation1 ()
    	{
       int integer;
       char command[256];
       integer=1;
       do
       cout<<"hi!";
       cout <<"\n\nEnter Command: ";
       cin.getline(command, 256, '\n');
       {
       if (!strcmpi("hi", command))
       {
    	integer=0;
       }
       }
       while (integer!=0);
       }
    AIM: MarderIII

  2. #2
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    No it confirms that Im entering "hi"
    AIM: MarderIII

  3. #3
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    here is what i did:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void conversation1 ();
    
    int main()
    {
    	conversation1();
    
    return 0;
    }
    
    void conversation1 ()
    	{
       int integer;
       char command[256];
       integer=1;
       do{
       cout <<"hi!";
       cout <<"\n\nEnter Command: ";
       cin.getline(command, 256, '\n');
       if (!strcmpi("hi", command))
       {
    	integer=0;
       }
       }while (integer!=0);
    return;  
    }
    and it works fine, if i enter hi it exits, anything else it loops...
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  4. #4
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    I got it thanks

    Do I need the return; in there? The big problem turned out to be that this function was being called from inside another doloop so it was actually working right, but the other doloop needed to have an exit. So I made one! : ) Still though, looking at your code helped me figure out the problem.
    Last edited by Noobie; 04-17-2003 at 08:15 AM.
    AIM: MarderIII

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Noobie
    Do I need the return; in there?
    no, but a lot of people will argue that it is good practice.

  6. #6
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    I think u should do this instead its a better way to achieve what u want..

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void conversation1()
    {
          char command[256] = "bob";
          while(strcmpi("hi", command))
          {
    	  cout<<"hi!";
    	  cout <<"\n\nEnter Command:"<< endl;
    	  cin>> command;
          }
    }
       
       
    int main()
    {
          conversation1();
          return 0;
    }
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    8
    i think that you are missing a bracket({) after the do in the do while statement.

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