Creating a file by name of user input

I need to give the user the option to write the name for a new file and then write an array to the file. I already did the array and it looks something like this. ( the program is done using functions so do i have to point the "inputted_row and _column to following function? ) How do i do all of this??



int *matrix = new int[inputted_row] ;
matrix = new int[inputted_column] ;
	
	for( column = 0 ; column < inputted_row ; ++column )
		{ 
			for( row = 0 ; row < inputted_column ; ++row )
				{	 
					random_number = rand()%100 ;
					cout << random_number << " "  ;
					
				}
			cout << endl ;
		}
	
	delete [] matrix;
Last edited on
you know how to ask the question, do you know how to use cin for user input? do you know the string class? do you know about ofstream class? Between everything I have mentioned I can set up the code I need to dump the file.
I dont understand what you're trying to say but i managed to get the file out. im having trouble now pointing the variables and printing them to the output file, the adresses come up when i do this. how do i fix it?

int File_WriteOut ( int random_number , int *matrix , int *inputted_row ) //fxn 3
{
ofstream yourfile ;
string filename ;



cout << "input a filename : \t" ;
cin >> filename ;
yourfile.open( filename.c_str() ) ;
cout << *inputted_row << endl ;
yourfile << *inputted_row ;
yourfile << "written" ;
yourfile.close() ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

//if you set up these like this you create a problem immediately.

int *matrix = new int[inputted_row] ;
matrix = new int[inputted_column] ;

// this should look like 
int *matrix = new int[inputted_row] [inputted_column];


// your loops would look like this
for(int row = 0 ; column < inputted_row ; ++row )
{ 
      for(int column = 0 ; row < inputted_column ; ++column )
      {	
             random_number = rand()%100 ;
             cout << random_number << " " ;
             *matrix[row][column] = rand_number;

      }
      cout << endl ;
}

// output routine
ofstream yourfile ;
string filename ;

cout << "input a filename : \t" ;
cin >> filename ;

yourfile.open( filename.c_str() );
for(int row = 0 ; column < inputted_row ; ++row )
{ 
      for(int column = 0 ; row < inputted_column ; ++column )
      {	
             yourfile << *matrix[row][column] << " ";

      }
      yourfile << endl ;
}


yourfile.close() ;

i actually get errors if i i make this declaration like this
// int *matrix = new int[inputted_row] [inputted_column];


error C2540: non-constant expression as array bound
error C2440: 'initializing' : cannot convert from 'int (*)[1]' to 'int *'
error C2109: subscript requires array or pointer type

what i need to do is write "inputted_row" and "inputted_column" to the first line of the file and then the rest of the random numbers array. when i point these variables to the next function and attempt to print them to the file, only the hex addresses come up. I kno its got to do with the poor use of pointers, but what?
I guess I don't understand why you need to dynamically allocate it with the new command.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int matrix[inputted_row][inputed_column];

// your loops would look like this
for(int row = 0 ; row < inputted_row ; ++row )
{ 
      for(int column = 0 ; column < inputted_column ; ++column )
      {	
             random_number = rand()%100 ;
             cout << random_number << " " ;
             matrix[row][column] = rand_number;

      }
      cout << endl ;
}

// output routine
ofstream yourfile ;
string filename ;

cout << "input a filename : \t" ;
cin >> filename ;

yourfile.open( filename.c_str() );
yourfile << "size: " << inputted_row << "," << inputted_column << endl;
for(int row = 0 ; row < inputted_row ; ++row )
{ 
      for(int column = 0 ; column < inputted_column ; ++column )
      {	
             yourfile << matrix[row][column] << " ";

      }
      yourfile << endl ;
}


yourfile.close() ;


note all the changes I put in the line you need for the other stuff..

I have never dynamically allocated an multi dimensional array like that nor had a need, so I didn't know it wouldn't compile like that. I know ways to get around it but those are complicated for a new programmer.
Last edited on
Topic archived. No new replies allowed.