Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-26-2012, 04:04 AM   PM User | #1
Hk3008
New to the CF scene

 
Join Date: Mar 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hk3008 is an unknown quantity at this point
Putting 2 functions together

Hello I am trying to get some help on putting two codes together in one file then having it display the results consecutively after running each do Ineed to use a header file and refer to the two .cpp files there or put them together I am in a basic programming calls first semester I ahve my code for the two files put together and running but I need to know how to make them one and be able to turn them in in one .txt file
Hk3008 is offline   Reply With Quote
Old 03-26-2012, 07:54 AM   PM User | #2
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
Can you give more information than that? Do you mean you have two main() procedures and you want to merge them into one main(), such that the "first" main() will run and then the "second" main() runs right after?

Generally, you can technically just copy the contents of one main() procedure into the other - putting the code block before or after the other main().

Example:
Code:
int main() {
  ... my code a ...
}

int main() {
 ... my code b...
}
==>

Code:
int main() {
 ... my code a...
 ... my code b...
}
There can be some variable conflicts (such as having variables with the same name being declared in both main() procedures), but I believe all you need to do to fix it would be to remove the "int" prefix on the "code b"'s code block.

If they are not main() procedures, you can create a main and include their c/hpp file and directly call the procedures.

Last edited by Apothem; 03-26-2012 at 07:56 AM..
Apothem is offline   Reply With Quote
Old 03-26-2012, 07:18 PM   PM User | #3
Labrar
New Coder

 
Join Date: Jun 2008
Posts: 61
Thanks: 0
Thanked 12 Times in 12 Posts
Labrar is an unknown quantity at this point
@Apothem

Also you should not initiate two mainfunctions (int main) at the same time.
Sorry for my bad english. Im not native.

@Hk3008

C is a structured language.
So (to the threadstarter) you should use include your code into your main code.
But the best way is to work with external classes which share your libraryheader.

Of course you also could compile several codes in one (for example with include)
but try to keep your code clean. If you need to join different codes, so prog each as a single programm first. When they work fine for themselve, join em.
If you need to join em in a dynamic way, use a wire method (Google is helping you with that)
Its a bit complicated but when you did it once, it comes easyer and easyer by time.
Labrar is offline   Reply With Quote
Old 03-26-2012, 08:39 PM   PM User | #4
Hk3008
New to the CF scene

 
Join Date: Mar 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Hk3008 is an unknown quantity at this point
ok so say I have code a
++++++++++++++++++++++++++++++++++++
Code:
#include <iostream>
#include <cmath>
using namespace std;


int main()
{
	//Enter Variables
	int number;
	cout << "Enter Variable a: ";
	cin >> number;
	int a = number;
	
	
	cout << "Enter Variable b: ";
	cin >> number;
	int b = number;

	cout << "Enter Variable c: ";
	cin >> number;
	int c = number;

	if (a > b && a > c);
        return a;
			cout << "This is your highest number(" << a << ")";
	if (b > a && b > c);
		return b;
			cout << "This is your highest number(" << b << ")";
	if (c > a && c > b);
		return c;
			cout << "This is your highest number(" << c << ")";
	
	return 0;
	}
+++++++++++++++++++++++++++++++++++++++++++++

and code b

+++++++++++++++++++++++++++++++++++++++++++++
Code:
//mortgage Calculator 

#include <iostream> 
#include <cmath> 

using namespace std; 


int main() 
{ 

//declaring variables 
float p = 0; // Loan Amount 
float i = 0; //Interest Rate APR 
int l = 0; //Term 
float j = i/(12 * 100); // Calculate Interest 
int n = l * 12; // Number of Months 
double m = 0; // Monthly Payment 
int choice = 0; 

cout << "Enter the loan amount:" << endl; 
cin >> p; 
cout << "Enter term of loan:" << endl; 
cin >> l; 
cout << "The interest rate is:" <<endl; 
cin >> i; 

//after user input, j and n must be recalculated, otherwise they will stay 0 !!!! 
j = i/(12 * 100); 
n = l * 12; 

//calculating the mortgage using this formula 
m = (p*j)/(1-pow((1+j),-n)); 

//prints out monthly payment 
cout << "The monthly payment is: $" << m << endl; 

return 0; 
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++

if I wanted them to both be in the same .cpp file how would I make it go through each and display results going from one to the other
like in this case it telling me my integer the ntelling me theloan interest rate and waiting for the user to push a key to see the results then idling at the end I know I am not explaining this all properly and I completely agree that once I understand how to do this the first time it becomes easier I jsut cannot find a step by step how to or a even a good example
Hk3008 is offline   Reply With Quote
Old 04-04-2012, 11:34 AM   PM User | #5
acomber
New Coder

 
Join Date: Jul 2011
Location: London, UK
Posts: 13
Thanks: 2
Thanked 1 Time in 1 Post
acomber is an unknown quantity at this point
Functions???

Hi

I assume you are a fairly new programmer and that I have understood your questions correctly. If not apologies.

The easiest way to do what you want is to split up the functionality into functions.

eg

Code:
#include <iostream>

void function1(int val) {
  std::cout << "doing stuff in functions1\n";
}

void function2(double val) {
  std::cout << "doing stuff in functions2\n";
}

int main(int argc, char* argv[]) {

   //first lets call function1
   function1(3);

   //now lets move onto do function2 stuff
   function2(2.7);

   return 0;
}
acomber is offline   Reply With Quote
Reply

Bookmarks

Tags
c++

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:11 AM.


Advertisement
Log in to turn off these ads.