Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing same inputs for each function in c++
    primarykey
    data
    text
    <p>Okay, so I have little program I made, which asks user for inputs, and then adds, subtracts and etc. But somehow, it doesn't work right. It must use x and y inputs for each function, but it seems like it uses outputs from previous instead.</p> <p>Here's a code:</p> <p>Main:</p> <pre><code>// ======================================================================== // NumberProject.cpp : Defines the entry point for the console application. // ======================================================================== // ================== #include "stdafx.h" #include &lt;iostream&gt; // ================== // ================ // Class Inclusions // ================== #include "Number.h" // ================== // ==================== using namespace std; // ==================== void Banner(); // ============= int main( ) { // ================================ // Variable and Object Declarations // ============ char answer; bool goAgain = true; int integerValue; Number x; Number y; Banner(); // ========= // User loop // =================== while ( goAgain ) { cout &lt;&lt; "Enter an integer: "; cin &gt;&gt; integerValue; x.Set( integerValue ); cout &lt;&lt; "Enter an integer: "; cin &gt;&gt; integerValue; y.Set( integerValue ); cout &lt;&lt; endl; // Addition x.Add( y ); cout &lt;&lt; "Addition: "; x.Output( ); // Subtraction x.Subtract( y ); cout &lt;&lt; "Subtraction: "; x.Output( ); // Multiplication x.Multiply( y ); cout &lt;&lt; "Multiplication: "; x.Output( ); // Division x.Divide( y ); cout &lt;&lt; "Division: "; x.Output( ); // =========================== // User loop termination code. // ==================================================== cout &lt;&lt; "Would you like go again continue? (y/n)" &lt;&lt; endl; cin &gt;&gt; answer; if ( answer == 'n' ) goAgain = false; // ==================== } // while // ========== return 0; } // Function main( ) // ===================== void Banner() { cout &lt;&lt; "====================================" &lt;&lt; endl; cout &lt;&lt; " Welcome to the Number Project " &lt;&lt; endl; cout &lt;&lt; "====================================" &lt;&lt; endl; cout &lt;&lt; endl; } // ================================================================ </code></pre> <p>Number.cpp:</p> <pre><code>// ================== #include "StdAfx.h" #include "math.h" // Needed for sqrt and pow. #include &lt;string&gt; #include &lt;iostream&gt; // ================== // ================ // Class Inclusions // ================== #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(); } void Number::Multiply( Number otherNumber ) { number = number * otherNumber.Get(); } void Number::Divide( Number otherNumber ) { number = number / otherNumber.Get(); } // ================================================= </code></pre> <p>Number.h:</p> <pre><code>// =========== #pragma once // =========== // ============== class Number { public: // ============ // Constructors // =============== Number( void ); Number( int ); // ============== // ========== // Destructor // =============== ~Number( void ); int Number::Get( ); // Accessor void Number::Set( int ); // Mutator void Number::Output(); void Number::Add( Number ); void Number::Subtract( Number ); void Number::Multiply( Number ); void Number::Divide( Number ); // =========================== private: int number; }; </code></pre> <p>Example output:</p> <pre><code>==================================== Welcome to the Number Project =================================== Enter an integer: 2 Enter an integer: 3 Addition: 5 Subtraction: 2 Multiplication: 6 Division: 2 Would you like go again continue? (y/n) </code></pre> <p>Please help?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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