Ifstream problem

Pages: 12
done
Last edited on
closed account (o3hC5Di1)
Hi there,

Try putting curly braces around your for-loop code:

97
98
99
100
101
102
103
for (int r = 0; r < dimension; r++)   // row loop
{
          for ( int c = 0; c < dimension; c++)
          { // column loop
                inFile >> a[r][c]; // read data into matrix
          }
}


Also, in order to ignore that first character, do this in your readData function, after opening the file:

inFile.ignore(5, '\n')

This will ignore the first five characters or until it encounters a linebreak, whichever happens first.
Hope that helps.

All the best,
NwN

it still out the alien word .

like the output

-8Ea9dsada9 -8Ea9dsada9 -8Ea9dsada9
-8Ea9dsada9 -8Ea9dsada9 -8Ea9dsada9
and so on...
closed account (o3hC5Di1)
Try this:

1
2
3
4
5
6
7
for (int r = 0; r < dimension; r++)   // row loop
{
          for ( int c = 0; c < dimension; c++)
          { // column loop
                inFile >> *(a[r][c]);
          }
}


You are storing pointers in the array, so it will write memory addresses to the file, which are useless when you reopen it.
Using the dereference operator (*), you can get to the value of a pointer.

Hope that helps.

All the best,
NwN
Your problem is scope.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Display (int dimension ) // display matrix
  { 
		cout << " Magic Square Program " << endl << endl;
		int **a;
		a = new int *[dimension];
		for(int i=0;i<dimension;i++)
		a[i]=new int [dimension];
//¿where do you put any values in the `a' matrix?
		for (int r = 0; r < dimension; r++)
       { 
		   for ( int c = 0; c < dimension; c++)
           {
               cout << a[r][c] << "\t ";  // display numbers
           }
           cout << endl;
       }    
   }
error.. the * must be a pointer it said.

ne555.. i just copy from the above.. if not ..
from my

 
cout<< a[r][c];


i don't know and really no idea how to create the a variable...
void Display (int **matrix, int dimension );
so tat in my read data also same rite? with display?

readData(a,limit)
Display(a,limit)
isnt correct?
closed account (28poGNh0)
The solution of you problem is simple ..you should learn alian language ,That's all..

I am just joking :)

first : If we compile your program on visual c++ ,we got 1 error 8 warnings.
7 warnings come from this train :
"C:\\Users\Wendy Chirs\Desktop\Inti\Applied\Assignment 1\Assignment 1\wendy.txt";

you should change It to this new one.
"C:\\Users\\Wendy Chirs\\Desktop\\Inti\\Applied\\Assignment 1\\Assignment 1\\wendy.txt";

or simply
"C:/Users/Wendy Chirs/Desktop/Inti/Applied/Assignment 1/Assignment 1/wendy.txt";

because to introduce a \(backslash) in a string ,should be presided by another backslash for exemple :
cout << "This is a backslash \\, and this is a quotation mark \" " << endl;

The last warning is to use _getch(); instead of getch(); ,This tell you that ,
The function may no longer be supported in a future release(bla bla bla).

The error come from this line inFile.open (fileName);and I think you know why?

The problem of your nice program is that you use **a as a local variable you need to golabalize it!

Also you need to delete the memory you've been taken,It not an error or a warning but It's a big deal .

Finaly I think this new version of your code may works and may helps,
if(The program works fine && you need some explanation) just ask
closed account (28poGNh0)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# include <iostream>
# include <fstream>
# include <string>
# include <vector>
# include <cstdlib>
# include <ctime>
# include <conio.h>
using namespace std;

int **a;

void readData(int);
void Display(int);

