0

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;
};

Edited by pyTony: fixed formatting

3
Contributors
4
Replies
5
Views
12 Years
Discussion Span
Last Post by madt
0

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.

Edited by happygeek: fixed formatting

0

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

0

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();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.