473,735 Members | 11,866 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

<Help> Simple Pause Needed.

OK, this has got to be a simple one and yet I cannot find the answer
in my textbook.

How can I get a simple pause after an output line, that simply waits
for any key to be pressed to move on?

Basically: "Press any key to continue..."

I beleive that I am looking for is something along the lines of a....

cin.get

but do not know the exact syntax for it.

I know it is so simple.... I can make and call functions with eyes
closed yet this illudes me...

Thanks.
Jul 19 '05 #1
31 14334
"da Vinci" <bl***@blank.co m> wrote in message
news:ek******** *************** *********@4ax.c om...
OK, this has got to be a simple one and yet I cannot find the answer
in my textbook.

How can I get a simple pause after an output line, that simply waits
for any key to be pressed to move on?

Basically: "Press any key to continue..."
You can get almost that. With most systems with keyboards,
input is 'line buffered', so your program doesn't see any
input until the user presses the 'Enter' (aka 'return') key.
This lets one edit the input before submitting it to the
program. Think 'fumble-fingers', and 'backspace key' :-)

So for such systems, you can have a 'Press Enter to continue',
in a few different ways.

You could use cin.get(), but I'd only use that as a prelude
to the program's termination, since even though 'get()' only
extracts a single character, the user could easily type in
e.g. ten characters (or more), and those characters would
still be waiting in a 'buffer', and be presented to the
next input request. So you'd need extra code to discard them.
(cin.ignore() etc.)

I find the most useful general purpose way to do this is
with the (nonmember) 'std::getline() ' function, which will
extract all characters up to the first newline character, and
store them in a std::string object. (The newline character itself
is extracted but not stored). Then just throw the string away,
or give it to your cat. :-)

Example:

#include <iostream>
#include <string>

void wait4user()
{
std::string response;
std::cout << "Press Enter to continue";
std::getline(st d::cin, response);
}

int main()
{
std::cout << "Hello";
wait4user();
std::cout << "world\n";
wait4user();
return 0;
}


I beleive that I am looking for is something along the lines of a....

cin.get
cin.get() would work, but has the disadvantage I cited above.

but do not know the exact syntax for it.
That's what books are for. :-) Any good C++ compiler
will also provide documentation for the standard library.

I know it is so simple.... I can make and call functions with eyes
closed yet this illudes me...


Open your eyes. :-)

HTH,
-Mike
Jul 19 '05 #2
Excellent!

It worked like a charm.

I didnt know that info about the cin.get and having characters in the
buffer. Appreciate that! I bet that will save a great deal of heart
ache in the future.

Aw, come on.... keeping your eyes open during the ride is no fun!! :-)

Thanks again.
On Tue, 30 Sep 2003 22:19:06 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote:
"da Vinci" <bl***@blank.co m> wrote in message
news:ek******* *************** **********@4ax. com...
OK, this has got to be a simple one and yet I cannot find the answer
in my textbook.

How can I get a simple pause after an output line, that simply waits
for any key to be pressed to move on?

Basically: "Press any key to continue..."


You can get almost that. With most systems with keyboards,
input is 'line buffered', so your program doesn't see any
input until the user presses the 'Enter' (aka 'return') key.
This lets one edit the input before submitting it to the
program. Think 'fumble-fingers', and 'backspace key' :-)

So for such systems, you can have a 'Press Enter to continue',
in a few different ways.

You could use cin.get(), but I'd only use that as a prelude
to the program's termination, since even though 'get()' only
extracts a single character, the user could easily type in
e.g. ten characters (or more), and those characters would
still be waiting in a 'buffer', and be presented to the
next input request. So you'd need extra code to discard them.
(cin.ignore( ) etc.)

I find the most useful general purpose way to do this is
with the (nonmember) 'std::getline() ' function, which will
extract all characters up to the first newline character, and
store them in a std::string object. (The newline character itself
is extracted but not stored). Then just throw the string away,
or give it to your cat. :-)

Example:

#include <iostream>
#include <string>

void wait4user()
{
std::string response;
std::cout << "Press Enter to continue";
std::getline(st d::cin, response);
}

int main()
{
std::cout << "Hello";
wait4user();
std::cout << "world\n";
wait4user();
return 0;
}


I beleive that I am looking for is something along the lines of a....

cin.get


cin.get() would work, but has the disadvantage I cited above.

but do not know the exact syntax for it.


That's what books are for. :-) Any good C++ compiler
will also provide documentation for the standard library.

I know it is so simple.... I can make and call functions with eyes
closed yet this illudes me...


Open your eyes. :-)

HTH,
-Mike


Jul 19 '05 #3
da Vinci wrote:
Excellent!


Please don't top-post. Read section 5 of the FAQ for posting guidelines.

http://www.parashift.com/c++-faq-lite/

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4
> OK, this has got to be a simple one and yet I cannot find the answer
in my textbook.

How can I get a simple pause after an output line, that simply waits
for any key to be pressed to move on?

Basically: "Press any key to continue..."

I beleive that I am looking for is something along the lines of a....

cin.get

but do not know the exact syntax for it.


There is no way in standard C++ (which is the topic of this newsgroup)
to do something like that. The closest thing would be

std::string dummy;
std::cin >> dummy;

which will wait for <enter>. If you want to detect a key without <enter>,
you will have to use something from your implementation. For further
informations, please ask in a newsgroup supporting your compiler.

Jul 19 '05 #5
> > OK, this has got to be a simple one and yet I cannot find the answer
in my textbook.

How can I get a simple pause after an output line, that simply waits
for any key to be pressed to move on?

Basically: "Press any key to continue..."

I beleive that I am looking for is something along the lines of a....

cin.get

but do not know the exact syntax for it.


There is no way in standard C++ (which is the topic of this newsgroup)
to do something like that. The closest thing would be

std::string dummy;
std::cin >> dummy;

which will wait for <enter>. If you want to detect a key without <enter>,
you will have to use something from your implementation. For further
informations, please ask in a newsgroup supporting your compiler.


http://www.parashift.com/c++-faq-lit...t.html#faq-5.9
Jonathan (pressed send to early)
Jul 19 '05 #6
"Jonathan Mcdougall" <jo************ ***@DELyahoo.ca > wrote in message
news:5n******** *************@w eber.videotron. net...
OK, this has got to be a simple one and yet I cannot find the answer
in my textbook.

How can I get a simple pause after an output line, that simply waits
for any key to be pressed to move on?

Basically: "Press any key to continue..."

I beleive that I am looking for is something along the lines of a....

cin.get

but do not know the exact syntax for it.
There is no way in standard C++ (which is the topic of this newsgroup)
to do something like that. The closest thing would be

std::string dummy;
std::cin >> dummy;


Warning:

This might be sufficient for some circumstances, but
note that there could be a problem.

If this construct is used in a place before subsequent
input is requested, and if in response to the above,
the user supplies input with embedded whitespace,
e.g. "OK I did", only the "OK" will be extracted,
and "I did" is still there waiting, and will be supplied
to the next input.
Use std::getline() instead.

which will wait for <enter>. If you want to detect a key without <enter>,
you will have to use something from your implementation. For further
informations, please ask in a newsgroup supporting your compiler.


Agreed.

-Mike
Jul 19 '05 #7
"da Vinci" <bl***@blank.co m> wrote in message
news:6q******** *************** *********@4ax.c om...
Excellent!

It worked like a charm.

I didnt know that info about the cin.get
Start reading, my friend. :-)
and having characters in the
buffer.
Perhaps I was not clear enough, but note that my
remarks about 'buffer' were only a generalization
about 'most systems' you're likely to encounter.

From the program's perspective, this issue is not
limited to just cin.get() but to all input operations.
The reason what I showed works, is that by definition,
std::getline() reads everything on a whole line,
and most systems with keyboards will buffer a line
at a time.

The language itself knows nothing about such operating
system buffers, and only 'sees' one character at a time
(The OS controls and supplies these characters to your
program).
Appreciate that! I bet that will save a great deal of heart
ache in the future.


Glad I could help.

BTW please don't top post. If you don't know what I'm
talking about, see the link Kevin provided.

-Mike
Jul 19 '05 #8
On Tue, 30 Sep 2003 23:49:09 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote:
Start reading, my friend. :-)
I tried. :) My textbook isn't very good, IMO, and the Schildt C/C++ SE
reference manual does cover get() and getline(), but not to the point
where I could get what I wanted out of it with my 1 month of C++
experiance. This sure isnt Turbo Pascal. :-)
BTW please don't top post. If you don't know what I'm
talking about, see the link Kevin provided.


Had no idea, but now that I read the FAQ link, I will ensure I do not
do it any longer. I must have missed it the first time I read through
it.

Thanks.
Jul 19 '05 #9
>Glad I could help.

-Mike


Just in case you wanted to see how I used it, this is the program I
was working on. Way beyond what the assignment required, but I have
always gone above and beyond.

Hopefully I am posting the code right. I read the FAQ portion of it
and hope I get it right. (I hate being the new guy!!) I cut out alot
of the white space to save room. This is not how I format everything
in the code.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

// Declare Functions Here

int Display_Main_Me nu (int);
void View_Totals (int, int, int, int, int);
int Check_Number (int);
void Wait ();
void Final_Data (int, int, int, int, int);

int main (int)
{

// Variables for various program functions

int day=1, prod_num, quantity_today= 0;
int prod_1=0, prod_2=0, prod_3=0, prod_4=0, prod_5=0;
int main_selection, good_or_bad=1;

// Variables for continue statements

char cont='N';
char day_done = 'N';

while ( day <= 7 )
{

main_selection = Display_Main_Me nu (day); // Calls Function

switch (main_selection )
{
case 1: // Input Data - While & Switch Loops
system ("cls"); // Clear Screen Command
cont = 'N'; // Must have to avoid infinite looping!!!
while (cont != 'Y' )
{
while ( good_or_bad != 0 )
{
cout << "\n\n\nEnte r product number (1-5): ";
cin >> prod_num;
good_or_bad = Check_Number(pr od_num);
} // Ends Inner While

good_or_bad = 1; // Reset it to avoid infinite looping!!!

cout << "Enter quantity sold on day " << day << ": ";
cin >> quantity_today;
cout << "\n\nYou sold " << quantity_today
<< " units of product " << prod_num << ".";
cout << "\nIs this correct (Y/N)?";
cin >> cont;

if ( cont == 'y' ) // Sets upper case letter
cont = 'Y';
} // Ends Middle While

switch (prod_num) // Add Quantity to Product Count
{

case 1:
prod_1 = prod_1 + quantity_today;
break;
case 2:
prod_2 = prod_2 + quantity_today;
break;
case 3:
prod_3 = prod_3 + quantity_today;
break;
case 4:
prod_4 = prod_4 + quantity_today;
break;
case 5:
prod_5 = prod_5 + quantity_today;
break;
} // Ends Inner Switch

break;

case 2: // User Selected "View Totals" - Call Function
View_Totals (prod_1, prod_2, prod_3, prod_4, prod_5);
break;

case 3: // User Wants to Advanced One Day
day++;
break;

case 4: // User Wants to End Input Session
day = 8;
break;

} // Ends Outer Switch

} // Ends While

// Display the final sales report from the data entered.
Final_Data (prod_1, prod_2, prod_3, prod_4, prod_5);

return 0;
} // Ends Main

/*This function will be called to display the main menu
for the user. The variable passed out of this function
tells the *main* what menu the user wishes to access.*/

int Display_Main_Me nu (int daynum)
{

int selection;

system ("cls"); // Clear Screen Command

cout << "\n\n\nDay " << daynum;
cout << "\n\n*****M AIN MENU*****";
cout << "\n\n1: Enter Data";
cout << "\n2: View Current Totals";
cout << "\n3: Continue to day " << ( daynum + 1 );
cout << "\n4: End Input Session";
cout << "\n\n\nSelectio n: ";
cin >> selection;

return selection;

}

/*This function will be called to display the totals currently
on file from previous input. There is no value returned from
this function.*/

void View_Totals(int prod1,int prod2,int prod3,int prod4,int prod5)
{
system ("cls"); // Clear Screen Command
cout << "\n\n\nYour current totals are:" << endl;
cout << "\nProduct 1: " << prod1 << endl;
cout << "Product 2: " << prod2 << endl;
cout << "Product 3: " << prod3 << endl;
cout << "Product 4: " << prod4 << endl;
cout << "Product 5: " << prod5 << endl;

Wait(); // Creates a "Press Enter to continue" statement
}

