### How to 1 more formula in C++?

How to 1 more formula in the program? Inside the program got 1 formula, so I want add 1 more formula. If add 1 more, it be 2 formula in the program. I want to run the program but run 1 formula only.

Yes, below want is run 1 formula. If add 1 more?

functions.h
 ``12345678910111213`` ``````#ifndef FUNCTIONS_H #define FUNCTIONS_H class finance_state { double current_asset , current_liabilities; public: finance_state(double ca , double cl); ~finance_state(); double CurrentRatio(); }; #endif ``````

functions.cpp
 ``1234567891011121314151617`` ``````#include "functions.h" finance_state :: finance_state(double ca , double cl) : current_asset(ca) , current_liabilities(cl) { } finance_state :: ~finance_state() { } double finance_state :: CurrentRatio() { double temp = this->current_asset / this->current_liabilities; return temp; }``````

EntryPoint.cpp
 ``123456789`` ``````#include #include "functions.h" int main () { finance_state MyState( 500.0 , 300.0 ); std::cout<< "my current ration is :\t"<< MyState.CurrentRatio()<
Which other formula do you want to include? You can might as well add another function like your CurrentRatio.
For this formula,

Gross margin = (Revenue – Cost of goods sold) / Revenue
Well you can include COGS and Revenue in your member variables and initialize them while constructing the object ( like your current_asset and current_liabilities ).

Then have another member function called GetGrossMargin as follows:
 ``123456`` ``````double finance_state::GetGrossMargin() { if ( this->revenue > 0 ) return (1 - this->cogs / this->revenue); return 0; }``````

Note: I would suggest you include the > 0 condition for your liabilities also, before trying to calculate the current ratio.
Went I include the function, got error... The second function need include in the functions.cpp part rite??
This is I did to run the program but got error. Can you explain me, why?

functions.h

 ``12345678910111213`` ``````#ifndef FUNCTIONS_H #define FUNCTIONS_H class finance_state { double current_asset , current_liabilities; public: finance_state(double ca , double cl); ~finance_state(); double CurrentRatio(); }; #endif ``````

functions.cpp

 ``123456789101112131415161718192021222324`` ``````#include "functions.h" finance_state :: finance_state(double ca , double cl) : current_asset(ca) , current_liabilities(cl) { } finance_state :: ~finance_state() { } double finance_state :: CurrentRatio() { double temp = this->current_asset / this->current_liabilities; return temp; } double finance_state::GetGrossMargin() { if ( this->revenue > 0 ) return (1 - this->cogs / this->revenue); return 0; }``````

EntryPoint.cpp

 ``123456789`` ``````#include #include "functions.h" int main () { finance_state MyState( 500.0 , 300.0 ); std::cout<< "my current ration is :\t"<< MyState.CurrentRatio()<
When you get an error, please post the exact text of the error.

functions.cpp
line 19 - GetGrossMargin() is not in your class declaration.
Lines 20, 21 - revenue and cogs are not members of your class.

||=== EntryPoint, Debug ===|
EntryPoint\main.cpp|line 2|functions.h: No such file or directory|

EntryPoint\main.cpp||In function `int main()':|

EntryPoint\main.cpp|line 6|error: `finance_state' was not declared in this scope|

EntryPoint\main.cpp|line 6|error: expected `;' before "MyState"|

EntryPoint\main.cpp|line 7|error: `MyState' was not declared in this scope|

EntryPoint\main.cpp|line 6|warning: unused variable 'finance_state'|

EntryPoint\main.cpp|line 7|warning: unused variable 'MyState'|

||=== Build finished: 4 errors, 2 warnings ===|
Try to make the following changes:

functions.h
 ``1234567891011121314`` ``````#ifndef FUNCTIONS_H #define FUNCTIONS_H class finance_state { double current_asset , current_liabilities, cogs, revenue; public: finance_state(double ca , double cl, double _cogs, double _revenue ); ~finance_state(); double CurrentRatio(); double GetGrossMargin(); }; #endif ``````

functions.cpp
 ``123456789101112131415161718192021222324`` ``````#include "functions.h" finance_state :: finance_state(double ca , double cl, double _cogs, double _revenue) : current_asset(ca) , current_liabilities(cl), cogs( _cogs ), revenue( _revenue ) { } finance_state :: ~finance_state() { } double finance_state :: CurrentRatio() { double temp = this->current_asset / this->current_liabilities; return temp; } double finance_state::GetGrossMargin() { if ( this->revenue > 0 ) return (1 - this->cogs / this->revenue); return 0; }``````

EntryPoint.cpp
 ``12345678910`` ``````#include #include "functions.h" int main () { finance_state MyState( 500.0 , 300.0, 200.0, 100.0 ); std::cout<< "my current ration is :\t"<< MyState.CurrentRatio()<
It's working. Thank you so much. Really appreciate your help. Thanks again... :)
Hello Abhishekm,

A small doubt, if I want to use IF...ELSE statement. How to use inside the program?
Last edited on
I would recommend you go through some basic tutorials for the C++ language. The one in this website is a nice tutorial for beginners.

The syntax for 'if else' statement is as follows:
 ``12345`` ``````if ( condition == true ) { //do something } else { // do something else }``````
OK... Thank you :)
Topic archived. No new replies allowed.