Return an array from function

This is a discussion on Return an array from function within the C++ Programming forums, part of the General Programming Boards category; Hi! Could aneone take a look at this code? I'm pretty n00b to C++ and i want to return an ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Return an array from function

    Hi!
    Could aneone take a look at this code?
    I'm pretty n00b to C++ and i want to return an array from a function within af class!
    But it gives me an error saying: "initializer fails to determine size of `ord'" and "invalid initializer".
    I am btw compiling with Dev-C++ 4.9.9.2
    Here's the code:

    Code:
    #include <iostream>
    using namespace std;
    
    class krypter
    {
         private:
         string a;
         int size;
         public:
    
         int tal_til_bogstav_ascii (char a[], int size)
         {
             for(int i=0;i<=size;i++)
             {
                     switch (a[i])
                     {
                            // Dont mind this function!
                     }
             }
         }
    
         int calculate_array (char a[])
         {
             size = 0;
             for(int i=0;i<=100;i++)
             {
                     if(a[i] != 0)
                     {
                             size = size + 1;
                     }
                     else
                         break;
             }
         return (size);
         }
    
         char set_array (char *a[], const int size)// this is the one!
         {
             char array[size];
             for(int i=0;i<=100;i++)
             {
                     if(a[i] != 0)
                     {
                             array[i] = *a[i];
                     }
                     else
                         break;
             }
         return (*array);
         }
    
    };
    
    int main()
    {
       start:
    
       krypter size1;
       krypter array;
       char input[100];
       cout << "What's ur word?" << endl;
       cin >> input;
       char * inp = input;
       int size = size1.calculate_array(input);
       cout << "There's " << size << " letters/signs in ur word" << endl<< endl;
    
       const int stor = size;
       char** in = (char**) input;
    
       char ord[] = array.set_array(in, size);// SHOULD init. ord[] but no :S
            for(int i=0;i<size;i++)
                    cout << ord[i];
    
    
       goto start;
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>cin >> input;

    you can't do that with an array. need to create a loop and prompt for each element
    Code:
    for(int i = 0; i < 100; i++)
       cin >> input[i];

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    tu! i'll try that and post back if I still have probs

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    there are other problems. For example, return (*array); only returns the first character of that array, not the entire array. char arrays that are allocated on the stack cannot be returned because they disappear when they go out of scope.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > char array[size];
    You can't declare arrays with a size you only get to find out about at run time.

    > for(int i=0;i<=100;i++)
    Woah, where did the size parameter go ?
    What if your string is longer than 100 characters?
    I thought std::string had a .length function anyway.

    > char** in = (char**) input;
    Consider casting in C++ as a sign you're about to do something wrong.
    This is no exception, this is wrong.

    > set_array
    Perhaps a description of what you want this function to do would help us figure out what sort of parameters you should be passing to it, and the result you expect.

    > goto start;
    Ugh, use a while loop man.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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