What's new

C & C++ Pahelp naman po padebug

Aircon

Forum Guru
Elite
Joined
Jun 1, 2018
Posts
1,296
Reaction
6,066
Points
1,130
pa help po na mapalabas yung correct output and mafix yung code :

fix this code you should print the correct output using this code :

include<iostream.h>
class maine
{
int x;
float y;
protected;
long x1;
public:
maine()
{ };
maine(int s, t=2.5)
{ x=s;
y=t;
}
getdata()
{
cin>>x>>y>>x1;
}
void displaydata()
{
cout<<x<<y<<x1;
} };
void main()
{ clrscr();
maine m1(20,2.4);
maine m2(1);
class maine m3;
}
 
To fix the code and achieve the correct output using cout, you need to make several changes. Here's the corrected code:

C++:
#include<iostream> // include iostream instead of iostream.h

using namespace std; // add a namespace to simplify code

class maine
{
private: // make member variables private
    int x;
    float y;
    long x1;
    
public:
    maine() {}; // remove unnecessary constructor
    
    maine(int s, float t = 2.5) // add missing data type for t
    {
        x = s;
        y = t;
    }
    
    void getdata() // add return type for functions
    {
        cin >> x >> y >> x1;
    }
    
    void displaydata()
    {
        cout << x << " " << y << " " << x1; // add space between variables for better readability
    }
};

int main() // change void main() to int main()
{
    maine m1(20, 2.4);
    maine m2(1);
    maine m3;
    
    m1.getdata(); // call getdata() function to input data
    m1.displaydata(); // call displaydata() function to output data
    
    return 0; // add return statement
}

Explanation:

1. Replace #include<iostream.h> with #include<iostream>. The .h header files are deprecated in modern C++.
2. Add using namespace std; to avoid having to write std:: before every standard library identifier.
3. Make member variables private by changing protected; to private:.
4. Add missing data type for t in the parameter list of maine(int s, float t = 2.5).
5. Add return type void for functions getdata() and displaydata().
6. Add a space between variables in the cout statement of displaydata() for better readability.
7. Change void main() to int main() and add a return statement return 0; at the end of the main() function.

After these changes, the code will correctly input data from the user and display it using the cout statement in the displaydata() function.
 

Similar threads

Back
Top