473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Loop while ENTER is held down exit when released

I am trying to write a program that goes through a loop while the user holds down the [ENTER] key and EXIT the loop when the key is no longer depressed.
I got the first part, the loop cycles while the key is depressed but the program just stops when the user no longer holds down the [ENTER] key.
I’m not even sure it’s possible. Thank you!

Expand|Select|Wrap|Line Numbers
  1. double a = 0;
  2.   double b = 3.5;
  3.   double c = 0;
  4.   char ch; ;
  5.   cout << "  PRESS ENTER KEY ";
  6.   ch = cin.get();
  7.   while(ch)
  8.   {
  9.      ch = cin.get();
  10.      c = a * b;
  11.      a = a + 0.01;
  12.      cout << "\n  TOTAL = " << c;
  13.  
  14.       if (ch = !cin.get()) // THIS DOESN’T WORK
  15.       {
  16.          break;
  17.       }
  18.   }
  19.   cout << "\n  FINISHED";
  20.   cout << "\n  TOTAL = " << c
Sep 15 '11 #1
3 3074
Banfa
9,065 Expert Mod 8TB
You are correct it is not possible, or at least it is not possible using platform independent functions and classes such as cin.

What exactly do you think this line does ch = cin.get();?
How about if the user has given no input yet?

What do you think this line does if (ch = !cin.get())? Break it part method call by method call, operator by operator.
Sep 16 '11 #2
Thank you very much for your confirmation.
This is part of a somewhat larger program.
The ch = cin.get(); only gets called when
the user initiates input.
Sep 16 '11 #3
Banfa
9,065 Expert Mod 8TB
True to some extent but cin is often buffered so in fact the program only receives the data from the user when the user presses enter. The the program receives it all at once.
Sep 17 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: junk mail | last post by:
My friend is trying to code a small c program where he wants to force the user to press enter and only enter to continue. Currently he is using getchar() with a loop but you can type any number...
5
by: ewillyb | last post by:
Hi, ASP.NET has some interesting behavior when the user hits the Enter key. If there are multiple ASP:Buttons (rendered as HTML submits) on the form, when the user hits enter, the first button's...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
6
by: Clark Sann | last post by:
Can someone help me understand what object should be used as the lock object? I've seen some programs that use Monitor.Enter(Me). Then, in those same programs, they sometimes use another object. ...
7
by: Marc | last post by:
Hi, I want my user to be able to rename a button control by selcting rename from a menu. This then opens a text box in which to enter the new name in. I want the button control to...
18
by: Zytan | last post by:
I want the same function to be run whether you press Enter or double click the listbox. It seems really verbose to write both handlers to both events everytime, even if they both call the same...
2
by: JWest46088 | last post by:
I want the user to enter information, such as names, and then I want to ask them after they entered the first name if they want to enter another name. How would I do that? This is what I have so...
7
by: Rotsey | last post by:
Hi, I am having a problem trying to select an item with the enter key. I want to work the combo with the keyboard. So when I use the down arrow to browse the list I want to then hit the...
24
by: MichaelK | last post by:
Who knows how to prevent submitting a form on the press Enter button before all fields on the form are filled up. People just enter the first field hit Enter and it submits the form and doing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.