using % on a double in c++ [duplicate] - c++

This question already has answers here:
Can't use modulus on doubles?
(4 answers)
Closed 9 years ago.
im trying to use the % operator on a double in c++, i have done the same in java and it works fine.
is there something im missing here or is this not allowed, sorry im new to c++ so might be making a really stupid error here
double i = full_price_in_pence / 100.0;
double j = full_price_in_pence % 100;
int final_pounds = (int) i;
int final_pence = (int) j;
and these are both double values
full_price_in_pence
full_price_in_pounds

You should use the std::fmod() function from the <cmath> Standard header:
#include <cmath>
// ...
double j = fmod(full_price_in_pence, 100);

% is for integers only, you're looking for fmod.

You cannot use % operator for a double variable. Only int variables are allowed to do that.
You can check some good answers from another question like this; you can find them here.

No, it's not allowed. Operands of the % operator must be of integral types. Use std::fmod() instead.

Related

Using an int variable in system() function c++ [duplicate]

This question already has answers here:
Easiest way to convert int to string in C++
(30 answers)
Closed 12 months ago.
I need using an int veriable in system() function for my c++ program.
for example:
int a = 0;
system("echo "a" ");
but i get an error and i need help about how i use this like that
Error:
C++ user-defined literal operator not found
That's never going to work. C++ doesn't plug integers into strings that way. Instead, you can do:
int a = 42;
std::string s = "echo " + std::to_string (a);
system (s.c_str ());
Also, you might consult this page, in order to learn the language properly.

C++ output does not have decimal point. I wanted to know where did I do wrong [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 2 years ago.
#include<iostream>
using namespace std;
int main ()
{
double avr_apple;
int total_apple, category;
total_apple=13;
category=2;
avr_apple=total_apple/category;
cout<<"average apple is : "<<avr_apple<<endl;
return 0;
}
An int can't hold decimal points. In this line:
total_apple/category;
both total_apple and category are int so when you divide them, you get an int as a result which doesn't have any decimal points.
To fix this, make total_apple variable a double or static_cast it to double:
avr_apple = static_cast<double>(total_apple) / category;
You can also write:
avr_apple = (double) total_apple / category;
Which is the same thing and looks simpler, but is more confusing to me because now I am a bit confused about what is being cast to double, total_apple?, category?, the whole result? Hmm. I am also confused about which type of cast is being applied here, is it a static_cast, is it some other type of cast? So just avoid this.
This also contains a very important lesson, 'A computer only does what it is told to do, nothing more, so make sure you tell it the right thing to do if you want the right result.'

Declared int has random starting value [duplicate]

This question already has answers here:
Variable initialization in C++
(11 answers)
Closed 4 years ago.
I'm very new to C++ and have just started out.
I'm doing a simple exercise where I need to declare a variable with type int and add to it.
Essentially the integer has a starting value of 44 and I have no idea why.
The exercise comes out of a book I'm following.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int sum;
sum = sum + 1;
cout << sum;
return 0;
}
If I run the following code I get the answer of 45, which makes no sense at all to me.
I want to understand why sum has a value of 44, if no value is assigned to it.
I'm using VScode and the g++ compiler.
Thanks!
You have to initialize variable at first.
The correct code would be:
int sum = 0;
sum = sum + 1;
In your case you have undefined behavior.

How does C++ process this [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I have here an equation i can't understand how c++ process this. Can someone explain this operation?
code:
#include <stdio.h>
main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int i = 0;
int num = a[i+++a[++i]]+a[++i+i++];
printf("\nnum1: %d i: %d,num,i);
}
why is the answer num = 9 while index i is just equal to 4;
Using ++ twice in the same expression on the same variable is explicitly undefined by all versions of both the C and C++ standards, and so i does not necessarily equal 4. It could be anything at the whim of the compiler writer.
Never do this. Never use ++ and -- twice in the same expression. There is no way to make any statement about what the resultant value will be, and no experience with what it does with one compiler will mean anything with respect to what another compiler does.

Obscure C++ syntax [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++ Comma Operator
About a year ago I noted some obscure syntax in a coding project I was working on:
table_value = table_index += 2, valueFromTable(table_index);
Does anyone recognise this?, it's like an assignment with an additional statement. This compiled in our entire suite of cross-platform compilers, so I'm pretty certain its valid C++ but I've never seen anything like it.
Any insight would be appreciated.
Gearoid
EDIT: heres some working code:
#include <iostream>
using namespace std ;
int valueFromTable(int a) { return a ; }
int main()
{
int table_index = 0 ;
int table_value = table_index += 2, valueFromTable(12);
cout<<table_value<<endl;
return 0 ;
}
This is the Comma operator.
It's standard C and C++ but heavily frowned upon.
It evaluates both arguments, and returns the result of the second.