Re: Elegant way to do this?
From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 12/11/04
- Next message: Rich: "Re: Polymorphism"
- Previous message: Rich: "Elegant way to do this?"
- In reply to: Rich: "Elegant way to do this?"
- Next in thread: Rich: "Re: Elegant way to do this?"
- Reply: Rich: "Re: Elegant way to do this?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Rich: "Re: Polymorphism"
- Previous message: Rich: "Elegant way to do this?"
- In reply to: Rich: "Elegant way to do this?"
- Next in thread: Rich: "Re: Elegant way to do this?"
- Reply: Rich: "Re: Elegant way to do this?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|