Array Problem

This is a discussion on Array Problem within the C++ Programming forums, part of the General Programming Boards category; I am learning arrays and I have a simple question. Why isnt my do-while loop working? Code: #include <iostream.h> int ...

  1. #1
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57

    Array Problem

    I am learning arrays and I have a simple question.
    Why isnt my do-while loop working?

    Code:
    #include <iostream.h>
    
    int main()
    {
        char string[100];
        do{
        cout << "What is your name?";
        cin.getline (string, 100);
        cout << "\nHello" << string <<".\n";
        cout << "Where are you from?";
        cin.getline (string, 100);
        cout << "\nYour from" << string <<"! Cool me too\n";
        cout << "What is your favorite sports team";
        cin.getline (string, 100);
        cout << "\nThe" << string <<"! Nah they suck";
    }
        while ( (string != "BYE" ) && (string != "bye" ) );
        
        return 0;
    }
    You can see the loop in it .
    can you guys please help me

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try this instead:
    Code:
    #include <iostream> //more standards-compliant
    #include<cstring> //for strcmpi()
    
    using namespace std; //more standards-compliant
    
    int main()
    {
        char string[100];
        do{
           cout << "What is your name?";
           cin.getline (string, '\n', 100); //stops when the user presses enter or when it reaches 100 chars, whichver comes first
           cout << "\nHello" << string <<".\n"; 
           cout << "Where are you from?";
           cin.getline (string, '\n', 100); //stops when the user presses enter or when it reaches 100 chars, whichver comes first
           cout << "\nYour from" << string <<"! Cool me too\n";
           cout << "What is your favorite sports team";
           cin.getline (string, '\n', 100); //stops when the user presses enter or when it reaches 100 chars, whichver comes first
           cout << "\nThe" << string <<"! Nah they suck";
       }while ( strcmpi(string,"BYE")!=0); //compares what's in string to "BYE" in all cases (BYE,BYe,ByE,Bye,bYE,bYe,byE,bye)
        
        return 0;
    }
    Last edited by major_small; 03-18-2004 at 08:48 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You can see the loop in it
    Not as clearly as I could if you used better indention.

    >Why isnt my do-while loop working?
    Because you can't compare C-style strings with relational operators. The result will not be what you're expecting. Include <cstring> and use strcmp instead:
    Code:
    while ( strcmp ( string, "BYE" ) != 0 && strcmp ( string, "bye" ) != 0 );
    Of course, you can avoid two calls by writing your own case insensitive comparison function. It's easy to do. Or if your compiler supports something like stricmp you can use that instead.
    My best code is written with the delete key.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >while ( !strcmpi(string,"BYE"));
    This won't do what you're expecting and certainly not what the OP's intention was.
    My best code is written with the delete key.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    >>This won't do what you're expecting and certainly not what the OP's intention was.

    sorry about that... fixed... I think... it did what I was expecting, but I wasn't paying enough attention to what the OP wanted...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    Those arent working correctly.
    Heres the code with the loops.

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std
    
    int main()
    {
        int age;
        char string[100];
        do{
            cout << "What is your name? ";
            cin.getline (string, 100);
            cout << "Hello " << string <<".\n";
            cout << "How old are you? ";
            cin.getline (string, 100);
            age = atoi (string);
            cout <<"\n" << string << ", Really!?!?! You're that old.\n";
            cout << "Where are you from? ";
            cin.getline (string, 100);
            cout << "\nYour from" << string <<"! Cool me too";
            cout << "\nWhat is your favorite sports team";
            cin.getline (string, 100);
            cout << "\nThe " << string <<"! Nah they suck";
        }
        while ( strcmp ( string, "BYE" ) != 0 && strcmp ( string, "bye" ) != 0 );
    return 0;
    }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Those arent working correctly.
    That's because you wanted to use a do..while loop. You would need to add another conditional after getting the name to check and see if it's an exit condition. Or you could use a simpler while loop and a redundant output call:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
      char string[100];
    
      cout << "What is your name? ";
      while ( cin.getline (string, 100)
        && strcmp ( string, "BYE" ) != 0
        && strcmp ( string, "bye" ) != 0 )
      {
        cout << "Hello " << string <<".\n";
        cout << "How old are you? ";
        cin.getline (string, 100);
        cout <<"\n" << string << ", Really!?!?! You're that old.\n";
        cout << "Where are you from? ";
        cin.getline (string, 100);
        cout << "\nYour from " << string <<"! Cool me too";
        cout << "\nWhat is your favorite sports team ";
        cin.getline (string, 100);
        cout << "\nThe " << string <<"! Nah they suck\n";
        cout << "What is your name? ";
      }
    
      return 0;
    }
    Our suggestions were correct, they just weren't what you wanted. We didn't give you what you wanted because you didn't ask for it.
    My best code is written with the delete key.

  8. #8
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    that only works in the first cin.getline
    I want ot be able to say bye and exit the program at any point

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    test it every time you get new input, then use break;
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    thatll be alot of code.
    ]is there any other way to do it.
    I don't know why the do..while isn't working

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >thatll be alot of code.
    Sorry.

    >is there any other way to do it.
    No, not really. If you want to scatter interactive prompts and input calls all over the place you have no choice but to test each one individually.

    >I don't know why the do..while isn't working
    I'm going to start ignoring you unless you stop saying "it's not working" and tell us what you think it should be doing in no uncertain terms.

    You tell us the problem, we give you an answer, you say it doesn't work. Then you go on to tell use you really wanted something different than the question you asked. When you don't like the answer we give, you revert to your original unhelpful question. Before you write code and ask questions, figure out what you really want, because this circular question and answer deal is annoying.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM

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