Re: Elegant way to do this?

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 12/11/04


Date: Sat, 11 Dec 2004 00:43:02 GMT


"Rich" <Someone@somewhere.com> wrote in message
news:cpdcrs$gg4$1@hercules.btinternet.com...
> Hello,
>
> I was looking for a way to read a value from an array and then have user
> press enter to retrieve next array value.
>
> I tried this
> char _prompt;
> Read value from array while less than array length -1
> cin >> _prompt;
> while ( _prompt != '^M' == False )
> ;
> I get the following warning
> warning C4806: '!=' : unsafe operation: no value of type 'bool' promoted
> to type 'int' can equal the given constant
>
> not surprisingly this does not work
>
> I can use system("PAUSE"); to cause this effect but it will accept any
> key rather than Enter.
>
> I thought cin may have something to do with this, but a search of VC++
> help does not turn up anything helpful.
>
> Is there an elegant solution to reading values from arrays or reading
> lines from files a line at a time awaiting the user to press enter to
> retrieve each value or line?

#include <iostream>
#include <string>

void wait_for_user()
{
    std::string s;
    std::getline(std::cin, s);
}

int main()
{
    int array[] = {1, 2, 3};
    size_t sz(sizeof array / sizeof *array);
    size_t i(0);

    while(i < sz)
    {
        std::cout << "Press Enter to get a value";
        wait_for_user();
        std::cout << "Value is " << array[i++] << '\n';
    }

    return 0;
}

-Mike



Relevant Pages

  • Re: Search through a (large) binary file.
    ... If you're only ever going to read the plain bytes, just read them directly from the FileStream you're working with. ... You can use the Position property to adjust from where you're reading in the file; save the current position, set the current position to 4 bytes earlier than the offset of the found string, read the 4 bytes of interest, then restore the current position to the previously saved value. ... If you have 4 bytes in an array, it would seem that you ought to just be using BitConverter to convert those directly to a 32-bit int, rather than doing all that stuff with the string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Getting a (non-radio) buttons index number from array
    ... index in the Comment array of the button that was fired. ... you don't need js if you print out the full DOM ... There needs to be some unique way to identify the row in the database. ... Then it's easy to retrieve the ...
    (comp.lang.php)
  • Re: [perfmon] Re: [perfmon2] perfmon2 merge news
    ... where the caller supplies an array of PMD numbers and the function ... returns their values (and you want that reading to be done atomically ... int *pmd_numbers; ...
    (Linux-Kernel)
  • Re: RegEx: finding a string that does not contain /<(w ...(-...)?|c ...)>/
    ... I have an array @a with character strings: ... I want to retrieve the sequence of ... would ideally work without lookaround (but if lookaround is absolutely ...
    (perl.beginners)
  • Re: [FAQ] How do I retrieve a page from a web site? (Was: PHP behaving like a user using a browser)
    ... contents as an array of lines. ... How do I retrieve a page from a web site that requires a cookie? ... Then, if you are coding in PHP 5, pass the context ...
    (comp.lang.php)