program does not return the value entered

#include <iostream>

using namespace std;

class testClass
{
public:
    int sum();
    void print() const;
    testClass();
    testClass(int a, int b);
private:
    int x;
    int y;
};

int testClass::sum()
{
    int c;
    c = x + y;
    return c;
}

void testClass::print() const
{
    cout << "x = " << x << "y = " << y << endl;
}

testClass::testClass()
{
    x = 0;
    y = 0;
}

testClass::testClass(int a, int b)
{
    x = a;
    y = b;
}

int main()
{
    int a;
    int b;

testClass mytestClass;

cout << "enter a value for x: ";
cin >> a;
cout << "enter a value for y: ";
cin >> b;

mytestClass.print();

    return 0;
};

Recommended Answers

All 4 Replies

You should use CODE tags so that it is indented correctly and things like ::print isn't replaced with a smiley face and "rint".

Also, you are inputting into the automatic variables a and b in main. These are completely unrelated to the ones in your class.

so how do i input into the variables specified in the class

If it's printing zero for both values, it's because you called the empty constructor, not the one where you pass the values.Instead of:

int main()
{
int a;
int b;
 
testClass mytestClass;
 
cout << "enter a value for x: ";
cin >> a;
cout << "enter a value for y: ";
cin >> b;
 
mytestClass.print();

Do:

int main()
{
int a;
int b;
 
 
cout << "enter a value for x: ";
cin >> a;
cout << "enter a value for y: ";
cin >> b;
testClass mytestClass(a,b);
 
mytestClass.print();

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.