setprecision is not working the way I'm expecting (c++) - c++

I keep receiving "10", when it should be "10.5" with the setprecision(1). I don't understand why it's happening and wish for some help rn! Thanks! I also linked the error message...
#include <iostream>
#include <iomanip>
using namespace std;
int avg(int a, int b)
{
int x = (a+b) / 2.0;
return x;
}
int main()
{
cout << fixed << setprecision(1) << avg(8, 13) << endl;
return 0;
}
spot_the_error_b.cpp: In function 'int main()':
spot_the_error_b.cpp:14:13: error: 'setPrecision' was not declared in this scope
14 | cout << setPrecision(1) << avg(8, 13) << endl;
|

You are returning an int in the avg function, you should return a double so that setprecision(1) can work correctly.
double avg(int a, int b)
{
return (a+b) / 2.0;
}

Related

GCD recursion function RTE in some IDEs

Trying to implement gcd int function using Euclidean Algorithm with recursion. Results from CodeBlocks differ from IDEone (which I use to test my code before submitting to a CP website, TLX: https://tlx.toki.id, which I assume has similar compilers etc. because a lot of times IDEone and TLX got RTE while in CodeBlocks it ran without any problem). First Question: 1. Do they actually have something different that affects the output?
My first attempt was as follows:
#include <iostream>
#include <cmath>
using namespace std;
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#define pass (void)0
#include <cstdio>
#include <cstring>
#define ll long long
int gcd(int x, int y){
if(y!=0){
gcd(y, x%y);
//return x%y;
} else {
return x;
}
}
int main() {
cout << "test" << endl;
int z = gcd(100, 10);
cout << z << " bruh" << endl;
cout << "hello" << endl;
}
which IDEone spits out
Runtime error #stdin #stdout 0.01s 5380KB
test
while it ran as expected (z = 1 and prints out the correct stuff) in CodeBlocks
I tried to pinpoint where the error exactly occurs by 1. printing out at what part of the code my computer went error by the following way
void gcd(int x, int y){
if(y!=0){
cout << "if " << x << ", " << y << endl;
gcd(y, x%y);
} else {
cout << "else " << x << ", " << y << endl;
//return x;
}
}
int main() {
cout << "test" << endl;
//int z = gcd(100, 10);
gcd(100, 10);
//cout << z << " bruh" << endl;
cout << "hello" << endl;
}
which in both IDE, it outputted:
test
if 100, 10
else 10, 0
hello
then I also tried:
int gcd(int x, int y){
if(y!=0){
gcd(y, x%y);
//return x%y;
} else {
return x;
}
}
int main() {
cout << "test" << endl;
int z = gcd(100, 10);
cout << z << " bruh" << endl;
cout << "hello" << endl;
}
CodeBlocks outputted the first, while IDEone had an error as in the second
test
10 bruh
hello
Runtime error #stdin #stdout 0.01s 5380KB
test
from what I've tried and understand so far, it seems there's an error when the function gcd() calls the gcd() function. 2. Is my assumption correct? 3. and how am I supposed to solve this problem?
Thanks in advance
The problem is that the recursive case of the gcd() function does not run a return statement. Thus, it should be modified like this:
int gcd(int x, int y){
if(y!=0){
return gcd(y, x%y);
} else {
return x;
}
}
This could easily have been caught by enabling and reading compiler warnings.

CPP program displays shadows the perimeter error [duplicate]

I am trying to make a function that returns double the integer number that I will pass to it. I am getting the following error message with my code:
declaration of 'int x' shadows a parameter int x; "
Here is my code:
#include <iostream>
int doublenumber();
using namespace std;
int doublenumber(int x)// <-- this is the function which returns double the value .
{
int x;
return 2 * x;
cout << endl;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin >> a;
doublenumber(a);
return 0;
}
You have x as a parameter and then try to declare it also as a local variable, which is what the complaint about "shadowing" refers to.
I did it because your advice was so helpful, and this is the final result :
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int n= doublenumber(a);
cout << "the double value is : " << n << endl;
return 0;
}
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int d = doublenumber(a);
cout << "Double : " << d << endl;
return 0;
}
There are some problem with your code. Your declaration and definition of function dies not match. So remove declaration as no necessity of it.
You are declaring local x variable inside function which will shadow your function arguments.

not declared in scope error in c++ when overloading

