10 Replies - 791 Views - Last Post: 14 October 2011 - 07:35 AM Rate Topic: -----

#1 Mu Nova   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 15-September 11

Using a function to modify a program

Posted 13 October 2011 - 03:13 PM

Hi guys, again. Been about a month and I haven't posted since, heh... Sorry about that, I'm absorbed in game forum, hard to tear myself away from that except when I need some help.

So, yeah, thread title. I have a program that works, and I should be able to use another function (with function prototype, and definition, and all that), but I just feel lost. I haven't gone over functions much with myself, so now I look at this bit where it says "use a function to modify this other one", like before, I'm just lost. lol

Much appreciated in advance, for any help. (so here's the program, and stuff)

#include <iostream>

using std::cout;
using std::endl;
using std::ios;
	
#include <iomanip>

using std::setw;
using std::setprecision;
using std::setiosflags;

#include <stdlib>

int main()
{
double amount,
principal = 1000.0,
rate = .05;

cout << "Year" << setw (21)
<< "Amount on deposit" << endl;


cout << setiosflags( ios::fixed | ios::showpoint) << setprecision(2);

for ( inte year = 1; year <= 10; year++){
amount = principal * pow( 1.0 + rate, year);
cout << setw(4) << year << setw(21) << amount << endl;
}
return 0;
}


Ah, and I can't edit. Hardly need to doublepost for it, but of course there's a space for return 0. >_<;

MOD EDIT: Added code tags and missing space. When posting code...USE CODE TAGS!!!

:code:

Is This A Good Question/Topic? 0
  • +

Replies To: Using a function to modify a program

#2 Mu Nova   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 15-September 11

Re: Using a function to modify a program

Posted 13 October 2011 - 03:27 PM

Well, just like before, I've at least figured part out. Removing the rate and putting it next to double amount, then... Now I have to change function main to be named something else, and make a new main.

Er... Sigh, I derp. To be specific, I should end up with a function that involves inputting the rate and having that calculated and printed, blahblah.
I have an idea of what I'd have to make of the new function main, but I have no idea what to name the current one to make it work. ._.
Was This Post Helpful? 0
  • +
  • -

#3 Mu Nova   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 15-September 11

Re: Using a function to modify a program

Posted 13 October 2011 - 03:45 PM

Or instead or additionally, the same kind thing with this program? Please? lol
This one is obviously more simple and may be better for getting the point across to me. I think.

	#include <iostream>

using std::cout;
using std::endl;

int main()
{
	int factorial, j;

	cout << "X\tFactorial of X\n";

	for ( int i = 1; i <= 5; ++i ) {
	   factorial = 1;

		for ( int j = 1; j <= i; ++j) { 
		   factorial *= j;
}


		cout << i << '\t' << factorial << '\n';
}

	cout << endl;
	return 0;
}


MOD EDIT: Added code tags and missing space. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 13 October 2011 - 05:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 black_yurizan   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 18-June 11

Re: Using a function to modify a program

Posted 13 October 2011 - 04:11 PM

I'm not so sure what you are asking. Are you asking us how to make the factorial for loop into a function? If so, this is what I would do


#include<iostream>

using namespace std;

void factorial(int factorial,int j);// prototype function

int main()
{
  int factorial,j;

factorial(factorial,j);

return 0;
}

// Rest of the function
void factorial(int factorial, int j)
{for ( int i = 1; i <= 5; ++i ) {
factorial = 1;

for ( int j = 1; j <= i; ++j) {
factorial *= j;
}
cout << i << '\t' << factorial << '\n';
}



I would recommend using the "using namespace std", it will be much easier for you to that than to do the using std:: cout and endl. I hope this helps

This post has been edited by black_yurizan: 13 October 2011 - 04:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 Mu Nova   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 15-September 11

Re: Using a function to modify a program

Posted 13 October 2011 - 04:59 PM

Ah, the void thing isn't something I've used... Gah, lol. That does still help though, I appreciate it.

And, using namespace? I get what kind of thing you're talking about, but what does it do? Unless you mean it fills in for cin and cout?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Using a function to modify a program

Posted 13 October 2011 - 05:02 PM

When you're posting code, PLEASE USE CODE TAGS!!!

:code:

If that's too complicated, then read the first link in my signature.

Also, if you're looking for help with functions, chcek the tutorials in my signature as well.
Was This Post Helpful? 0
  • +
  • -

#7 Mu Nova   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 15-September 11

Re: Using a function to modify a program

Posted 13 October 2011 - 05:09 PM

Ah, right... I've used the code tags before but I forgot this time. lol, sorry.
(and thanks for those links, too. I think I'll check em out)

Ah, but additionally, if I could also get help with this: Supposed to get a program to calculate
sin(x)=1 - x^(3)/3! + x^(5)/5! - x^(7)/7!... and so on. And in such a way that the user inputs the x. Then I'm supposed to figure how many terms needed with sin(0.1), until 0.0998334166, etc., but I THINK I can do that, I've done something like it before.
There's another similar thing involving pi in this book, but I still don't get it. Gahhhh, confound you, C++. lol (or my difficulty with it)
Was This Post Helpful? 0
  • +
  • -

#8 Mu Nova   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 15-September 11

Re: Using a function to modify a program

Posted 13 October 2011 - 05:42 PM

Okay, now a really serious problem. My compiler or w/e (or the CMD run thing, whichever or whatever I should refer to)... It works fine on other computers, but when I'm at home (except for one old program I just ran), it says... well... I'll post a link to a picture.
http://img256.images...treamerror2.jpg
I just don't get why this happens. Sucks bigtime. >_<
Was This Post Helpful? 0
  • +
  • -

#9 black_yurizan   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 18-June 11

Re: Using a function to modify a program

Posted 13 October 2011 - 06:06 PM

Well I know one of the errors is saying that you are missing a bracket in the factorial function. If you copied the function I used, then I'm sorry to tell you that I forgot to put another bracket at the end of the function. Also, I've just realized that you already initialized "j" in the second "for loop" so you don't need to put it in the functions parameters. Now for the rest of the errors, I believe the problem is that you're using "using std:: cout " and "using std::endl" instead of " using namespace std;". If you use the one I recommended I'm pretty sure you won't get this error message.
Was This Post Helpful? 0
  • +
  • -

#10 Mu Nova   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 15-September 11

Re: Using a function to modify a program

Posted 13 October 2011 - 06:19 PM

I can run a program somewhere else, have it work fine, and I bring it home and it does this. And it still gives me the same error when I change the stuff to namespace. >_>
Also, it gets another error when I add that other }, and... >_>
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg   User is online

  • member icon

Reputation: 5812
  • View blog
  • Posts: 17,743
  • Joined: 25-December 09

Re: Using a function to modify a program

Posted 14 October 2011 - 07:35 AM

I believe that the Borland C++ version you are using may not be a standard compliant compiler. You should get a more modern compiler that uses namespaces and uses the standard include files (iostream) instead of the pre-standard include files (iostream.h).

There are several good modern compilers available free to download to consider, Code::Blocks, wx-DevC++, and Microsoft Visual C++ to name a few.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1