void main()
{
	srand(unsigned(time(NULL)));
	int limit = 0,rndNbr = 0;//Initialisation is very good

	cout << "Enter Order Number for matrix : ";
	cin >> limit;

	a = new int *[limit]; 
	for(int i=0;i<limit;i++)
		a[i]=new int [limit];

	// Those lines initialize elements for your a[][] ,also randomize theme
	vector<int>myVect;
	for(int i=0;i<limit*limit;i++)
		myVect.push_back(i+1);

	for(int i=0;i<limit;i++)for(int j=0;j<limit;j++)
	{
		rndNbr = rand()%myVect.size()+1;
		a[i][j] = myVect[rndNbr-1] ;
		myVect.erase(myVect.begin()+rndNbr-1);
	}
	
	cout << "\n\nMagic Square of Order " << limit << " x " << limit << " is \n\n";
	for(int i=0;i<limit;i++)
	{
		for(int j=0;j<limit;j++)
		cout << a[i][j] << "\t";cout << endl;
	}

	char choice = ' ';
	
	while(choice!='q')
	{
		cout << "Do you want to save the file ?[y/n] : ";
		choice = _getch();//To prevent the user, entering a sea of letters

		switch(tolower(choice))
		{
			case 'y':
			{
				ofstream myfile;
				string filename;
				cout << "\nPlease enter a file name to write: ";
				cin.ignore();
				getline( cin, filename);

				myfile.open(filename.c_str());
				myfile << limit <<endl;
				for(int i=0;i<limit;i++)
				{
					for(int j=0;j<limit;j++)
					myfile << a[i][j] << "\t";myfile << endl;
				}
				myfile.close();
				readData(limit);
				Display(limit);
			}
			break;
			case 'n':
			{
				choice = 'q';
				cout << endl;
			}
			break;
			default :
				cout << "Please enter a correct input [y/n]" << endl;
			break;
		}
	}
	/// if you take a memory loacation dont forget to give it back
	for(int i=0;i<limit;i++)
		delete[] a[i];
		delete[] a;

	_getch();
}

void readData(int dimension)
{
	ifstream inFile;
	string fileName = "C:\\Users\\Wendy Chirs\\Desktop\\Inti\\Applied\\Assignment 1\\Assignment 1\\wendy.txt";

	inFile.open (fileName.c_str());  // open file for reading                
	for (int r = 0; r < dimension; r++)   // row loop
	for ( int c = 0; c < dimension; c++)  // column loop
		inFile >> a[r][c]; // read data into matrix
	inFile.close( ); // close the file                
}

void Display (int dimension ) // display matrix
{ 
	cout << " Magic Square Program " << endl << endl;

	for (int r = 0; r < dimension; r++)
    { 
		for ( int c = 0; c < dimension; c++)
		{
		   cout << a[r][c] << "\t ";  // display numbers
		}
		cout << endl;
    }    
}


One last question : why this "3" above the matrix in the file wendy.txt ?
Last edited on
thanks for your code ya. but i think i use should my own basic standing to done the problem. i get it what you mean d . but i still not yet learn that srand(unsigned(time NULL)) so better i keep learning on it before i use it senior , hehe. and i not really ure how to use the vector as i learn the basic in cplusplus.com only.. thanks.. haha
closed account (28poGNh0)
Your welcome everytime.
One last question why this "3" above the matrix in wendy.txt ?
my question required as i need to do so for my txt .

can u see my new thread ?

http://www.cplusplus.com/forum/beginner/78300/

i cannot solve the problem..

sorry ^^

and thanks your always senior^^
The problem of your nice program is that you use **a as a local variable you need to golabalize it!
That's a bad solution. Simply pass it as a parameter.
ne555 . can help me c for my new thread please?

really need someone help to store my txt files into dynamic array ..

no idea how to do it..
http://www.cplusplus.com/forum/beginner/78300/

headache of it..

as I have only basic knowledge about it.
closed account (28poGNh0)
I can agree with you ne555 ,but can you tell me why It's a bad solution
closed account (28poGNh0)
And for you BasicNewbie I asked a question , why this "3" above the matrix in wendy.txt ? why did you put It there ?
Suppose that you have several matrix to operate with. ¿How will you use the functions?

About the `3' ¿which `3'? you are imagining things. xP
closed account (28poGNh0)
Well here I dont agree ,give me an exemple ..try to edit the exemple I did above

and about the '3' ,you miss notice that he edited the problem ,there is no problem tells done(check his problem)

I know, it was a joke. He shouldn't delete his posts.

¿You don't agree with my question?
Pages: 12