/* This function determines whether the input product number is
correct. The value must be an integer between 1 and 5. If it is
valid, then a value of 0 is returned. If it is invalid, then a
value of 1 is returned.*/

int Check_Number(in t prodnum)
{
int return_this;

switch (prodnum)
{
case 1:
case 2:
case 3:
case 4:
case 5:
return_this = 0;
break;
default:
return_this = 1;
break;
}
return return_this;
}
/* This function will use a string to create a pause until the user
hits the enter key. Any other characters entered will be ignored.*/

void Wait()
{
string response;
cout << "Press Enter to continue";
getline(cin, response);
}

/* This function will have the number of products passed into it.
From this data, it will determine the amount of each product sold
and total gross sales for the week. Then, it will display the data.*/

void Final_Data(int prod1,int prod2,int prod3,int prod4,int prod5)
{
double p1total, p2total, p3total, p4total, p5total, gross;

p1total = (prod1 * 2.98);
p2total = (prod2 * 4.50);
p3total = (prod3 * 9.98);
p4total = (prod4 * 4.49);
p5total = (prod5 * 6.87);
gross = (p1total + p2total + p3total + p4total + p5total);

system ("cls"); // Clear Screen

cout << "\n\n\nYour final sales report for this week is as follows:";

cout << "\n\nProduc t Number\tAmount Sold\tTotal Income";
cout << "\n1\t\t" << prod1 << "\t\t" << "$" << p1total;
cout << "\n2\t\t" << prod2 << "\t\t" << "$" << p2total;
cout << "\n3\t\t" << prod3 << "\t\t" << "$" << p3total;
cout << "\n4\t\t" << prod4 << "\t\t" << "$" << p4total;
cout << "\n5\t\t" << prod5 << "\t\t" << "$" << p5total;

cout << "\n\nYour total gross sales for this week is $" << gross
<< "\n\n\n";
}
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1690
by: Chuck | last post by:
Hello, I have an Access XP database that has several fields. One of the fields is a text field and has an account number that is preceeded by a text character, like: F102354. Every account number has this prefix and is always followed by a 6 digit number. What I need to do is to drop the character at the beginning and keep the remaining part of the account number.
1
1554
by: Chuck | last post by:
I have a question tht I'm sure someone out there can answer. A friend of mine has developed an Access application that is a very business specific (niche type of application). The application would only be used by one type of business but there are a lot of these businesses out there along without too much competition. He is interested in selling the application (a runtime version of Access would be supplied as well) but he works...
1
719
by: Chuck | last post by:
I have a query that uses the query with the critera set to . We I run the query the msgbox pops up asking for the name information to be entered, but on the top (blue background) of the message box it says" Enter Parameter Value ". Is there anyway to change this to something that is not as generic. I appreciate any help you can give. Thanks Chuck
2
309
by: Chuck | last post by:
I am trying to retrive one field from the "company info" table that contains several fields but I want the "company name" field. There is only one record in this table. When I entered the code below (which I copied out of a book). I get the following error. The hickup comes in the "dim db as database" line. However if I comment this out. It doesn't like the next line either. I appreciate any help you can give me. Also is this a problem...
1
2950
by: Chuck | last post by:
I appreciate any help!!! I have an application that has 4 different forms that display the same information except for the fact that they are fed from 4 different parameter queries. The 4 different forms are activated by 4 different buttons on the main switchboard. Of course, this makes me change 4 different forms when I make a change to any of them. Is there anyway to open one form from different queries when different buttons are...
1
1488
by: WCBF | last post by:
I have an Access application that was written in ver 2002. I have distributed the application to our other offices using the Developers Toolkit and installed a runtime version of Access. At that time no other versions of Access was on their computer systems. Now, if Access 2003 (full version) is installed on each machine what will this cause to the run time version of Access as well as the application. Is their anyway to control the...
3
2078
by: WCBF | last post by:
I know this code has worked before but now appears to get a compile error. The code it seems to choke on line 12. I receive the following message and it highlights the "rst!" statement. Error received: Microsoft Visual Basic Compile error: Type declaration character does not match declared data type Private Sub Form_Load() 1 Dim db As Database
0
8962
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9463
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9327
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6747
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6049
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4559
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.