Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,916 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,107 people online right now. Registration is fast and FREE... Join Now!




While Problems

 
Reply to this topicStart new topic

While Problems, While (stuff != "text")

HKothari
10 Dec, 2007 - 05:04 PM
Post #1

New D.I.C Head
*

Joined: 1 Dec, 2007
Posts: 12


My Contributions
Alright, my problem is kind of unusual my code compiles but it doesn't work, here is an example of what I mean.
CODE
    char option[128];
    cout << "[Stuff]\n";
    while (option != "done")
    {
          cout << "stuff: ";
          cin.getline(option, 128);
    };
    cout << "Done.\n";
    cin.get();
What I need it to do is when the person types "done" and presses enter, it leaves the while statement, but it will not work, why is my code so grumpy? biggrin.gif That's just a simple example.
User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: While Problems
10 Dec, 2007 - 06:57 PM
Post #2

D.I.C Stomach
Group Icon

Joined: 1 Aug, 2007
Posts: 995



Thanked: 4 times
Dream Kudos: 375
My Contributions
Use string instead of array of chars
CODE
#include <iostream>
#include <string>

using namespace std;

int main()
{

    string option = "done";
    cout << "[Stuff]\n";
    getline(cin, option);
    while (option != "done")
    {    
          
          cout << "stuff: ";
          getline(cin, option);
    }
    cout << "Done.\n";
    cin.get();
    
    return 0;
    
}

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: While Problems
10 Dec, 2007 - 07:06 PM
Post #3

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
You can't use the standard relational/inequality operators with arrays/C-strings. You either need to loop through element-by-element (works for all arrays), or use the strcmp() function (works for null-terminated character arrays i.e. C-strings). The latter option is probably what you want here:
CODE
    char option[128]={0};
    cout << "[Stuff]\n";
    while (strcmp(option, "done") ) {
          cout << "stuff: ";
          cin.getline(option, 128);
    }
    cout << "Done.\n";


The strcmp() function is a little weird from the usual true/false perspective, since it returns 0 if the strings are equal (hence the lack of the ! in the while control statement). Note that I've also initialized the array option to zeros, just on the off-chance that the random data in memory at that location starts out with the string "done" (unlikely, but why take a chance?).

Hope that helps,

-jjh

*edit - p.s. or use nirvanarupali's solution above. The std::string is a great little container, and that's probably a cleaner solution, but it's not clear from your code whether you're restricted to using C-style strings for this code.

This post has been edited by jjhaag: 10 Dec, 2007 - 07:08 PM
User is offlineProfile CardPM
+Quote Post

HKothari
RE: While Problems
10 Dec, 2007 - 07:35 PM
Post #4

New D.I.C Head
*

Joined: 1 Dec, 2007
Posts: 12


My Contributions
QUOTE(nirvanarupali @ 10 Dec, 2007 - 07:57 PM) *

Use string instead of array of chars

Thank you, that example you gave worked wonderfully, but I have one question, when I don't have the getline before the while, it doesn't work, is there a loophole around this?

User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: While Problems
10 Dec, 2007 - 07:40 PM
Post #5

D.I.C Stomach
Group Icon

Joined: 1 Aug, 2007
Posts: 995



Thanked: 4 times
Dream Kudos: 375
My Contributions
Because you need to have a user input, and then the while loop checks it.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: While Problems
10 Dec, 2007 - 07:41 PM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Yep. You can either initialize the string to a value other than done (an empty string works fine here), or use a do-while loop:
CODE
    do {
        
        cout << "stuff: ";
        getline(cin, option);
    }while (option != "done");



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 02:31PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month