CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2011
    Posts
    6

    Post Return an string array from a function C++

    I'm trying to return a string array from a function. Within this function the user will define the legnth of the array and then populate the array. I need the array return to the call so that I can send it to a print function when the time comes. I have include the code that I have, thanks for the help

    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include "windows.h"
    #include <dos.h>

    using namespace std;
    //int get_Num_Units( );
    void output(string& s1);
    void get_pressure(double p);
    void nuclear();
    void industrial();
    string *Seri_Num();
    int get_Unit_Num();
    int get_chameber_Num();

    int main()
    {

    //string unit_one,unit_two,unit_three;
    int unit_type;
    //double pressure;
    //int check=0;

    //cout << "Enter the max pressure:" << endl;
    //cin >> pressure;
    cout << " 1. Nuclear units press 1 " << endl;
    cout << " 2. Industrial nuits press 2 " << endl;
    cin >> unit_type;


    if (unit_type == 1)
    {

    nuclear();
    }
    else if (unit_type == 2)
    {
    void industrial();
    }


    //cout <<"Your name is: "<< myname << endl;

    return 0;
    }

    //
    void nuclear()
    {

    int units,chamber;
    string Seri = "";

    chamber = get_chameber_Num();
    //units = get_Unit_Num();
    Seri = Seri_Num();



    }
    //This is uesd to get the chamber number
    int get_chameber_Num()
    {
    system("CLS");
    int chamber;
    char check = 'y';
    do
    {


    cout << "Enter the chamber number: " << endl;
    cin >> chamber;

    cout << "Is this correct?? " << endl;
    cin >> check;

    }
    while( check != 'y');
    system("CLS");
    return chamber;
    }

    // This is to get the total number of units from the user
    int get_Unit_Num()
    {

    int unit_num;
    do
    {
    system("CLS");
    cout << "Enter number of units:" << endl;
    cin >> unit_num;

    if(unit_num > 3)
    {

    cout << "Enter a number no greater than 3: " << endl;

    }




    }
    while(unit_num > 3);
    system("CLS");
    return unit_num;
    }

    // this function is used to load the array and get the serial numbers from the user

    string *Seri_Num()
    {
    string serNum;
    int unit_num;
    string *unit= new string[unit_num];


    do
    {
    system("CLS");
    cout << "Enter number of units:" << endl;
    cin >> unit_num;

    if(unit_num > 3)
    {

    cout << "Enter a number no greater than 3: " << endl;

    }




    }
    while(unit_num > 3);
    system("CLS");

    for (int i=0; i< unit_num; i++) // This loads the array
    {
    cout << "Enter serial number (8 charecter max) \n";
    cin >> serNum;
    unit[i] = serNum;
    }
    return unit; // return the array


    }
    Attached Files Attached Files
    Last edited by hfbroady; July 13th, 2011 at 08:57 PM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Return an string array from a function C++

    Quote Originally Posted by hfbroady View Post
    I'm trying to return a string array from a function. Within this function the user will define the legnth of the array and then populate the array.
    Sorry, that's plain impossible in C++ since the size of a C++ array needs to be known at compile-time. Looks like what you actually want is an std::vector that not only can be sized at runtime, but also can be returned from a function much easier than an array.

    Note that I didn't actually have a (significant) look at your code because it's practically unreadable due to you not using code tags. Please use them next time you post code, otherwise you can't really expect anyone here to read your code. However, in this case, the answer to your question actually was apparent from the question itself.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Return an string array from a function C++

    Quote Originally Posted by hfbroady View Post
    I'm trying to return a string array from a function. Within this function the user will define the legnth of the array and then populate the array. I need the array return to the call so that I can send it to a print function when the time comes. I have include the code that I have, thanks for the help
    Code:
    string *Seri_Num();
    You didn't include the <string> header, so this is not guaranteed to compile. You must include all headers that you are using in the module, even if one of those other headers includes <string> just by chance.
    Code:
    string *Seri_Num()
    {
        string serNum;
        int unit_num;
        string *unit= new string[unit_num];
    unit_num is uninitialized. Therefore it has a garbage value, but you're using it in the new[] operation.

    In general, your code uses error-prone pointer manipulations and has memory leaks. C++ has a std::vector class that has been standard C++ since 1998. Why are you not using it, and instead, you're using error prone methods?

    As Eri523 stated, use vector, then the issue becomes much more manageable than your current code.
    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    typedef std::vector<std::string> StringArray;
    
    StringArray Seri_Num()
    {
        int unit_num;
        cout << "Enter number of units:" << endl;
        cin >> unit_num;
        StringArray unit(unit_num);
    
         //...
    
         return unit; // return the array
    } 
    
    int main()
    {
        StringArray arr = Seri_Num();
        cout << "There are " << arr.size() << " entries in the array\n";
    }
    That code just created an array of strings, and returned the array. Note that there are no pointers, no dynamic allocation, no memory leaks, etc. In other words, it works.

    And next time, please use code tags if you're posting code.

    Regards,

    Paul McKenzie

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

    Re: Return an string array from a function C++

    Quote Originally Posted by hfbroady View Post
    I have include the code that I have, thanks for the help
    Besides the above remarks, the way you are handling user input is also not robust. Have a look at these FAQ's: http://www.parashift.com/c++-faq-lite/input-output.html
    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

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