returning dynamic array from function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bushnellweb
    New Member
    • Jan 2010
    • 48

    returning dynamic array from function

    The output for newInfo[0] is blank. it should be the record number

    Code:
    string *changeData(string arr[][4],int records)
    {
    	string newInfo[5];
    	stringstream ss;
    	ss << recordSelected;
    	newInfo[0] = ss.str();
    	
    	cin.ignore(1);
    	cout << "New ID: ";
    	getline(cin,newInfo[1]);
    	cout << "New DESC: ";
    	getline(cin,newInfo[2]);
    	cout << "New COST: ";
    	getline(cin,newInfo[3]);
    	cout << "New SUPPLY: ";
    	getline(cin,newInfo[4]);
    
    	string *arr2 = newInfo;
    
    	system("pause");
    
    	return(arr2);
    }
    
    int main()
    {
        int c = countRecords(fileName,false);//  c=2
        string (*p)[4] = new string[c][4];
    
         string newInfo[5] = {*changeData(p,c)};
    
    cout << newInfo[0] << endl; // [B]Output is blank[/B]
    }
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Returning a pointer to a local variable (in this case, string) is not good - it may be overwritten in any time after return.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      Try debugging your software with the associate debugger.

      By the way what is this line?
      ss << recordSelected;

      what is var type of recordSelected. I dont know how

      ss << recordSelected;. If recordSelected is any kind of number(int, long) type variable and ss is string then check whether this line work at all..
      Last edited by johny10151981; Feb 10 '10, 09:28 AM. Reason: in complete message

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Why not use a vector instead of all this array stuff? All the vector is is an array that has the control code already written and debugged so you don't have to do it over and over and over for your own arrays.

        Comment

        • bushnellweb
          New Member
          • Jan 2010
          • 48

          #5
          hmm I never learned vectors..I guess I will have to look into it

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            If you can use an array, you can use a vector. vector is required to be implemented as an array.

            Code:
            vector<int> arr;              //create rthe array
            arr.push_back(2);          //add 2 to the end of the array
            cout << arr[0];              //display the 2.
            It's not too hard.

            Comment

            • bushnellweb
              New Member
              • Jan 2010
              • 48

              #7
              @weaknessforcat s - thanks man that really helps, wasn't too hard to figure out mutli-dim vectors.

              Comment

              • bushnellweb
                New Member
                • Jan 2010
                • 48

                #8
                another problem

                I switched my app to vectors and got this error already. any ideas?

                error: debug assertion failed! expression: vector subscript out of range

                Code:
                #include <iostream>
                #include <string>
                #include <fstream>
                #include <new>
                #include <iomanip>
                #include <sstream>
                #include <vector>
                
                using namespace std;
                
                
                /*
                	function: countRecords
                	params: string fileName, bool outputTF
                	purpose: counts records from passed file name and outputs data if bool is true
                */
                int countRecords(string fileName, bool outputTF)
                {
                	ifstream inFile;
                	int recordCount = 0;
                
                	inFile.open(fileName.c_str(),ios::in);
                	if(inFile.is_open())
                	{
                		//priming read
                		inFile.ignore(1000,'\n');
                		recordCount = 1;
                
                		//loop
                		while(!inFile.eof())
                		{
                			inFile.ignore(1000,'\n');
                			recordCount++;
                		}
                
                		if(outputTF == true)
                		{
                			cout << "recordCount= " << recordCount << endl;
                		}
                		
                	}else{ cout << "File would not open!" << endl; }
                	inFile.close();
                
                	return recordCount;
                }
                
                // ***************** MAIN PROCEDURE ***********************
                int main()
                {
                	const int fields = 4; //creates constant int of 4 fields (number of fields doesn't usually change)
                	const char fieldDel = '_'; //creates constant char for field delimeter
                	string fileName = "data1.dat"; //define fileName
                	int c = countRecords(fileName,false); //define number of records in data1.dat
                
                	vector <vector<string>> vec1; //create 2 dimensional vector
                
                	ifstream inFile; //define ifstream as inFile - lets data from data1.dat into the app
                	inFile.open("data1.dat",ios::in); //opens inFile for reading
                	if(inFile.is_open()) //only do actions to inFile if file actually opens
                	{
                		while(!inFile.eof()) //only do actions while inFile is not at EOF
                		{
                			vector <string> rowVec; //creates temporary vector for adding to main vector
                			for(int i=0; i<c; i++) //loops through records
                			{
                				for(int t=0; t<fields; t++) //loops through # of fields
                				{
                					if(t == fields-1) //if t is equal to fields-1 then read until '\n' instead of field delimeter
                					{
                						getline(inFile,rowVec[t],'\n');
                						vec1.push_back(rowVec); //pushes rowVec into multi-dim vector
                					}else{
                						getline(inFile,rowVec[t],fieldDel);
                					} //end if(t == fields-1)
                				}// end field loop
                			} //end record loop
                		} //end while(!inFile.eof())
                	} //end if(inFile.is_open())
                
                	
                
                
                
                
                
                return 0; //return 0
                }

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Yes at some point while running your code you tried to access an index in the vector that was beyond its size.

                  Your best option is to running it in a debugger, the assurtion should cause the debugger to halt when the error happens and you will be able to see what has happened by examining the call stack.

                  Comment

                  • bushnellweb
                    New Member
                    • Jan 2010
                    • 48

                    #10
                    whats a good debugger?

                    Comment

                    • bushnellweb
                      New Member
                      • Jan 2010
                      • 48

                      #11
                      oh you know what I think its because I am doing

                      getline(inFile, rowVec[t],'\n');

                      and rowVec[t] hasn't been defined yet. I need to push_back the data. not getline

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Indeed one of the basic rules of programming that.

                        If the data in an object is worth examining then its worth populating.

                        Comment

                        Working...