Press Enter to continue or q to quit c++ - do-loops

I'm writing a c++ program that displays 2 outputs of a man to make it look like he is jumping if you press enter. If you enter 'q', the program is supposed to stop. This is as far as I have gotten.
// This program will display a jumping man.
include
include
using namespace std;
int main ()
{
string user_input;
do
{
cout << endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl;
cout << " O" << endl;
cout << " /|\\" << endl;
cout << " ( )" << endl;
cout << "------------------------------------------------------------" << endl;
cout << "Press ENTER to continue or enter q to quit:";
getline(cin, user_input);
cout << endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl;
cout << " \\O/" << endl;
cout << " | " << endl;
cout << " / \\" << endl << endl;
cout << "------------------------------------------------------------" <<endl;
cout << "Press ENTER to continue or enter q to quit:";
} while(getline(cin, user_input));
return 0;
}
I have been trying for hours and I still can not figure out how to stop the program if you enter q.
I've tried variations of the while statement such as
while(getline(cin,user_input) && user_input =! 'q')
but it doesn't work. Any help at all would be much appreciated.

This seems like homework which means this isn't the place ask. But I would assume it might have something to do with there not being any if statements to check the value given by the user. And your other attempt failed because of getline getting the entire newline not just the 'q' in this case. Here's more Getline keeps on getting newline character. How can I avoid this?. You could replace the check you tried with while(getline(cin,string) && string != "q\n")
Edit: I wanted this to be a comment but lack reputation.

