I need a function, witch make a break in a for-loop and wait for the
<enter>-key ... when I use Pascal I just use the Read; or the
ReadLn;-function, then the loop stop as long as the user push the
<Enter>-key. 11 24292
"Denis Hierstein" <Hi********@compuserve.de> wrote in message
news:bh*********@ngspool-d02.news.aol.com... I need a function, witch make a break in a for-loop and wait for the <enter>-key ... when I use Pascal I just use the Read; or the ReadLn;-function, then the loop stop as long as the user push the <Enter>-key.
You cannot wait for any key in standard C++. Standard C++ has no support
for keyboards.
You can do this
#include <string>
#include <iostream>
std::string dummy;
std::getline(std::cin, dummy);
which will read a line of text from the standard input. This might be what
you want but that depends on exactly what you are trying to do.
john
John Harrison wrote: "Denis Hierstein" <Hi********@compuserve.de> wrote in message news:bh*********@ngspool-d02.news.aol.com...
I need a function, witch make a break in a for-loop and wait for the <enter>-key ... when I use Pascal I just use the Read; or the ReadLn;-function, then the loop stop as long as the user push the <Enter>-key.
You cannot wait for any key in standard C++. Standard C++ has no support for keyboards.
You can do this
#include <string> #include <iostream>
std::string dummy; std::getline(std::cin, dummy);
which will read a line of text from the standard input. This might be what you want but that depends on exactly what you are trying to do.
john
Out of curiosity, is this behaviour guarantied by the standard? Is
getline forced (by the standard) to read a string that is never used or
is it possible for the compiler to remove this as an optimization?
Does the same answer hold for other ways of reading a string (or other
data) from a stream object (eg [int i; std::cin >>i] , [string s; cin >>
s]) )?
The reason for this question is my previous experiences with Fortran,
where optimization may even remove parts of code that actually is doing
(at least) output to file or screen.
/hall
--
( - Remove capital X from email to reply - )
hall wrote: John Harrison wrote: #include <string> #include <iostream>
std::string dummy; std::getline(std::cin, dummy);
which will read a line of text from the standard input. This might be what you want but that depends on exactly what you are trying to do.
john
Out of curiosity, is this behaviour guarantied by the standard? Is getline forced (by the standard) to read a string that is never used or is it possible for the compiler to remove this as an optimization?
The compiler can't remove this as it violates principle #1 in optimization:
The compiler can optimize away anything it wants, as long as the programms
behaviour does not change.
There is one exception to that rule, which is explicitely mentioned in the
C++ standard: return value optimization, where the compiler is allowed to
optimize away a call to the copy constructor even if that cctor has
side effects. But ASFAIK this is the only exception.
--
Karl Heinz Buchegger kb******@gascad.at
hall <Xc***********@yahoo.se> wrote in news:3F************@yahoo.se:
John Harrison wrote: "Denis Hierstein" <Hi********@compuserve.de> wrote in message news:bh*********@ngspool-d02.news.aol.com...
I need a function, witch make a break in a for-loop and wait for the <enter>-key ... when I use Pascal I just use the Read; or the ReadLn;-function, then the loop stop as long as the user push the <Enter>-key.
You cannot wait for any key in standard C++. Standard C++ has no support for keyboards.
You can do this
#include <string> #include <iostream>
std::string dummy; std::getline(std::cin, dummy);
which will read a line of text from the standard input. This might be what you want but that depends on exactly what you are trying to do.
john
Out of curiosity, is this behaviour guarantied by the standard? Is getline forced (by the standard) to read a string that is never used or is it possible for the compiler to remove this as an optimization?
It is not allowed to remove the read by optimization, because it changes
the whole effect that your program has (i.e. no keyboard input read->
different system state).
If so, you could never read a dummy line that contains no valuable
information for your program.
Does the same answer hold for other ways of reading a string (or other data) from a stream object (eg [int i; std::cin >>i] , [string s; cin s]) )?
Of course. You optimizer will not decide whether your code makes sense
and erase the code if not. That's what the programmers are for.
The reason for this question is my previous experiences with Fortran, where optimization may even remove parts of code that actually is doing (at least) output to file or screen.
I guess your optimizer then was broken. I do not think it is legal to
remove program output even in Fortran. Its like you write a program
calculating pi to the 4050493493...4993..32nd digit and then your
optimizer doesn't make the output. See?
Karl Heinz Buchegger wrote:
hall wrote: John Harrison wrote: > #include <string> > #include <iostream> > > std::string dummy; > std::getline(std::cin, dummy); > > which will read a line of text from the standard input. This might > be what you want but that depends on exactly what you are trying to > do. > > john > >
Out of curiosity, is this behaviour guarantied by the standard? Is getline forced (by the standard) to read a string that is never used or is it possible for the compiler to remove this as an optimization?
The compiler can't remove this as it violates principle #1 in optimization: The compiler can optimize away anything it wants, as long as the programms behaviour does not change.
How would the behaviour of the program change?
Rolf Magnus wrote: Karl Heinz Buchegger wrote:
hall wrote: John Harrison wrote: > #include <string> > #include <iostream> > > std::string dummy; > std::getline(std::cin, dummy); > > which will read a line of text from the standard input. This might > be what you want but that depends on exactly what you are trying > to do. > > john > >
Out of curiosity, is this behaviour guarantied by the standard? Is getline forced (by the standard) to read a string that is never used or is it possible for the compiler to remove this as an optimization?
The compiler can't remove this as it violates principle #1 in optimization: The compiler can optimize away anything it wants, as long as the programms behaviour does not change.
How would the behaviour of the program change?
Sorry, I might have misunderstood. I thought, "hall" was asking if the
compiler could optimize the string away, i.e. instead of copying the
line into a string and then throwing that string away, just remove the
line from cin without copying it anywhere.
Denis
This might help a little. you just need to make sure that when you call the
function, the standard input stream is empty.
cheers
Pete
#include<iostream>
int main()
{
while(true)
{
std::cout << "Press enter key";
if(std::cin.peek() == '\n') // looks to see if the next character in the
standard input stream is enter
{
std::cin.ignore(1000,'\n'); // clear the stream
break; // break the loop
}
else
{
std::cin.ignore(1000, '\n');
continue;
}
}
std::cout << "while broken";
return 0;
}
"Denis Hierstein" <Hi********@compuserve.de> wrote in message
news:bh*********@ngspool-d02.news.aol.com... I need a function, witch make a break in a for-loop and wait for the <enter>-key ... when I use Pascal I just use the Read; or the ReadLn;-function, then the loop stop as long as the user push the <Enter>-key.
Rolf Magnus wrote:
[SNIP] Sorry, I might have misunderstood. I thought, "hall" was asking if the compiler could optimize the string away, i.e. instead of copying the line into a string and then throwing that string away, just remove the line from cin without copying it anywhere.
In theory (if the std::string functionalty used by the code is all inline)
the compiler _may_ remove the string creation, but of course not the input.
I personally doubt if any compiler is clever enough to do that. Why? It
would then need to create an unnamed static string as a replacement so that
it can still write the characters somewhere - since it cannot optimize away
that part of the getline function, even if it is inline (one definition
rule). I wonder what the standard says about an optimization like this...
Anyways it is probably possible to trick it using ignore and a "hand made"
static variable - but I am absolutely beginner in iostreams so I better stop
guessing. :-)
Attila
Immanuel Albrecht wrote: hall <Xc***********@yahoo.se> wrote in news:3F************@yahoo.se:
John Harrison wrote:
You cannot wait for any key in standard C++. Standard C++ has no support for keyboards.
You can do this
#include <string> #include <iostream>
std::string dummy; std::getline(std::cin, dummy);
which will read a line of text from the standard input. This might be what you want but that depends on exactly what you are trying to do.
john
Out of curiosity, is this behaviour guarantied by the standard? Is getline forced (by the standard) to read a string that is never used or is it possible for the compiler to remove this as an optimization?
It is not allowed to remove the read by optimization, because it changes the whole effect that your program has (i.e. no keyboard input read-> different system state).
If so, you could never read a dummy line that contains no valuable information for your program.
Does the same answer hold for other ways of reading a string (or other data) from a stream object (eg [int i; std::cin >>i] , [string s; cin
s]) )?
Of course. You optimizer will not decide whether your code makes sense and erase the code if not. That's what the programmers are for.
unless you are using Fortran The reason for this question is my previous experiences with Fortran, where optimization may even remove parts of code that actually is doing (at least) output to file or screen.
I guess your optimizer then was broken. I do not think it is legal to remove program output even in Fortran. Its like you write a program calculating pi to the 4050493493...4993..32nd digit and then your optimizer doesn't make the output. See?
Actually, no. The compiler was not broken. Optimizing can be(*) a rather
violent process in Fortran and may remove functionality in the code,
resulting in missing outputs, ignored function calls etc. And this is of
course why I wanted to make sure that C++ did not support anything like
this in its standard.
However, code such as
for (unsigned long int i=0; i<255^4; i++){
float f=... // silly calculations affecting variables
// only within the for-loop scope
}
should be removed by any decent optimization algorithm in C++ (and
programmer to, for that matter), right?
/hall
(*)perhaps I should add that how far the optimization is allowed to go
in Fortran can be controlled by setting the options for it. It doesn't
go wild on your code everytime ;-)
--
( - Remove capital X from email to reply - )
Rolf Magnus wrote: Rolf Magnus wrote:
Karl Heinz Buchegger wrote:
hall wrote:
John Harrison wrote:
>#include <string> >#include <iostream> > >std::string dummy; >std::getline(std::cin, dummy); > >which will read a line of text from the standard input. This might >be what you want but that depends on exactly what you are trying >to do. > >john > >
Out of curiosity, is this behaviour guarantied by the standard? Is getline forced (by the standard) to read a string that is never used or is it possible for the compiler to remove this as an optimization?
The compiler can't remove this as it violates principle #1 in optimization: The compiler can optimize away anything it wants, as long as the programms behaviour does not change.
How would the behaviour of the program change?
Sorry, I might have misunderstood. I thought, "hall" was asking if the compiler could optimize the string away, i.e. instead of copying the line into a string and then throwing that string away, just remove the line from cin without copying it anywhere.
No, I was worried that some compiler might remove the entire
[std::cin << var] part of the code, thus making John's suggestion for
how to induce a pause into the program useless.
Optimizing away the actual string is perhaps not so useful. The time it
takes for the user to press enter and read this from the keyboard buffer
into the C++ stream is probably much longer than the time to create and
destroy the string object, and for memory, who has the patience to input
a string long enough to take up more than a fraction of a modern
computers memory ;-) ?
anyway, thanks for sharing your wisdom (all of you)
/hall
--
( - Remove capital X from email to reply - )
hall <Xc***********@yahoo.se> wrote in news:3F**************@yahoo.se: Actually, no. The compiler was not broken. Optimizing can be(*) a rather violent process in Fortran and may remove functionality in the code, resulting in missing outputs, ignored function calls etc. And this is of course why I wanted to make sure that C++ did not support anything like this in its standard.
Well at least, there would be a switch which should read "break
optimizer" ;)
However, code such as for (unsigned long int i=0; i<255^4; i++){
255^4 means 251, ^ is bitwise-xor! (Not power as one
might think.).
float f=... // silly calculations affecting variables // only within the for-loop scope }
should be removed by any decent optimization algorithm in C++ (and programmer to, for that matter), right?
Depends on what else is done within the for scope. As long as it works
the same it probably would.
But do not think that a C++ optimizer will do your thinking as the
Fortran one would have. At least I haven't seen one that will kill
variables that are not used but that will get a value. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: aurgathor |
last post by:
I have a code that reads in 2 shorts, and checks if they are
in a given range. I'd like to modify it to use a predefined
default value if nothing, but as newline is entered. What
would be the...
|
by: jerrygarciuh |
last post by:
Hello,
I have been playing with various Googled solutions for capturing the <Enter>
key to suppress form submission.
My first question is whether anyone has a script that works in all common...
|
by: Les Paul |
last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's
just like a Wiki page, but my own very simple version. It's a page full
of plain old HTML content, and then at the bottom,...
|
by: Susan Bricker |
last post by:
I know that I saw some information concerning the <shift>+<enter>
combination use to bypass launching an Access mdb application and enter
the Access design workspace. Would someone please direct...
|
by: Rocio |
last post by:
I have a aspx page, with 2 controls (ascx).
Control1 contains a SEARCH button, and textbox to enter the string to
search for.
Control 2 contains a LOGIN button, and 2 text boxes to enter the...
|
by: Nicky |
last post by:
hi, all
I know we can do this by some jscript. But is there a way to do it in
asp.net c# code?
In our project, users could sumit a piece of html code and I need to remove
all html tag out. What's...
|
by: peshrad |
last post by:
Hi !
I'm working with Win 2K and Visual Studio 2003.
I have a problem because pressing <ENTER> in a text input control causes a
postback of my web form.
Here comes some example code (already...
|
by: Rainer Queck |
last post by:
Hi NG,
one more question about thread safety of generic lists.
Let's assume a generic list:
List<MyTyp> aList = new List<MyType>();
Would it be a problem if one thread removes elements from...
|
by: Tom Edelbrok |
last post by:
I'm using VS 2005 to develop an intranet asp.net web application and I get a
weird situation. If I start out with any ASPX page that contains an
ImageButton control followed by a TextBox control,...
|
by: Stevecbl |
last post by:
Hello,
I have an application that works on a PPC 2003 device but causes a slight problem on a WM 5.0 device. These devices are equipt with barcode scanners.
The problem that I am seeing is that...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
| |