Re: Same patter, new problem
This is your function declaration:
Code:
int print_line (void)
and this is how you're calling it:
Code:
print_line(i, 11-2*i)
"(i, 11-2*i)" is not the same as "(void)".
Viggy
Re: Same patter, new problem
I'm still getting the error "too many arguments for function print_line", it seems to me that there should be 2 functions, one for printing the asterisks and one for printing the spaces.
Re: Same patter, new problem
Okay, so I've finally got it printing but its printing an infinite number of asterisk. I know it needs an \n in the printf statement that prints the last set of asterisk but when I added another loop i got a forbidden nested loop error. Here's how it looks now:
int print_line() {
int i;
i = 1;
while (i < 6) {
printf("*", i);
}
i = 5;
while (i > 0) {
printf(" ", i);
}
return 0;
}
int main (void) {
int i;
i = 1;
while (i < 6) {
print_line(i, 11-2*i);
i = i + 1;
}
print_line(6, 0);
i = 5;
while (i > 0) {
print_line(i, 11-2*i);
i = i - 1;
}
return 0;
}
Re: Same patter, new problem
Quote:
Originally Posted by
adam86107
Okay, so I've finally got it printing but its printing an infinite number of asterisk. I know it needs an \n in the printf statement that prints the last set of asterisk but when I added another loop i got a forbidden nested loop error. Here's how it looks now:
Don't know what the "forbidden nested loop error" is. Did you read MrViggy's reply in post #2? He mentioned that you are calling the print_line function with the wrong number of arguments. You might want to fix this error first before moving on to other things.
Re: Same patter, new problem
Quote:
Originally Posted by
Arjay
Don't know what the "forbidden nested loop error" is. Did you read MrViggy's reply in post #2? He mentioned that you are calling the print_line function with the wrong number of arguments. You might want to fix this error first before moving on to other things.
I tried that but for some reason every time I replaced (void) with (i, 11-2*i) in the print_line function it gives me a "parse error before numeric constant" error. I've now managed to get it to print everything from infinite asterisks to infinite spaces.
Re: Same patter, new problem
Quote:
Originally Posted by
adam86107
I tried that but for some reason every time I replaced (void) with (i, 11-2*i) in the print_line function it gives me a "parse error before numeric constant" error. I've now managed to get it to print everything from infinite asterisks to infinite spaces.
You might want to read up on how to declare and implement parameters for a function.
Re: Same patter, new problem
So here is what I have now, still cannot get rid of the error for print_line having too many arguments in main.
int print_line(int i) {
i = 1;
while (i < 6) {
printf("*", 11-2*i);
i = i + 1;
}
while (i > 0) {
printf (" ", i);
i = i - 1;
}
i = 11-2*i;
while (i < 6) {
printf("*\n", 11-2*i);
i = i + 1;
}
return 0;
}
int main (void) {
int i;
i = 1;
while (i < 6) {
print_line(i, 11-2*i);
i = i + 1;
}
print_line(6, 0);
i = 5;
while (i > 0) {
print_line(i, 11-2*i);
i = i - 1;
}
return 0;
}
Re: Same patter, new problem
Quote:
Originally Posted by
adam86107
still cannot get rid of the error for print_line having too many arguments in main.
You can't learn C++ through trial and error. You need to get a good book (see the FAQ) and follow that page for page.
Re: Same patter, new problem
Quote:
Originally Posted by
adam86107
So here is what I have now, still cannot get rid of the error for print_line having too many arguments in main.
Yep. The compiler is very smart. It knows you are declaring the print_line function with one parameter, but are trying to pass in two parameters.
The code is an improvement since previously you declared the print_line function with no parameters (e.g. printline(void)) and still tried to pass in two parameters.
You might want to find an online C++ tutorial and work through the chapter on passing parameters to functions. If you take just a few minutes to do this, the error you are making will be immediately obvious.
Re: Same patter, new problem
In addition to the "go find a tutorial or a good book" advice, I would also add: until you know how something works, keep your tests as simple as possible.
For example, if you need to learn how to declare and use functions, take out all of the math and loops to get something like:
Code:
int print_line(int i) {
printf("*", i);
return 0;
}
int main (void) {
print_line(123);
return 0;
}
Get the very basic thing working, then modify it so you understand how it works (ie. add more inputs, change the return type, test how returning variables work). That way you get to see what's going on without the visual clutter and without accidentally changing something important in some other area.
Oh yeah, and also the [ code ] and [ /code ] tags (without the spaces) are your friend for posting on this forum.
Re: Same patter, new problem
Quote:
Originally Posted by
Ankheg
In addition to the "go find a tutorial or a good book" advice, I would also add: until you know how something works, keep your tests as simple as possible.
For example, if you need to learn how to declare and use functions, take out all of the math and loops to get something like:
Code:
int print_line(int i) {
printf("*", i);
return 0;
}
int main (void) {
print_line(123);
return 0;
}
Get the very basic thing working, then modify it so you understand how it works (ie. add more inputs, change the return type, test how returning variables work). That way you get to see what's going on without the visual clutter and without accidentally changing something important in some other area.
Oh yeah, and also the [ code ] and [ /code ] tags (without the spaces) are your friend for posting on this forum.
This
does not do what you think it does.
Viggy
Re: Same patter, new problem
Quote:
Originally Posted by
MrViggy
This
does not do what you think it does.
Viggy
If it's me you're addressing (as I have to assume since you're quoting me), I think I have a reasonable idea of what that does. I also doubt it's doing what he wants it to do.
My objective wasn't to fix his code when I wrote that snippet, it was to boil down the problem (problems?) to the point where it should be easy to figure out. Perhaps I should have been more clear about that.
adam86107: I assume you want that printf to output the number you give it? Start there, and get that working. Then you can use that knowledge to test other things.
Edit: Oh, looking back I'm thinking he just wanted to output a bunch of stars and spaces, and just left in the second parameter to printf because it looked right to him and didn't give him errors. Here adam, try putting this isn't the code snippet I gave you instead of the printf line:
Code:
printf("There are %d lights!\n",i);
That might give you a better idea of how printf works, and should help you figure out how the function parameters work.
Re: Same patter, new problem
Quote:
Originally Posted by
Ankheg
If it's me you're addressing (as I have to assume since you're quoting me), I think I have a reasonable idea of what that does. I also doubt it's doing what he wants it to do.
My objective wasn't to fix his code when I wrote that snippet, it was to boil down the problem (problems?) to the point where it should be easy to figure out. Perhaps I should have been more clear about that.
adam86107: I assume you want that printf to output the number you give it? Start there, and get that working. Then you can use that knowledge to test other things.
Edit: Oh, looking back I'm thinking he just wanted to output a bunch of stars and spaces, and just left in the second parameter to printf because it looked right to him and didn't give him errors. Here adam, try putting this isn't the code snippet I gave you instead of the printf line:
Code:
printf("There are %d lights!\n",i);
That might give you a better idea of how printf works, and should help you figure out how the function parameters work.
Yes, it was.
Is redundant, as the 'i' is discarded. You could have (and should have) written:
As, there are no format specifiers in the character string.
Viggy
Re: Same patter, new problem
Quote:
Originally Posted by
MrViggy
Yes, it was.
Is redundant, as the 'i' is discarded. You could have (and should have) written:
As, there are no format specifiers in the character string.
Viggy
Ankheg said:
Quote:
My objective wasn't to fix his code when I wrote that snippet, it was to boil down the problem (problems?) to the point where it should be easy to figure out.
That, IMO is much more valuable teaching to give the OP than 'your problem is here --> line of code'. Teaching how to fish vs giving fish.
Re: Same patter, new problem
Quote:
Originally Posted by
Martin O
Ankheg said:
That, IMO is much more valuable teaching to give the OP than 'your problem is here --> line of code'. Teaching how to fish vs giving fish.
Teaching by giving an incorrect example, and not stating that it's incorrect? Not a good way of teaching, IMHO.
Viggy
Re: Same patter, new problem
Quote:
Originally Posted by
MrViggy
Teaching by giving an incorrect example, and not stating that it's incorrect? Not a good way of teaching, IMHO.
Viggy
Ok you have a point. He should have, in addition to demonstrating how to boil down the problem, also pointed out the incorrect printf statement.