Note that there are some explanatory texts on larger screens.

plurals
  1. PONumber addition and subtraction in c++ classes
    text
    copied!<p>Ok, so I'm trying to get my program work, and it seems like something is wrong, but I don't know why. Could you guys please look at it and tell me whats wrong?</p> <p>The problem is: Subtracting won't work properly :c</p> <p>Here's code for Main function (NumberProject.cpp):</p> <pre><code>// NumberProject.cpp : Defines the entry point for the console application. #include "stdafx.h" #include &lt;iostream&gt; #include "Number.h" using namespace std; int main( ) { // Variable and Object Declarations char answer; bool goAgain = true; int integerValue; Number numberA; // Invoke the default constructor. Number numberB; // Invoke the default constructor. // User loop while ( goAgain ) { cout &lt;&lt; "Enter an integer: "; cin &gt;&gt; integerValue; numberA.Set( integerValue ); cout &lt;&lt; "Enter an integer: "; cin &gt;&gt; integerValue; numberB.Set( integerValue ); cout &lt;&lt; endl; // Addition numberA.Add( numberB ); cout &lt;&lt; "Addition: "; numberA.Output( ); // Subtraction numberA.Subtract( numberB ); cout &lt;&lt; "Subtraction: "; numberB.Output( ); // User loop termination code. cout &lt;&lt; "Would you like to continue? (y/n)" &lt;&lt; endl; cin &gt;&gt; answer; if ( answer == 'n' ) goAgain = false; } // while return 0; } // Function main( ) </code></pre> <p>Number.cpp (class):</p> <pre><code>#include "StdAfx.h" #include "math.h" // Needed for sqrt and pow. #include &lt;string&gt; #include &lt;iostream&gt; #include "Number.h" using namespace std; Number::Number( void ) { number = 0; } Number::Number( int integerValue ) { number = integerValue; } Number::~Number( void ) { } int Number::Get( ) { return number; } void Number::Set( int integerValue ) { number = integerValue; } void Number::Output( ) { cout &lt;&lt; number &lt;&lt; endl; cout &lt;&lt; endl; } void Number::Add( Number otherNumber ) { number = number + otherNumber.Get(); } void Number::Subtract( Number otherNumber ) { number = number - otherNumber.Get(); } </code></pre> <p>Number.h:</p> <pre><code>#pragma once class Number { public: Number( void ); Number( int ); ~Number( void ); int Number::Get( ); // Accessor void Number::Set( int ); // Mutator void Number::Output(); void Number::Add( Number ); void Number::Subtract( Number ); private: int number; }; // Class Number </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload