constant too big C++ [closed] - c++

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.

Related

Print Minimum & Maximum Value Of Data Types in C++ [duplicate]

This question already has answers here:
(C++) INT_MAX and INT_MIN could not be resolved?
(4 answers)
How come INT_MAX and INT_MIN resolve in C++ without <climits> [duplicate]
(1 answer)
Why it seems not necessary to include some STL headers
(2 answers)
C++ code runs with missing header, why?
(1 answer)
Closed 3 months ago.
I was watching a tutorial on YouTube and I seen her coding this to print min and max value of data type. You can see in screenshot below. Her code was working perfectly but mine is not.
Here's My code and It's giving error.
#include <iostream>
using namespace std;
int main(){
// Print Minimum & Maximum Value Of Data Types
cout << "Minimum Value Of Int Is " << INT_MAX << endl;
system("pause>0");
}
The macro INT_MAX is defined in the <climits> header file. You need to include it if you want to use the macro.
If the Youtube video doesn't say anything about that, then perhaps it's not that good source for learning, and you should find another way (and it's much too easy to find bad videos than good, so I recommend you stay away from Youtube).
For a more C++-ish way instead include <limits> and use the std::numeric_limits class template.
More specifically its max static member function:
std::cout << "Max int value is " << std::numeric_limits<int>::max() << '\n';

How can you print out more than one decimal place within the same line 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 1 year ago.
Improve this question
I am new to coding and I was wondering how can you print out two different decimal places within the same line. For instance, the first value should have one decimal place and the second value should have three decimal places. I know how to use setprecision to have different decimal places for different lines, but not how to have it on the same line.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double precision_value = 12.409583385;
cout << fixed << setprecision(2) << precision_value << " " << setprecision(4) << precision_value;
return 0;
}
A few things to note:
In the same print statement, the fixed only needs to be set once and works for the remainder of the stream. The setprecision() can be updated to any value you need after that.
Also, prefer to remove using namespace std; and utilize the namespace std:: wherever required. But, for demonstration purposes, this suffices for your example needs.

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.

Why is using cout in a class function considered bad? [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 8 years ago.
Improve this question
The Assignment was to create a PairList class that held a vector of a Pair class. The Pair class is a template class that holds any two variables of one type.
The code prompting that question:
template <class T>
void PairList<T>::printList()
{
for(unsigned int i = 0; i < this->pList.size(); i++)
{
cout << i+1 << ".\t"
<< (this->pList[i]).getFirst()
<< "\t" <<(this>pList[i]).getSecond()
<< endl;
}
}
Something my teacher said about this code:
"One thing I will say about the ones I like is that both used cout in functions and it doesn't belong. I would like your input as to why it is not correct and what things could have or should have been done to eliminate the use of cout in these functions
This is a warning for now. In the future I will start docking serious points like the 70% I do in the level 1 class."
He wasn't very specific on what functions he wanted and let us create whatever we thought was necessary. I received an A but wanted to know why I shouldn't use cout in this function.
Why is using cout in a utility function a bad thing to do?
What should I do instead?
I'm sorry this is such an ambiguous question. I realize that after posting. Also I'm overwhelmed by all extremely fast feedback!
I suspect that whoever said using cout in a class is bad is thinking that, instead of doing
type function ()
{
. . . .
cout << "WHATEVER" << endl ;
. . . .
}
that you should have
type function (std::ostream &strm)
{
. . . .
strm << "WHATEVER" << endl ;
. . . .
}
There are two issues at play here. Regarding headers, you should not typically place implementation in your header files, so a std::cout command would be inappropriate there. Regarding your class, it greatly depends--in some cases, using cout in a class function is perfectly fine.

Displaying today's date [closed]

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