1 Replies - 235 Views - Last Post: 05 April 2010 - 02:48 AM Rate Topic: -----

#1 laura9008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-September 09

inputting information into a text file from function

Posted 05 April 2010 - 02:22 AM

k, so what i need to know is how do i put info. into the text file in the main function from the above functions being called in the main.. :/..if that makes sense...
i need p[i] to print in the text file and just entering output in void print isnt cutting it nor is calling the function in my output statement in main under radixsort..
any help at all is appreciated...thanks.
void print(int *p,int s)
{
        int i;
        for(i = 0; i < s; i++)
        cout << p[i] << " ";
        cout << endl;
}
void radixsort(int *p,int s)
{
                int i, a[MAX], c = 0, first = 1;
                for(i = 0; i < s; i++)
                {
                        if(p[i] > c)
                                c = p[i];
                }
                while(c/first > 0)
                {
                        int temp[10]={0};
                        for(i = 0; i < s; i++)
                                temp[p[i]/first%10]++;
                        for(i = 1 ; i < 10; i++)
                                temp[i]+=temp[i-1];
                        for(i = s-1; i >= 0; i--)
                                a[--temp[p[i]/first%10]]=p[i];
                        for(i = 0; i < s; i++)
                                p[i]=a[i];
                        first*=10;

                    cout << "\nSorted numbers: ";
                    print(p,s);
        		}
}
int main()
{
    char textFile[20];
    int arr[MAX];
    int i, s;
    
    cout << "Enter name of file to save sorted numbers to: ";
    cin >> textFile;
    ofstream output;
    output.open(textFile);
    output << textFile << endl;
    
    cout << "Enter total numbers to be sorted (numbers must be less than " <<
     MAX << "): ";
    output << "Total numbers to be sorted: ";
    cin >> s;
    output << s << endl;
    
    cout << "Enter " << s << " numbers: " << " ";
    output << "Numbers to be sorted: ";
    for(i = 0; i < s; i++)
    {
      cin >> arr[i];
      output << arr[i] << " ";
    }
    radixsort(&arr[0], s);
    //output << "\nSorted numbers: " ;
    
    output.close();
    
    system("PAUSE");
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: inputting information into a text file from function

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: inputting information into a text file from function

Posted 05 April 2010 - 02:48 AM

What about passing ofstream to your "print" function?
// Pass by reference, won't work otherwise
void print(int *p,int s, ofstream &file)
{
   int i;
   for(i = 0; i < s; i++)
       // Output in file:
       file << p[i] << " ";
   file << endl;
}


And of course call the function with proper arguments:
print(p, s, output);


PS: You need to pass ofstream & to your radixsort function as well.

This post has been edited by sarmanu: 05 April 2010 - 02:52 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1