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
Related
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.
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;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am having issues with converting age in years to seconds. It comes up with a wrong number and a negative one. Can someone please help?
#include <iostream>
using namespace std;
int main()
{
int years;
years = years;
cout << "How many years have you lived: " ;
cin >> years;
cout << "You have lived: " << years; cout << " years:" << endl;
int days = 0.5 + (years * 365.25);
cout << days; cout << " days" << endl;
int hours = days * 24;
cout << hours; cout << " hours" << endl;
int minutes = hours * 60;
cout << minutes; cout << " minutes" << endl;
int seconds = minutes * 60;
cout << seconds; cout << " seconds" << endl;
return 0;
}
You are getting wrong/negative seconds because of overflow problem. int can't hold so big numbers (seconds).
So, use long long minutes and long long seconds instead of int
And, don't forget to remove the line
years = years;
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
every time I try to compile it I get an error message along the lines of
"exercise3.cpp:10:49: error: expected ‘;’ before ‘endl’"
and if I add the ; before endl I get the error message
"exercise3.cpp:8:71: error: statement cannot resolve address of overloaded function"
I have no idea what the problem is with this, so any help would be appreciated.
code: (sorry about not being lined right but i assure you that it is.)
#include "iostream"
using namespace std;
int main()
{
cout << "hello there!\n";
cout << "here is 5: "<< 5 << "\n";
cout << "the manipulator end1 wrotes a new line to the screen."; endl;
cout << "here is a very bug number:\t" << 70000 endl;
cout << "here is the sum of 8 and 5:\t" << 8+5 endl;
cout << "heres a fraction:\t\t" << (float) 5/8 endl;
cout << "and a very big number:\t" << (double) 70000*70000 endl;
cout << "you really dont have to type this line XD\n";
cout << "or this one :P cause its not really needed!\n";
return 0;
}
You need to insert << before endl.
cout << "the manipulator end1 wrotes a new line to the screen."; endl;
Should be
cout << "the manipulator end1 wrotes a new line to the screen." << endl;
And so on
You need << before each endl.