Simple Code Gone Wrong

This is a discussion on Simple Code Gone Wrong within the C Programming forums, part of the General Programming Boards category; Help, I'm trying to code a C program that produces the output: @ @@ @@@ @@@@ @@@@@ However, what I've ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    1

    Simple Code Gone Wrong

    Help, I'm trying to code a C program that produces the output:

    @
    @@
    @@@
    @@@@
    @@@@@

    However, what I've done does not produce the correct output, where did I go wrong?


    #include <stdio.h>

    int main()
    {
    int lines = 0;
    int chars = 1;

    while(lines<=4)
    {
    printf("@");


    while(chars<=lines)
    {
    printf("@");
    chars++;
    }

    printf("\n");
    lines++;
    }

    printf("\n\n\n");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    while(lines<=4)
    {		
        while(chars<lines)
        {
             printf("@");
             chars++;
        }
        printf("@");
        printf("\n");
        lines++;
        chars = 0;/* Reset to zero*/
    }

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    you just need to reset 'chars' to 1 after each inner while loop....

    oh, and use code tags...

    Code:
    #include <stdio.h>
    
    int main()
    {
    int lines = 0;
    int chars = 1;
    
    while(lines<=4)
    {
    printf("@");
    
    
    while(chars<=lines)
    {
    printf("@");
    chars++;
    }
    chars = 1;
    
    printf("\n");
    lines++;
    }
    
    printf("\n\n\n");
    return 0;
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Or :
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char buf[] = "@@@@@";
      int i;
      
      for (i = 1; i <= 5; i++)
        printf ("%.*s\n", i, buf);
        
      return(0);
    }
    
    /*
    Output:
    @
    @@
    @@@
    @@@@
    @@@@@
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    puts( "
    @
    @@
    @@@
    @@@@
    @@@@@
    ");
    Or perhaps:
    Code:
    puts("@\n@@\n@@@\n@@@@\n@@@@@");


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by vVv
    Your first version isn't correct C code, quzah. You aren't allowed to spread string literals over multiple lines.
    Sure you are.

    6.4.5 String literals

    A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes.
    How does my example not fulfill that role?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not finding anything on it in the text version I have. However, considering that a string starts and stops on non-escaped " marks, I don't see how it's possible for that to be invalid.

    To illustrate the point, simply leave out a quotation mark and watch your compiler (any compiler, and I do mean any) keep reading until it finds a matching " anywhere else in the program:
    Code:
    #include <stdio.h>
    int main ( void )
    {
        char * s = "hello; /*oops, no quote*/
        int x;
    
        printf(";");  <--- your compiler would pick up here
        return 0;
    }
    I was going to try and make an example that would look realistic that would still compile, but I couldn't think of anything off hand that would still look like real code (ie: not obfuscated) that would compile.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nifty. I was under the impression that it would just take the embedded newline and treat it as one. I've always seen it do so before. Actually I believe the only example of such was a rather obscure illustration.

    That in mind, I really don't see the point in the ANSI group's decision to disallow it. I suppose you could run into some obscure implementation where it might be problematic, though I honestly can't envision it.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    or you could do:
    Code:
    puts(
    "@\
    @@\
    @@@\
    @@@@\
    @@@@@"
    );

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    8

    Re: Simple Code Gone Wrong

    This will solve your problem!!

    # include <stdio.h>
    # include <conio.h>
    void main ()
    {
    int i,j;
    char a='2';
    for (i=1;i<6;i++)
    {
    for (j=1;j<=i;j++)
    {
    printf ("%c", a);
    }
    printf ("\n");
    }

    getch ();
    }


    Originally posted by mrlucky0
    Help, I'm trying to code a C program that produces the output:

    @
    @@
    @@@
    @@@@
    @@@@@

    However, what I've done does not produce the correct output, where did I go wrong?


    #include <stdio.h>

    int main()
    {
    int lines = 0;
    int chars = 1;

    while(lines<=4)
    {
    printf("@");


    while(chars<=lines)
    {
    printf("@");
    chars++;
    }

    printf("\n");
    lines++;
    }

    printf("\n\n\n");
    return 0;
    }

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    13

    here's ur mistake

    chars once it is incremented..must be reset back to 1..to get the expected output...

    do it and run...

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was under the impression that it would just take the embedded newline and treat it as one.
    This behavior falls outside of the standard's "jurisdiction". At best it would have to be described as implementation-defined, but such a feature would no doubt reach holy war status and you would find too much nonportable code floating around. It's far better to simply disallow it completely and sidestep the problem.

    >I really don't see the point in the ANSI group's decision to disallow it.
    Your example is really the only valid one I can think of, everything else would be an error. If, in a situation where you forget a closing paren and when one is finally found later the code remains valid, you have a potentially hard to find bug. Then there's the problem of actually parsing the line. Most sane programmers will indent for readability, but does the parser include those spaces or just the embedded newlines? Either could be considered useful, and both can cause confusion because neither is completely intuitive. It's a feature that is full of problems and offers little gain for dealing with the problems.

    >This will solve your problem!!
    >void main ()
    void main isn't a solution, it's a grievous error. Your choice of including a nonportable header and function (conio and getch) for something so simple as a hedge against the program closing before the user can read the output is silly. Even more so because you can get a similar effect with getchar.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong with this very simple code?
    By sway1 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2008, 01:18 PM
  2. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 01:02 PM
  3. Whats wrong with this simple code?
    By o14v in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 02:46 PM
  4. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21