CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Sep 2010
    Posts
    22

    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.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    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

  3. #3
    Join Date
    Sep 2010
    Posts
    22

    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.

  4. #4
    Join Date
    Sep 2010
    Posts
    22

    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;
    }

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Same patter, new problem

    Quote Originally Posted by adam86107 View Post
    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.

  6. #6
    Join Date
    Sep 2010
    Posts
    22

    Re: Same patter, new problem

    Quote Originally Posted by Arjay View Post
    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.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Same patter, new problem

    Quote Originally Posted by adam86107 View Post
    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.

  8. #8
    Join Date
    Sep 2010
    Posts
    22

    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;
    }

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Same patter, new problem

    Quote Originally Posted by adam86107 View Post
    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

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Same patter, new problem

    Quote Originally Posted by adam86107 View Post
    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.

  11. #11
    Join Date
    Aug 2010
    Posts
    47

    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.

  12. #12
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Same patter, new problem

    Quote Originally Posted by Ankheg View Post
    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
    Code:
    printf("*", i);
    does not do what you think it does.

    Viggy

  13. #13
    Join Date
    Aug 2010
    Posts
    47

    Re: Same patter, new problem

    Quote Originally Posted by MrViggy View Post
    This
    Code:
    printf("*", i);
    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 &#37;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.

  14. #14
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Same patter, new problem

    Quote Originally Posted by Ankheg View Post
    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 &#37;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.
    Code:
    printf("*", i);
    Is redundant, as the 'i' is discarded. You could have (and should have) written:
    Code:
    printf("*");
    As, there are no format specifiers in the character string.

    Viggy

  15. #15
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Same patter, new problem

    Quote Originally Posted by MrViggy View Post
    Yes, it was.
    Code:
    printf("*", i);
    Is redundant, as the 'i' is discarded. You could have (and should have) written:
    Code:
    printf("*");
    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured