I want to display 265612.2 how ever the output still 265612
I input : 4.2
#include <iostream>
using namespace std;
int main()
{
double a;
double b;
cout<<"Enter the number of light year : ";
cin>>a;
b = a * 63241;
cout<<b;
return 0;
}
You need to "manipulate" the output stream (std::cout):
#include <iomanip> // manipulating helpers
#include <iostream>
int main() {
double a;
double b;
std::cout<<"Enter the number of light year : ";
if(std::cin >> a) {
std::cout << std::setprecision(1) // one decimal
<< std::fixed; // fixed precision
b = a * 63241.01;
std::cout << b;
}
}
Demo
Related
I am trying to use class to create a program, that with user input, can perform the operations of the Pythagorean theorum, but i get this error:
Error (active) E0304 no instance of overloaded function "getline" matches the argument list
This is my code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
class PythagoreanTheorum
{
public:
double a;
double b;
double c;
void seta(double A)
{
a = A;
}
void setb(double B)
{
b = B;
}
void setc(double C)
{
c = C;
}
double calcAreea()
{
return a * pow(a, 2) + b * pow(b, 2) == c * pow(c, 2);
}
};
int main()
{
//Define one right triangles
PythagoreanTheorum righttriangle1;
double a;
double b;
double c;
cout << "Enter the value for a: " << endl;
righttriangle1.a = getline(cin, a);
cout << "Enter the value for b: " << endl;
righttriangle1.b = getline(cin, b);
cout << "Enter the value for c: " << endl;
righttriangle1.c = getline(cin, c);
}
std::getline reads strings, not doubles. So you'd have to read with std::string and then convert it to double (with stod).
You can instead use >> operator for inputs, too:
cout << "Enter the value for a: " << endl;
std::cin >> righttriangle1.a;
cout << "Enter the value for b: " << endl;
std::cin >> righttriangle1.b;
cout << "Enter the value for c: " << endl;
std::cin >> righttriangle1.c;
This is how I would write the code, assuming that calcAreea() in your code is meant to show Pythagoreas Theorem being applied.
My code:
#include <iostream>
#include <cmath>
using namespace std;
class PythagoreanTheorum
{
public:
void seta(double A)
{
a = A;
}
void setb(double B)
{
b = B;
}
void setc(double C)
{
c = C;
}
double calcTheorem()
{
cout<<"Applying Pythagoreas Theorem:"<<pow(a,2)<<"+"<<pow(b,2)<<"="<<pow(c,2);
}
private:
double a;
double b;
double c;
};
int main()
{
//Define one right triangles
//Test -> a = 3, b = 4, c = 5
PythagoreanTheorum righttriangle1;
double a;
double b;
double c;
cout << "Enter the value for a: " << endl;
cin>>a;
righttriangle1.seta(a);
cout << "Enter the value for b: " << endl;
cin>>b;
righttriangle1.setb(b);
cout << "Enter the value for c: " << endl;
cin>>c;
righttriangle1.setc(c);
righttriangle1.calcTheorem();
}
I removed string header file since it wasn't being used, I also used cin instead of getline since that's way better in this case, also I wanted to not use using namespace std; but since it was in your code I retained it & also renamed calcAreea as calcTheorem since it wasn't calculating area
EDIT: I forgot to mention I declared variables in class in private instead of public
Have this task:
#include <iostream>
#include <clocale>
#include <math.h>
using namespace std;
int main()
{
using std::setlocale;
setlocale (LC_ALL,"");
double x, y, z, a, b, c, s;
cout << "Enter x: ";
cin >> x;
cout << "Enter y: ";
cin >> y;
cout << "Enter z:";
cin >> z;
a = sqrt(10*(pow(x,1/3)*(x,y+2)));
b = pow(asin(z),2);
c = fabs(x-y);
s = a*(b-c);
cout << "Result s = " << s << endl;
return 0;
}
Don't know why result is shown is NaN. Could you please help? My code is above.
Sample added:
Here you go:
#include <iostream>
#include <clocale>
#include <cmath>
using namespace std; //to simplify code, you shouldnt be using this
int main()
{
double x = 16.55*pow(10,-3);
double y = -2.75;
double z = 0.15;
double h = 1.0/3.0;
double s = sqrt(10*(pow(x,h)+pow(x,y+2)))*(pow(asin(z),2)-abs(x-y));
cout<<s;
}
Just as Sam mentioned, 1/3 is not what it seems to be. In that state it's calculated as an int, which gives 0 as a result.
I'm new to programming, I have been reading Sams Teach Yourself C++ in 24 hours. I just started learning classes and am confused on how to allow user input on private data. I created the following class that returns the area of a Trapezoid. Any suggestions would be greatly appreciated.
class Trapezoid
{
//assigns a number to a variable
private:
int a = 20;
int b = 25;
int height = 30;
int area;
public:
int getArea();
};
int Trapezoid::getArea()
{
// calculates the area and returns it.
area = (a + b) / 2 + height;
return area;
}
#include "AreaTrapezoid.hpp"
#include <iostream>
int main()
{
// accesses area inside the Trapeziod class.
Trapezoid areaT;
areaT.getArea();
// displays the area result
std::cout << "The area of a Trapezoid: " << areaT.getArea() << std::endl;
std::cout << system("pause");
return 0;
}
You need to read the user's input, and then expose public access to assign new values to the class's private members. For example:
class Trapezoid
{
//assigns a number to a variable
private:
int a = 20;
int b = 25;
int height = 30;
public:
int getArea();
void setA(int value);
void setB(int value);
void setHeight(int value);
};
int Trapezoid::getArea()
{
// calculates the area and returns it.
return (a + b) / 2 + height;
}
void Trapezoid::setA(int value)
{
a = value;
}
void Trapezoid::setB(int value);
{
b = value;
}
void Trapezoid::setHeight(int value)
{
height = value;
}
#include <iostream>
#include <limits>
#include <cstdlib>
#include "AreaTrapezoid.hpp"
int main()
{
Trapezoid areaT;
int value;
// get the user's input and apply it to the Trapeziod.
std::cout << "Enter A: ";
std::cin >> value;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
areaT.setA(value);
std::cout << "Enter B: ";
std::cin >> value;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
areaT.setB(value);
std::cout << "Enter Height: ";
std::cin >> value;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
areaT.setHeight(value);
// displays the area result
std::cout << "The area of the Trapezoid: " << areaT.getArea() << std::endl;
std::system("pause");
return 0;
}
Alternatively, use a constructor instead:
class Trapezoid
{
//assigns a number to a variable
private:
int a;
int b;
int height;
public:
Trapezoid(int a, int b, int height);
int getArea();
};
Trapezoid::Trapezoid(int a, int b, int height)
: a(a), b(b), height(height)
{
}
int Trapezoid::getArea()
{
// calculates the area and returns it.
return (a + b) / 2 + height;
}
#include <iostream>
#include <limits>
#include <cstdlib>
#include "AreaTrapezoid.hpp"
int main()
{
int a, b, h;
// get the user's input and apply it to the Trapeziod.
std::cout << "Enter A: ";
std::cin >> a;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter B: ";
std::cin >> b;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
areaT.setB(value);
std::cout << "Enter Height: ";
std::cin >> h;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// displays the area result
Trapezoid areaT(a, b, h);
std::cout << "The area of the Trapezoid: " << areaT.getArea() << std::endl;
std::system("pause");
return 0;
}
I don't know why my code is giving to me incorrect results.
When I put a number like 6670680902 the result is 6.67068e+0.7 (which is 66706800). That is not the correct result.
When I use the calculator the correct result of 667006080902 / 100 is 66706809.02.
What should I do to fix it?
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace System;
using namespace std;
int main()
{
float a;
float b;
cout << "Ingrese el codigo: "; cin >> a;
b = a / 100;
cout << "result: " << b;
_getch();
return 0;
}
The first issue here is by default C++ will show larger numbers using scientific notation, there are ways to prevent this for floating point numbers like floats. One simple way is to add << fixed before your number:
cout << "result: " << fixed << b;
Which will return 66706812.0.
The next problem is that floats are not good at being precise, which is why the number still isn't correct. Floats are less precise compared to something like a double which has twice the precision. If you use a double instead for a and b:
int main()
{
double a;
double b;
//...
cout << "result: " << fixed << b;
//...
}
you will get the value you expect: 66706809.02
Can do by using 'limits`
#include "iostream"
#include <string>
#include <limits>
using namespace System;
using namespace std;
int main()
{
double a;
double b;
cout << "Ingrese el codigo: "; cin >> a;
b = a / 100;
cout.precision(numeric_limits<double>::digits10 + 1);
cout << "result: " << b << endl;
_getch();
return 0;
}
Output:
result: 66706809.02
I have this code:
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
double f(double x);
double biseccion(double a, double b, double tolerancia, int maxiter);
int main()
{
double a, b, raiz;
double tolerancia=0.00000;
int maxiter=25;
cout << "Input begin of interval: ";
cin >> a;
cout << "Input end of interval: ";
cin >> b;
cout << "\n";
cout << " # de"<<"\n"<<"Iteration"<<"\t"<<" A"<<"\t"<<" B"<<"\t"<<" C"<<"\t"<<" f(c)"<<endl;
raiz=biseccion(a,b,tolerancia,maxiter);
cout << "\n";
cout << "The root is: "<< raiz <<endl;
return 0;
}
double f(double x)
{
return x*x*x-x-2;
}
double biseccion(double a, double b, double tolerancia, int maxiter)
{
double c;
int numiter=1;
do
{
c=(a+b)/2;
if(f(a)*f(c)<0)
{
b=c;
}
else
{
a=c;
}
cout<<" "<<numiter<<"\t"<<"\t"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<f(c)<<endl;
numiter++;
}
while((abs(f(c))>tolerancia)&&(numiter<maxiter));
return c;
}
Instead of writing "x*x*x-x-2" in my code, I want user to input it before asking for begin of interval. How can I do that?
I try using a variable to store the "x*x*x-x-2", but none work.
You need to parse the input, its probably not as easy as you are thinking but there are some libraries that can help you.
muparser.sourceforge.net/
code.google.com/p/expressionparser/
partow.net/programming/exprtk/index.html
here is also a solution in c# that might help you too.
Is there a string math evaluator in .NET?