Why std::endl can't be used after std::cin [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
What's the diff between \n and endl? Is \n faster?
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter 2 numbers:" << endl;
int a = 0, b = 0;
cin >> a >> b;
cout << "The sum of " << a << " and " << b << " is " << a + b << endl;
return 0;
}

Yes, '\n' is faster here. std::endl will additionally call flush() which is not needed in this case.

Related

How do i print only decimal points in C++ without set limit but with setting precision dynamicly? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
double n;
cout << "Enter a number: " << endl;
cin >> n;
cout<<setprecision(4)<<n;
I want to set floting point precision dynamicly i. e. at runtime instead of static value 4.
You can do something like:
double n;
cout << "Enter a number : " << endl;
cin >> n;
cout << "Enter required precision : ";
int precision;
cin >> precision;
cout << setprecision(precision) << n;

How to cout with input in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Hello guys actually i want to show output with input
e.g:
int main()
int a, b, c;
cin >>a >>b;
c = a-b;
cout <<"Here would be input e.g: 4+5=" << c <<endl;
cout <<"Here would be input e.g: 4-5=" << e <<endl;
If input is in this format a+b and user enter input's then output show after input like this 4+5= and after equal answer would there. Thanx in advance
cout << "Here would be input e.g: " << a << "+" << b<<"=" << c << endl;
cout << "Here would be input e.g: " << a << "-" << b<<"=" << e << endl;
Add the input variables in the cout command as follows:
cout << a << " + " << b<<" = " << c << endl;

Alternative way to push into the stack [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I developed this program to add and the retrieve 15$ in the stack.
I was wondering is there another more efficient way to write my code.
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main()
{
const int MAX = 100;
int count;
stack<int, vector<int> > billStack;
for (int i=0; i<15; i++) {
billStack.push(i); // add 15 bills onto stack
}
cout << "The stack has " << billStack.size() << " bills.\n";
int cash;
cout << "How many bills to retrieve?\n";
cin >> cash;
for (int i = 0; i< cash; i++) {
billStack.pop();
}
cout << "Cash out :" << cash << ". Remaining: " << billStack.size() << endl;
return 0;
}
Considering that you don't ever use the actual contents of the stack, just its size:
#include <iostream>
using namespace std;
int main()
{
int stackSize = 15;
cout << "The stack has " << stackSize << " bills.\n";
int cash;
cout << "How many bills to retrieve?\n";
cin >> cash;
stackSize -= cash;
cout << "Cash out :" << cash << ". Remaining: " << stackSize << endl;
return 0;
}

How to show double 00 like 00:00:59 in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to make a time like this:
00:00:59;
But I can't get it to show 00 instead it show only one 0.
A simple way using std::setw and std::setfill is this:
int hour = 0;
int minute = 0;
int second = 59;
std::cout << std::setw(2) << std::setfill('0') << hour << ":";
std::cout << std::setw(2) << std::setfill('0') << minute << ":";
std::cout << std::setw(2) << std::setfill('0') << second << endl;
It will print:
00:00:59

for-loop construction start in a c++ program [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a problem in the for-construction, I don't know why my counting starts from 0 but you can't write on it(you can write starting with Register 1).I've marked in the code where is the problem.
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string day, date;
int registerCount;
std::cout << "INPUT DATA"
<< std::endl << std::endl
<< "Enter the day in which you want to perform the register: "
<< std::endl;
std::cin >> day;
std::cout << "DATE:" << std::endl;
std::cin >> date;
std::cout << "Enter the number of registers you wanna perfom for the day "
<< day << ":" << std::endl;
std::cin >> registerCount;
std::vector<std::string> registers(registerCount);
for (int i = 0; i < registerCount; ++i) **here is the problem**
{
std::cout << "Register " << i << ":" << std::endl; **it starts from 0 but you can't write on Register 0**
std::getline(std::cin, registers[i]);
}
std::cout << "The data for the day of " << day << " are the following: "
<< std::endl;
std::cout << "DATE: " << date << std::endl;
for (int i = 0; i < registerCount; ++i)
std::cout << registers[i] << std::endl;
}
Use
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Header - #include<limits>
after your
std::cin >> registerCount;
to "eat up" the trailing newline coming from previous std::cin which is cause of skipping of the first input
for (int i = 0; i < registerCount; ++i)
You're defining i as starting at 0 here. Very simply, all you need to do is change the "i = 0" to "i = 1".
The way you're assigning to that vector looks a little bit wrong too. I think you need to use push_back() on the vector in that syntax, but I'm not too familiar with all the different operators for vector.