Displaying today's date [closed] - c++

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
In my program I am trying to display today's date in a day//month//year layout. I've tried using many different ways but I keep getting errors about unsafe use of cTime.
Is there a simple way of getting my program to show the date:
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << (now->tm_year + 1900) << '-'
<< (now->tm_mon + 1) << '-'
<< now->tm_mday
<< endl;
I keep getting "'localtime': This function or variable may be unsafe. Consider using localtime_s instead."
So I did, and now I get
'localtime_s' : function does not take 1 arguments

localtime_s is a Microsoft-specific function that is similar to the C-standard function but with slightly different arguments that are less error-prone. See http://msdn.microsoft.com/en-us/library/a442x3ye.aspx for more information.
That said, if you are using C++, it might be better to use the functions from the chrono part of the C++ standard library. They are easier to use and safe: http://en.cppreference.com/w/cpp/chrono

Unfortunately the standard definition for functions to get from a time_t to formated text all seem to have some potential safety issues, particularly in multi-threaded programs. This leaves you with a choice between going for a vendor specific route or loosing safety in return for portability.
Microsoft specific route:
Details of localtime_s are available here: http://msdn.microsoft.com/en-us/library/a442x3ye%28v=vs.110%29.aspx
Following that something like this should work on visual studio 2012 without warning:
#include <ctime>
#include <iostream>
using namespace std;
int main() {
time_t t = time(0); // get time now
struct tm now;
localtime_s(&now, &t);
cout << (now->tm_year + 1900) << '-'
<< (now->tm_mon + 1) << '-'
<< now->tm_mday
<< endl;
}
However localtime_s is microsoft specific so using it will limit the portability of your code.
Standard compliant but less safe route:
If you prefer standards compliance to safer versions you can use your original version of you code but add the following at the top of the file:
#define _CRT_SECURE_NO_WARNINGS

Related

Why does the Sleep() function behave like this? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm playing around with the Sleep() function in C++ right now, and I am not understanding why this code operates the way it does.
I made a program to find the difference between two times, but it is not working as expected.
#include <iostream>
#include <chrono>
#include <Windows.h>
using namespace std;
int main() {
int timeA = (int)chrono::system_clock::now;
cout << timeA << "\n";
for (int i = 0; i < 5; i++) Sleep(1000);
int timeB = (int)chrono::system_clock::now;
cout << timeB << "\n";
int timeDifference = timeB - timeA;
cout << timeDifference;
cin.get();
return 0;
}
It seems as if the program is setting the variables at the same time, and then sleeping. Is this the case? If so, help me to understand why, please.
now is a function, not an attribute or variable. You failed to call it, and are casting the function pointer itself to int, which will always produce the same value for a given run (on a typical 64 bit system, the low 32 bits of the address where now is located).
Change both lines to use chrono::system_clock::now(), not chrono::system_clock::now.
Note that this is one of the reasons to avoid C-style casts, as well as a reason to compile with warnings turned up; it protects you from casting to wildly incorrect end results, without at least some sort of alert.

mt19937 random number range - weird output 00A8106E [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to create a little game in c++, and therefore I need a function to return random numbers in a specific range.
Most of the answers I found were similar to this one https://stackoverflow.com/a/19728404/5780938, and I think this is the solution I'm looking for.
To test if the function does, what I want it to, I tried outputting the results in several different ways.
At the moment my code looks like this:
#include "stdafx.h"
#include <iostream>
#include <random>
int zufälligeZahl();
int main()
{
using std::cin;
using std::cout;
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
cout << zufälligeZahl << "\n";
return 0;
}
int zufälligeZahl()
{
std::random_device rd;
std::mt19937 zGenerator(rd());
std::uniform_int_distribution<int> uni(1, 13);
int random_integer = uni(zGenerator);
return random_integer;
}
I've tried this in many different ways, but no matter what I do, it doesn't work. Either the output is something like 00A8106E, or I don't get any output at all.
I'm using Visual Studio Community 2015.
You are not calling the function zufälligeZahl, you are printing out the address of the function.
Fix your code by actually calling the function:
cout << zufälligeZahl() << "\n";
You forgot the parentheses.

constant too big C++ [closed]

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 7 years ago.
Improve this question
I am working with a src someone made for a game I am making. the person recently has left and I am trying to pick up the pieces, I don't really know c++ but I was hoping to get some help with a compile issue coming from a specific file I am trying to compile. is there a way I can stretch the limits of a const or perhaps use a different type that will hold much larger values?
The numeric limits for the fundamental types of your implementation are defined in the <limits> header, and the trait class std::numeric_limits provides the values for your machine. That's the end of it, you cannot "beat" those limits with fundamental types. Example (from cppreference.com):
#include <limits>
#include <iostream>
int main()
{
std::cout << "type\tlowest\thighest\n";
std::cout << "int\t"
<< std::numeric_limits<int>::lowest() << '\t'
<< std::numeric_limits<int>::max() << '\n';
std::cout << "float\t"
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t"
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::max() << '\n';
}
Live on Coliru
If you want more than this, or even arbitrary precision/length numbers, then you need to use a multi-precision library, e.g. Boost.Multiprecision.

Strange GCC optimisation bug [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm writing a fairly large application containing many different modules. I've always been programming with the GCC debugging info turned on and all optimisations turned off, for obvious reasons of debugging. Now I've decided that it's time for a release and I've set GCC to optimise to the best of it's abilities (-O3). And this is when the strangest of bugs appeared. Take the following code:
void SomeClass::setValue(int i) { this->iValue = i; }
int SomeClass::getValue() const { return this->iValue; }
Now without optimisations, these work perfectly. With optimisations, the value of SomeClass.iValue is not modified in the setValue() method. In fact, the output of the following:
cout << x.getValue();
x.setValue(5);
cout << x.getValue();
returns
0
0
when the iValue is intialised in the class to 0.
Now the strange part: if I insert the following code into setValue():
void SomeClass::setValue(int i) { cout << "Narf"; this->iValue = i; }
the code works!
Can someone please explain to me what is going on?
did you try checking cout<<x.iValue; ? perhaps the problem is in SomeClass::getValue(); for example, it returning void or being const ? :)
also, just an idea, the optimisation might happen in cout not your actual code as hinted by cout << "Narf";
Firstly, your code is not exactly correct. Getter function should return int instead of void. I think you just typed it incorrectly here, because gcc will not let you compile that (std::cout has no overloaded operator << for type void). What is more, SomeClass.i_value doesn't compile either. Did you mean this->i_value or just i_value?

Simple C++ password program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm learning to use C++ and I decided to create a password program where the user is asked for the password and it compares the user input to the password and returns a wrong or a right. For some reason, this programs always returns a wrong and I'm not sure why. It must be something to do with comparing the strings but I'm not sure.
#include <iostream>
#include <string>
using namespace std;
int main(){
string pass = "password";
string input;
cout << "What is your password: ";
cin >> input;
if (input==pass){
cout << "Correct" << endl;
}else{
cout << "Wrong" << endl;
}
return 0;
}
I would love some help from programmers who are in any way more well versed in C++ as I've just transferred over to C++ from Python and the transitions a bit rocky.
1.you could use compare function, to see:http://www.cplusplus.com/reference/string/string/compare/
2.you should debug at line if (input==pass){
to print pass and input and check if they are the same.
I found I needed to:
#include <string>
to get the definition of the insertion operator (for cin >> input;) and std::string::operator==() (for if (input==pass)). Once I did that, it worked fine in Visual C++.
What compiler are you using?