|
-
October 22nd, 2010, 02:30 PM
#1
Same patter, new problem
Ok, so I've gotta print this pattern and main has already been defined. I just need to create the function that main calls and define it. The problem is that the compiler keeps coming up with an error in main that the function has too many arguments, here is what I have:
#include <stdio.h>
int print_line (void) {
int i;
i = 1;
while (i < 6) {
printf("*", i);
i = i + 1;
}
while (i > 0) {
printf (" ", i);
i = i - 1;
}
while (i < 6) {
printf ("*\n", 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;
}
The instructions doesn't actually say that I need to create function print_line but that I need to define print_line. It needs to have 3 loops, one for the number of spaces in each line and 2 for the number of asterisks in each line.
I know now that this is C and it is posted in a C++ forum but I couldn't find a C forum on the site anywhere and as last time I'm not looking for the answer by no means, but just a nudge in the right direction.
-
October 22nd, 2010, 02:32 PM
#2
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
-
October 23rd, 2010, 01:56 PM
#3
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.
-
October 23rd, 2010, 02:37 PM
#4
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;
}
-
October 23rd, 2010, 04:23 PM
#5
Re: Same patter, new problem
 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.
-
October 23rd, 2010, 04:53 PM
#6
Re: Same patter, new problem
 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.
-
October 23rd, 2010, 06:07 PM
#7
Re: Same patter, new problem
 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.
-
October 24th, 2010, 04:48 PM
#8
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;
}
-
October 24th, 2010, 04:58 PM
#9
Re: Same patter, new problem
 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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 24th, 2010, 05:08 PM
#10
Re: Same patter, new problem
 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.
-
October 25th, 2010, 01:32 PM
#11
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.
-
October 25th, 2010, 04:04 PM
#12
Re: Same patter, new problem
 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
-
October 26th, 2010, 07:29 AM
#13
Re: Same patter, new problem
 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.
Last edited by Ankheg; October 26th, 2010 at 07:42 AM.
-
October 26th, 2010, 10:17 AM
#14
Re: Same patter, new problem
 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
-
October 26th, 2010, 11:22 AM
#15
Re: Same patter, new problem
 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:
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|