I am getting an error when trying to run this code
In function 'int main()':
error: 'area' was not declared in this scope
I cannot find a clear solution to the problem.
#include <iostream>
using namespace std;
int main() {
area(13.3, 67.4);
return 0;
}
void area(int a, int b){
cout << "The area is " << a * b << endl;
}
void area(float a, float b){
cout << "The area is " << a * b << endl;
}
void area(double a, double b){
cout << "The area is " << a * b << endl;
}
You need to declare the functions before you can use them.
Either forward declare them:
#include <iostream>
using namespace std;
// forward declarations
void area(int a, int b);
void area(float a, float b);
void area(double a, double b);
int main() {
area(13.3, 67.4);
return 0;
}
void area(int a, int b){
cout << "The area is " << a * b << endl;
}
void area(float a, float b){
cout << "The area is " << a * b << endl;
}
void area(double a, double b){
cout << "The area is " << a * b << endl;
}
Otherwise, move the implementations above main():
#include <iostream>
using namespace std;
void area(int a, int b){
cout << "The area is " << a * b << endl;
}
void area(float a, float b){
cout << "The area is " << a * b << endl;
}
void area(double a, double b){
cout << "The area is " << a * b << endl;
}
int main() {
area(13.3, 67.4);
return 0;
}
That being said, since the implementations are exactly the same, just with different data types, consider using a single templated function instead:
#include <iostream>
using namespace std;
template<typename T>
void area(T a, T b){
cout << "The area is " << a * b << endl;
}
int main() {
area<double>(13.3, 67.4);
return 0;
}
Put functions prototypes above your main function and you should be fine.

without declaration the function first, I can swap the value of the variables?

#include <iostream>
using namespace std;
void swap(int, int);
int main()
{
int a=10;
int b=20;
swap (a, b);
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
void swap(int x, int y)
{
int t;
t = x;
x = y;
y = t;
}
those code above can't swap the value of a and b.
but my question is , when I forgot to type the third line "void swap(int, int);
" , the values of a and b swaped !! why?
It's because you have
using namespace std;
At the beginning of your source code.
This is a a bad programming practice, whose consequences you just experienced, first hand. You told the compiler that you want to invoke std::swap, without having any clue that you actually did that.
It's ironical, because you version of swap() won't work right, but std::swap does; so you were operating under the mistaken impression that your code was working, when it didn't.
Never use "using namespace std;" with your code. Simply forget that this part of the C++ language ever existed.
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
system("pause");
swap(a, b);
cout << "a: " << a << endl;
cout << "b: " << b << endl;
system("pause");
return 0;
}
void swap is unnecessary
If you put the function definition above main then you don't need a prototype otherwise you do need it and the compiler should give you an error if you don't have a prototype

error: cannot convert ‘float (*)(int)’ to ‘float’

My program converts temperature from the Fahrenheit scale to the Celcius scale and finally to absolute value scale.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int farh;
float cels(int a)
{
float c;
const int m0 = 32;
const float m1 = 0.5555;
c=(a-m0)/m1;
return c;
}
float ab(float a)
{
const float m2 = 273.15;
float d;
d=a-m2;
return d;
}
int main() {
const int WIDTH = 16;
cout << setiosflags ( ios :: left );
cout << setw(WIDTH) << "Fahrenheit" << setw(WIDTH) << "Celcius" << setw(WIDTH) << "Absolute Value" << '\n';
cout.setf(ios::fixed);
cout.precision(2);
for (farh = 0 ; farh <= 300 ; farh = farh + 20) {
cout.width(16);
cout << farh << cels(farh) << ab(cels) << "\n";
}
return 0;
}
The Compile time error message I receive is:
d26.cc: In function ‘int main()’:
d26.cc:38:40: error: cannot convert ‘float (*)(int)’ to ‘float’ for argument ‘1’ to ‘float ab(float)’
cout << farh << cels(farh) << ab(cels) << "\n";
Why am I receiving this error?
ab takes a float and returns a float:
float ab(float a)
But cels isn't a float, it's a function:
float cels(int a)
You probably meant
ab(cels(farh))
Or to take a temporary:
float cur_cels = cels(farh);
cout << farh << cur_cels << ab(cur_cels) << "\n";
Side-note, ab should probably be named kelvin.
Actually you have passed a funtion pointer to ab. if your intent is to pass the function ( clearly not! ) you can use the following syntax :
float ab(float(*callback)(int),int pass) {
callback(pass); /* It calls the function indirectly with <pass> */
}
Its great for menu creation for example if you have two options:
1. Fahrenheit to Cell
2. Fahrenheit to Kelvin it will be useful
You can search callback functions in C with google.