Nested Iteration??

This is a discussion on Nested Iteration?? within the C++ Programming forums, part of the General Programming Boards category; Ok, i'm getting annoyed now. I use Visual C++ 6.0, and i've been trying to understand how to use nested ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    3

    Angry Nested Iteration??

    Ok, i'm getting annoyed now.
    I use Visual C++ 6.0, and i've been trying to understand how to use nested iteration to produce this triangle of letters.

    Having spent a good two hours reading through some useful books, but i'm still stuck. i initialise my char array and give it the relative elements, but when it comes down to using the elements from my array, it says they aren't initialised or something like that.
    When I tried an alternative, it tells me that i have too may characters as the name for most elements in the array.
    I think I can get the program to print certain elements, dependent on the user, but this whole assigning long collections of letters as elements doesn't seem to be greeted with much appreciation by my computer.

    By the way, i'm a beginner, so be nice!!

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    As if anyone not intimately familiar with your problem would have any idea what you're talking about??

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    code?

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    3
    well, i'm still stuck...

    forgive the obscene amount of letters in the array, but it's all i can think to do, but here is what i have managed so far:

    [code]
    #include<stdio.h>
    main()

    {
    int i, z;
    char char_array[25]={'a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz'};

    printf("Choose a number between 0 and 25 in correlation with your chosen letter\n\n");
    scanf("%i", &z);
    for(i=0;i<z;i++)
    {

    printf("%c", char_array[i]);

    }

    return 0;
    }
    [\code]

    When I compile, it tells me that there are 'too many initialisers' once, 'too many characters in constant' (22 times), 3 warings saying 'truncation of constant value' and 3 saying 'truncation from 'const int' to 'char''. Is it something to do with the size of the array being too small???

    The only other way is to use lots of 'if' fuctions, but i don't think i'm meant to

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Well, your making an array with enough room to store 25 arrays and you try to store 26 char's.

    Try
    Code:
    char array[] = {....};
    
    or 
    
    char array[26] = {...};

  6. #6
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    You're creating an enormous amount of non-null terminated strings. First thing you should do is change all that 'a', 'bb' etc. for "a", "bb", "cc" etc. Otherwise when you start printing the strings, it will print garbage data untill it reaches a null char.
    I'm not sure if this will help to reduce the number of errors you have, but your program will work the way you expect it to.
    Good Luck
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    3
    Wondeful stuff guys!!

    I've done that array[] thing, and " instead of ' and all it says now is that i have too many initailisers-what does this mean?I've tried chanigning a few bits and pieces of the program and looking it up in the help file, but nothing's working!

    All these web sites say it's because i have too many elements in the array, but using the empty square brackets should nullify that, so is it something to do with not having a null character in the array?

    And Re: Salem, does the extra loop involve the getprevious command? I don't understand how it works, and the help file confuses me.
    Last edited by Mewr; 08-04-2003 at 10:43 AM.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    There are 25 spaces and you've filled it to 26

    change the [25] to [26]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 05:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 06:25 AM
  4. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM
  5. The "continue" statement in JAVA
    By Hexxx in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-26-2004, 06:19 PM

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