So I finally figured it out this morning and thought I would share for future reference.
include
include
using namespace std;
int main ()
{
string user_input;
int counter = 0;
do
{
if ( counter % 2 == 0)
{
cout << endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl;
cout << " O" << endl;
cout << " /|\\" << endl;
cout << " ( )" << endl;
cout << "------------------------------------------------------------" << endl;
}
if ( counter % 2 != 0)
{
cout << endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl << endl
<< endl << endl << endl << endl << endl << endl;
cout << " \\O/" << endl;
cout << " | " << endl;
cout << " / \\" << endl << endl;
cout << "------------------------------------------------------------" <<endl;
}
cout << "Press ENTER to continue or enter q to quit:";
getline (cin, user_input);
if (user_input == "q")
{
return 0;
}
counter = counter + 1;
} while (user_input != "q");
return 0;
If the loop repeat, denoted by int counter which adds one each loop, is even the program displays the man standing. Then, the prompt "Press ENTER to continue or 'q' to quit: " is displayed. It continues if the user enters ENTER, getline (cin, user_input). If the user enters 'q' the loop breaks (return 0;). If the loop is repeated, if the user presses ENTER, then the counter is now odd and the program displays the man jumping.

Related

Replace printf with cout statements

I'm trying to write a script which could inline replace a printf statement with the cout statement. It could be either in sed or awk.
For example, if printf statement is
printf("int=%d, str=%s", i, s) ;
Then it should be replaced with :
cout << "int=" << i << ", str=" << s;
printf statement could have any no. of arguments. Accordingly, cout statement should be generated and replaced inline.
Other such examples could be:
printf("Statement2: %d %d %s %f", i, j, s ,f);
printf("statement3: %d %f",
i,
f);
CString failedMsg;
failedMsg.Format(_T("FAILED Triggered=%d expected=%d in %s"),
(int)i,
(int)j,
static_cast<LPCTSTR>(__TFUNCTION__));
Output:
cout << "Statement2: " << i << " " << j << " " << s << " " << f;
cout << "statement3: " << i << " " << f;
std::stringstream failedMsg;
failedMsg << "FAILED Triggered=" << (int)i
<< " expected=" << (int)i
<< " in " << __TFUNCTION__;
Yes, I'm new to StackOverflow. I'll learn the way how it works and how to ask for help. But for right now I'll just request for your gracious support to solve this.
Thanks.
───▄██▄─██▄───▄
─▄██████████▄███▄
─▌████████████▌
▐▐█░█▌░▀████▀░░
░▐▄▐▄░░░▐▄▐▄░░░░
using Perl if your printf function is on a single line not separate line; otherwise I think you cannot
for something like:
printf("int=%d, str=%s", i, s) ;
std::cout << "one" << '\n'; // just for sure
printf("one=%d, two=%d, three=%d, four=%d, five=%d", a, b , c, d , e);
for( int i = 0; i < 10; ++i ){
std::cout << i << '\n';
}
you can:
perl -ne '$T=0;/printf/ && s/printf|[ ",;()]+|%[a-z]/ /g && (#A=split) && ($T=1);if($T){$s=($#A+1)/2;$n=0;print "std::cout << ";print "\"$A[$n] \" << $A[$n+++$s] << " for 1..$s;print q("\n":),"\n";}else{print}' file
the output:
std::cout << "int= " << i << "str= " << s << "\n":
std::cout << "one" << '\n'; // just for sure
std::cout << "one= " << a << "two= " << b << "three= " << c << "four= " << d << "five= " << e << "\n":
for( int i = 0; i < 10; ++i ){
std::cout << i << '\n';
}
For something like:
printf("Statement2: %d %d %s %f", i, j, s ,f);
std::cout << "one" << '\n'; // just for sure
printf("Statement2: %d %d %s %f", i, j, s ,f);
for( int i = 0; i < 10; ++i ){
std::cout << i << '\n';
}
You can use: ( as script.sh )
perl -ne '$T=0;/printf/ && s/printf|[ ",;()]+|%[a-z]/ /g && (#A=split) && ($T=1);if($T){$n=0;print "std::cout << \"$A[0] \" << ";print " $A[++$n] <<" for 1..$#A-1;print q( "\n"),"\n"; }else{print}' file
the output:
std::cout << "Statement2: " << i << j << s << "\n"
std::cout << "one" << '\n'; // just for sure
std::cout << "Statement2: " << i << j << s << "\n"
for( int i = 0; i < 10; ++i ){
std::cout << i << '\n';
}

display the results c++ program

I need help with setw(). I have this fumction :
void PrintRecord(PLAYER &m, ostream &cout)
{
cout << setw(3) << m.PlayerID << '\t' << m.LastName << ',' << setw(4)
<< m.FirstName;
if ( m.Hits > 0 || m.Walks > 0 || m.Outs > 0 )
{
cout << "\t\t" << setprecision(3) << fixed << m.Hits << '\t'
<< m.Walks << '\t' << m.Outs << '\t' << m.BattingAvg
<< '\t' << m.OnBaseAvg << endl;
}
else
{
cout << endl;
}
}
which is not printing my data in columns.
i want to print my data on alighn colmns,
thanks
You should set the same width for each value in a column by calling setw before each value to be printed. Usually you won't need to use '\t' when using setw. Unlike setprecision, setw is not "sticky" so you need to set it before each printed value.
cout << setw(3) << m.PlayerID << setw(20) << m.LastName << ',' << setw(20)
<< m.FirstName;
if ( m.Hits > 0 || m.Walks > 0 || m.Outs > 0 )
{
cout << setw(20) << setprecision(3) << fixed << m.Hits << setw(20)
<< m.Walks << setw(20) << m.Outs << setw(20) << m.BattingAvg
<< setw(20) << m.OnBaseAvg << endl;
}

C++ setprecision only cuts off the digits.. it does not round

double d = 8.19999999999999999999;
cout << d << "\n";
cout << fixed << d << "\n";
cout << setprecision(15) << d << "\n";
cout << fixed << setprecision(15) << d << "\n";
output
8.2
8.200000
8.199999999999999
8.199999999999999
Can I change that so it prints
8.2
8.200000
8.200000000000000
8.200000000000000
instead?

End program after breaking from loop

while (1)
{
cout << "Enter number and unit: ";
cin >> number;
cin >> unit;
if (unit == "in")
{
result = 2.54f * number;
cout << number << " Inches are " << result << " centimeters!" << endl;
}
else if (unit == "ft")
{
result = 0.3f * number;
cout << number << " Feet are " << result << " meters!" << endl;
}
else if (unit == "yd")
{
result = 0.9f * number;
cout << number << " Yards are " << result << " meters!" << endl;
}
else if (unit == "mi")
{
result = 1.6f * number;
cout << number << " Miles are " << result << " kilometers!" << endl;
}
else
{
cout << "\tINVALID!!!\n...the program will now exit...\n";
cin.clear();
cin >> unit;
cin >> number;
break;
}
}
cout << "Thank you for using English to Metric!\n" << endl;
return 0;}
What am I missing in order to get the last cout and exit the program without having to input anything after break?
The break in the else clause will exit the loop, but immediately before it you perform
cin.clear();
cin >> unit;
cin >> number;
break;
The cin >>s are why the program is waiting for additional input.
Without them, the cin.clear() is unnecessary, unless you plan to do additional cin operations later.
else
{
std::cin.clear(); // optional
break;
}
} // end while
std::cout << "Thank you\n";
Will work.

C++ Numeric values are not printing correctly

Main Program
#include <iostream>
#include <iomanip>
#include "pizza.h"
using namespace std;
int main() {
menuItem item;
menu:
item.getMenu();//Menu Prompt
cout << "\nI would like number: " << flush;
int menuChoice;
cin >> menuChoice;
while (menuChoice < 1 || menuChoice > 3) {
cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
}
if (menuChoice == 1) {
item.getPizza();
}
if (menuChoice == 2) {
item.getSandwhich();
}
if (menuChoice == 3) {
item.getSide();
}
int ready4checkout;
cout << "\n\n~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~" << endl;
cout << "\nAre you ready to checkout?\n" << endl;
cout << "checkout = 1\nAdd to the order = 2" << endl;
cout << "\nI would like to:" << flush;
cin >> ready4checkout;
if (ready4checkout == 1) {
item.addcheckout();
}
if (ready4checkout == 2) {
goto menu;
}
cin.ignore();
cin.get();
}
Header File
#include<iostream>
#include <iomanip>
using namespace std;
class menuItem {
protected:
float smallPizza = 5.99;
float mediumPizza = 6.99;
float largePizza = 9.99;
float halfSandwhich = 6.00;
float wholeSandwhich = 8.00;
float bonelessWings = .70;
float breadStix = 4.99;
public:
float checkout;
float pizzaCheck;
float sandCheck;
float sideCheck;
menuItem item(float smallPizza, float mediumPizza, float largePizza, float halfSandwhich, float wholeSandwhich, float bonelessWings, float breadStix,float checkout);
string getMenu() {
string menu;
cout << "~*~*~*~*~*~*~*~ Welcome to Pizza Italiano! ~*~*~*~*~*~*~*~*~\n\n" << endl;
cout << "~~~~~~~~~~~~~~~~~ Main Menu ~~~~~~~~~~~~~~~~~~" << endl;
cout << "Enter the value of the food that you wish to devour:\n" << endl;
cout << "\n1.Pizza\n2.Sandwhich\n3.Appetizer" << endl;
return menu;
}
string getPizza() {
float pizzaCheck;
string pizza;
float pizzaSize;
pizza:
cout << " \n~~~~~~ How would you like your pizza made? ~~~~~~\n" << endl;
cout << " Size" << endl;
cout << "-----------" << endl;
cout << "1. Large $9.99 \n2. Medium $6.99 \n3. Small $5.99" << endl;
cout << "\nI would like the number: " << flush;
cin >> pizzaSize;
cin.ignore();
while (pizzaSize < 1 || pizzaSize > 3) {
cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
goto pizza;
}
if(pizzaSize == 1) {
cout << "\n****You have selected a Large pizza****" << endl;
pizzaCheck = pizzaSize;
pizzaCheck = largePizza + pizzaCheck;
} else if(pizzaSize == 2) {
cout << "\n****You have selected a Medium Pizza****" << endl;
pizzaCheck = pizzaSize;
pizzaCheck = mediumPizza + pizzaCheck;
} else {
cout << "\n****You have selected a Personal pizza****" << endl;
pizzaCheck = pizzaSize;
pizzaCheck = smallPizza + pizzaCheck;
}
cin.get();
return pizza;
}
string getSandwhich() {
string sandwhich;
float sandSize;
Sandwhich:
cout << "~~~~~~~~~~~~ What size would you like your sandwhich to be? ~~~~~~~~~~~~~~~\n\n" << endl;
cout << "1. Six inch $6.00\n2. Twelve inch $8.00" << endl;
cout << "I would like number:" << flush;
cin >> sandSize;
while (sandSize < 1 || sandSize > 2) {
cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
goto Sandwhich;
}
if (sandSize == 1) {
cout << "**** You have selected a six inch sub ****" << endl;
sandCheck = halfSandwhich + sandSize;
} else {
cout << "**** You have selected a 12 inch sub ****" << endl;
sandCheck = wholeSandwhich + sandSize;
}
return sandwhich;
}
string getSide() {
string Appetizer;
float side;
sides:
cout << "~~~~~~~~~~~~~~~ Which appetizer would you like? ~~~~~~~~~~~~~~~~~\n\n" << endl;
cout << "1. Boneless Wings .70\n2. BreadStix $4.99\n\n" << endl;
cout << "I would like the number:" << flush;
cin >> side;
while (side < 1 || side > 2) {
cout << "\nINVAILD INPUT:\nEnter a Value that corresponds with your menu choice" << endl;
goto sides;
}
if (side == 1) {
cout << "\n**** You have selected Boneless wings ****\n" << endl;
cout << "How many wings would you like?\n" << endl;
cout << "I would like this many wings:" << flush;
int wings;
cin >> wings;
cout << "\n**** You will recieve " << wings << " wings. ****" << endl;
side = sideCheck;
sideCheck = bonelessWings += sideCheck;
} else {
cout << "\n**** You have selected an order of breadstix ****" << endl;
side = sideCheck;
sideCheck = breadStix += sideCheck;
}
return Appetizer;
}
float addcheckout() {
checkout = pizzaCheck;
cout << "\nYour total is " << checkout << endl;
cout << "\nThank You for your purchase!";
return checkout;
}
};
At the end of the program, when it gives the checkout total, it gives a value something like -1.07374e.+008
I am still learning how to code, and I am stumped on this one. Any help would be much appreciated!
I think you shadow your member field pizzaCheck in your method getPizza, so that you actually don‘t write to the member field but to your local var declared at the first line of that method.
Try removing that local var. (I did not test it yet though)

Resources