Output is not what I want C++ [duplicate] - c++

This question already has answers here:
How to output float to cout without scientific notation or trailing zeros?
(3 answers)
Closed 3 years ago.
I got some problems . I input a=5.999 with d=1 and expected output is 6 but its output is 6.0. I mean I want to delete .0 in my output if it had.
Example: Input:
a=3.864361,d=3,Output expected: 3.864/ Input:a=5.9577,d=1 Output expected: 6/ Input:
a=0.07266,d=3,Output expected:0.073.
#include <iomanip>
using namespace std;
int main()
{
float a;
int d;
cin>>a>>d;
cout<<setprecision(d)<<fixed<<a;
return 0;
}

Sadly, this is one example I know of where C++ solution is purely inferior to C. To the best of my knowledge, there is no way to do that with C++ streams.
On the other hand, C has printf format specifier exactly for that: %g
Following code will do what you want (don't forget to include stdio.h):
printf("%.1g\n", a);

Related

Cout works well on wrong-written function [duplicate]

This question already has answers here:
Why is the phrase: "undefined behavior means the compiler can do anything it wants" true?
(2 answers)
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 5 months ago.
I try to help people learn to code in C++ and I received an answer for an easy task to write a code that gets three numbers as input and outputs a sum of them. I received a wrong-written code with no return statement which... suprisingly works. It prints a good answer to the console, which (as far as know) shouldn't happen without return. I know it is written wrong and I will reply how it should be done but I want to be precise and include an answer why it worked. Here is the code:
#include <iostream>
using namespace std;
int add ()
{
int a,b,c, result;
cin >> a;
cin >> b;
cin >> c;
result=a+b+c;
}
int main()
{
cout << add();
return 0;
}
I would be grateful for an answer.
In some smart compilers it can run the code. but warning will be still there that function is expected to return integer but it is not returning anything.
but in environment like visual studio it will give the error and program will not be build.

Is directly putting in binary possible C++ [duplicate]

This question already has answers here:
Can I use a binary literal in C or C++?
(24 answers)
Closed 9 months ago.
Is putting in binary as a value possible? I want something like char test = 00101011 and it will become 43. I know this is possible by making a function that converts binary to decimal (which can be inputted) but thats not direct and Im pretty sure it takes time.
You need to put the prefix 0b.
#include <iostream>
int main()
{
char c = 0b00101011;
std::cout << static_cast<int>(c) << std::endl;
}

Why sizeof() method is giving different results? [duplicate]

This question already has answers here:
Size of character ('a') in C/C++
(4 answers)
Closed 2 years ago.
When I run the program below in C, I get the output result to be 4.
#include <stdio.h>
//using namespace std;
int main()
{
printf("%d", sizeof('a'));
return 0;
}
But when I run the code below in C++, I get the output result to be 1.
#include <iostream>
using namespace std;
int main()
{
printf("%d", sizeof('a'));
return 0;
}
Could you please explain why do I get different output for the same code as if 'a' is the way we define characters in both the languages ?
In C, a character representation (like 'a') has type int. So, sizeof operator returns the size of an integer.
In C++, it's of a character type.

Please explain how this c++ program is working? [duplicate]

This question already has answers here:
"" + something in C++
(3 answers)
Closed 5 years ago.
I am having trouble , in understanding this program please help:
#include <iostream>
using namespace std;
int main(){
const char* s = 5+"hellow world";
cout<<s;
return 0;
}
It is correct and gives following output
In third line of your code, an anonymous character array is created by the compiler. When you add 5 to the c-string, it performs pointer arithmetic and moves the pointer 5 ahead to the string. Hence, it skips the 5 character from the c-string and only stores other characters from the array into the s.

Operator ++ with stdout giving unexpected result in C/C++ [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
During study in C++ at school, when we learn about operator ++ in C++, we know ++c and c++ are different.
When we test more about this with this kind of code:
#include <iostream>
using namespace std;
int main(){
int c=10;
cout<<++c<<" "<<c++<<endl;
return 0;
}
Why did above code gave output of 12 and 10 in C++?
The computer (we tests with both cout and printf, we also tried VC++ and g++) give this to me:
12 10
Both "cout" and "printf" gave the same result.
But when we test in calculation, the result is all right.
#include <iostream>
using namespace std;
int main(){
int c=10;
int r=++c^c++;
cout<<r<<endl;
return 0;
}
Above source code gave me 0 which means while above XOR operation executing, both left hand side (++c) and right hand side (c++) giving the same value to XOR operator which is 11 (We get the value 11 by replacing c++ with 11 and computer gives the same result 0).
This is really wired. Did anyone noticed this?
By the way, we test in both debug mode and release mode in both Windows and Lubuntu. So we think this is relating to the standard library. But we aren't expecting that we can read the stdlib as a NOOB. So hoping someone can find a reason or solution.
The code int r=++c^c++; is undefined behaviour and should not be used, not ever. You can't modify the variable twice before sequence point.