0
Write a c++ program. Make sure your program handle all validations Use at Least One class. C= 1/b + 2 / (a+1)
#include<cstdlib> #include<iostream> #include<math.h> using namespace std; class Coding{ private: double a, b, c; public: void display(); Coding(){ cout<<"Enter the value for a: "; cin>>a; cout<<"Enter the value for b: "; cin>>b; if(a!=-1) { cout<<"a cannot be -1" ; } if (b!=0) { c=(1 / b) + 2 / (a + 1); cout<<"c = "; cout << c << endl; } else{ cout << "b cannot be zero: "; } } }; int main(){ Coding myCoding; myCoding.display( value a, value b); } Please I Need A Helping Hand 👆 I'm Getting Errors At The Last Line
1 ответ
0
You have some errors in that code, probably you have not yet understood functions well. Also taking user input is also a concern since you are not putting it in the right place.
I'm not a C++ programmer but this should work and you can take the input in the main() and pass to the constructor call but I have kept it simple.
#include<cstdlib>
#include<iostream>
#include<math.h>
using namespace std;
class Coding{
private:
double a, b, c;
public:
Coding(double a, double b){
this->a = a;
this->b = b;
}
void display(){
if(a==-1)
{
cout<<"a cannot be -1" ;
}
if (b!=0)
{
c=(1 / b) + 2 / (a + 1);
cout<<"c = ";
cout << c << endl;
}
else{
cout << "b cannot be zero: ";
}
}
};
int main(){
Coding myCoding(5,6);
myCoding.display();
}