Exception when comparing an (int)double and (int)int [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Exception when comparing an (int)double and (int)int
IGNORE THIS POST. ACCIDENTLY REPOSTED
Sorry

if((int)time >= 600){ time_s.insert(4, sec);
should be,
if((int)time >= 600){ time_s.insert(3, sec); // digit 3 instead of 4
From your code, I suppose the string size is 6 characters (0 to 4 and 5th character as nul). Inserting 2 digit at 4th position and 5th position would overwrite nul.
Do cross verify, as I have made a guess seeing your code.

Related

Getting two different results whilie printing string::npos without typecasting and with typecasting [duplicate]

This question already has answers here:
Is std::string::npos == -1 always true?
(3 answers)
Why is (18446744073709551615 == -1) true?
(4 answers)
Closed 9 months ago.
Can anyone please help me to solve this doubt?
When I'm executing the below two lines of code, I'm getting two different results. What's the reason?
cout<<endl<<"npos (int): "<<(int)string::npos<<endl;
cout<<endl<<"npos: "<<string::npos<<endl;
OUTPUT:
npos (int): -1
npos: 18446744073709551615

Is there a way for C++ to detect that when a value ends with a certain number it'll do an if-statement? [duplicate]

This question already has answers here:
How does the modulus operator work?
(5 answers)
Closed 2 years ago.
it's my first time posting in here! I know that my question doesn't sound really correct, but I'm really new to programming. In my program, I need that if a user enters a number (1 <= 365) then the program will tell which day is it. Then I came up with a solution but I don't know if C++ can even do it. For example, if A/7= 3.6 and B/7= 2.1 or C/7=2.9 then it'll read the last decimal and do a different if function every time (for example if it ends with .6 program then says it's Thursday and .9 it's Sunday and so on and so on). I hope it kind of makes sense.
Thanks
#include <cstdio>
main()
{
float n, D;
printf("Enter the number of the day n:\n");
scanf("%f", &n);
D = n/7
if (D = )/*here I'm trying that if D ends with some number it'll do this*/
{
printf("It's Monday")
}
}
How about multiply 10 then '%' to get the remainder?

C++ Random doubles from -1 to 1 [duplicate]

This question already has answers here:
generate random double numbers in c++
(10 answers)
Closed 4 years ago.
I need to randomly generate numbers (using random()) from -1 to 1 that are doubles. For example the output would be something like:
1,
-0.3324,
0.7821,
0.9823,
-0.111
etc... this is what I was trying to do walk_Length = (double)rand()%2 - 1;
You might get away with something like
double walk_Length = static_cast<double>(rand()) / RAND_MAX * 2 - 1;

How to limit input length in JFX textField? [duplicate]

This question already has answers here:
JavaFX TextField Array max length of text value
(1 answer)
How to restrict TextField so that it can contain only one '.' character? JavaFX
(3 answers)
Java 8 U40 TextFormatter (JavaFX) to restrict user input only for decimal number
(1 answer)
Numeric TextField for Integers in JavaFX 8 with TextFormatter and/or UnaryOperator
(4 answers)
Closed 5 years ago.
I need to limit input length to 4 digits in JFX textField (field can be empty or have value 0-9999). Below solution works only partially - I can input only digits , but as many as i want - limit do not work. Even if I remove {0,4} from regex and
change IF condition to:
if(newText.matches("\d") && newText.length()>=0 && newText.length()<5)
it doesn't work too. Where is the error?
JFXtextField.textProperty().addListener((obs, oldText, newText) ->
{
if(newText.matches("\\d{0,4}"))
{
newText = newText;
}
else
{
newText = oldText;
}
});

How to Set certain digits for generating random numbers in C++ [duplicate]

This question already has answers here:
How do I scale down numbers from rand()?
(9 answers)
Closed 8 years ago.
Hi. Is there any way to set the size of random numbers?( in random number generator "rand()")
For example I want to generate 10 digits random numbers.
and one more question, how can i set random function to generate numbers between 0 and 1 (for example 0100110110) ?
Im not sure about setting the size of the numbers. However I dont think it would be possible to get each digit to produce just a 0 or 1.
What you can do however is something like below:
ostringstream 10digitNumber;
for(int i = 0 ; i < 10 ; i ++){
v1 = rand() % 2;// generate o or 1
10digitNumber<< v1;// build up a string of 1 and 0
}
int real10DigitNumber = static_cast<int>10digitNumber); // typecast to integer
Please forgive me if my syntax isn't 100 %. Its being awhile since I used c++.