PDA

View Full Version : Help with functions


sumlani
02-18-2008, 12:22 AM
Here is my code

#include <iostream>
using namespace std;
void Num_Stars(int);
int main()
{
int i = 1;
cout << endl;
for (i = 0; i <= 7; ++i)
{
Num_Stars(i);
cout << endl;
}
cout << endl;
return 0;
}
void Num_Stars(int count)
{
cout << endl;
for(i = 0; i <= count; ++j)
cout << '*';
return;


I'm trying to get Num_Star to be called from main() using values 1-7 as parameters. The values should print in astericks. I'm a beginner at this, so I really need any and all assistance to get this program to run.

yawningdog
02-18-2008, 02:02 AM
There are three problems with your function definition. You haven't declared your increment variable in the function scope, you're incrementing a different variable, and you haven't closed your brackets. The function should look like this...void Num_Stars(int count)
{
cout << endl;
for(int i = 0; i < count; ++i)
cout << '*';
return;
}
You probably don't want to use "i" as the argument for your function. If memory serves, it's in the same scope as the loop argument and gets rewritten as soon as the loop begins execution. Here is the program as I suspect you want it to work.#include <iostream>
using namespace std;
void Num_Stars(int);
int main()
{
int stars = 1;
cout << endl;
Num_Stars(stars);
cout << endl;
return 0;
}
void Num_Stars(int count)
{
cout << endl;
for(int i = 0; i < count; ++i)
cout << '*';
return;
}

sumlani
02-18-2008, 11:47 AM
Thanks, it did run! I've been working on this for a while. I did realize that I had ++j instead of ++i. Also, how would I get it to do multiple lines? I tried to set stars <= 7, needless to say it didn't work :)

yawningdog
02-18-2008, 05:45 PM
If you want to print seven rows of seven stars each, then placing the function call in a loop will do.

Remember, if you want seven stars then you need to either use < instead of <= or start with 1, or else you will end up with eight.

sumlani
02-23-2008, 06:14 PM
Thanks for all of your help. I'm just taking programming for the first time & I made the mistake of taking it online! So at times it is very hard when you've tried everything that you can think of.

yawningdog
02-24-2008, 01:02 PM
I'll br wrapping up my BS in computer science (networking) this year and I've already finished all of my programming requirements. By the time I was actually enrolled in these classes I already knew enough about programming to teach them. The way to learn programming is the way you are learning programming. Write code, compile it, watch it work or fail and move on to the next problem.