Thread: frustrated bout char array

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    8

    frustrated bout char array

    hello! is there a way to declare a char array of length that will be determined by a variable later?

    i'm working on a syntax checker program for a programming language and i have to use strlen() to check the longest line of code in the program before i can declare the length of the char array.

    i know that i can use string or other containers for this but the library my professor gave me accepts only char array. for now, i'm just declaring a char array of size 901 but i know this would not be acceptable.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try this:
    Code:
    #include<iostream>
    #include<cctype>
    
    int main()
    {
            char*line;
            int num;
    
            std::cout<<"Enter the size: ";
            std::cin>>num;
    
            line=new char[num];
    
            for(int i=0;i<num;i++)
            {
                    line[i]='1';
            }
    
            std::cout<<"A char array of "<<strlen(line)<<" elements has been created\n";
            delete[]line;
            return 0;
    }
    not a great example, but I think it gets the point across...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >i'm just declaring a char array of size 901 but i know this would not be acceptable

    That probably isn't as oddball as you might think. For example, you might do something like this:

    1)assign 901 to a const int representing a number likely to be longer than the likely maximum length. Declare a char array using the const int to temporarilyuhold each
    line as you evaluate it it's actual length.

    2)use a loop to read the file/program one line at a time using getline, the char array
    from #1 above, and the const int.

    3)within the loop determine the actual length of each line. compare it to a variable representing the longest line in the program found so far. if length of current line is
    longer than length of longest line found so far, assign current length to variable holding
    value of longest line found so far.

    4)at end of loop use the actual maximum length found + 1 to declare the size of the
    length of the char array you will use the rest of the program.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM